My Visitors

Flag Counter

Thursday 15 November 2012

Getting Started With C

Q1.Write a program to calculate and print the area of a square.....

#include<stdio.h>
#include<conio.h>
void main()
{
   clrscr();
  int height,width,area;
  height=5;
  width=4;
 area=height*width;
  printf("Area of  square=%d",area);
 getch();
}


Q2.Write a program that adds two floating point numbers and shows their sum on the screen............

#include<stdio.h>
#include<conio.h>

void main(void)

{

   clrscr();
   float var1,var2,res;
   var1=24.27;
   var2=41.50;
   res=var1+var2;
  printf("%f + %f=%f",var1,var2,res);


getch();

}

Q3.Write a program to convert the distance in kilometers into meters.........

#include<stdio.h>
#include<conio.h>

void main(void)

{

   clrscr();

  double meter,kilometer;
  printf("Enter distance in kilometers:");
  scanf("%lf",&kilometer);
  meter=kilometer*1000;
  printf("\n%lf kilometers=%lf meters",kilometer,meter);

getch();

}


Q4.Write a program that displays the ASCII code of the character typed by the user.....

#include<stdio.h>
#include<conio.h>

void main()

{
   clrscr();
   char ch;

   printf("Enter a character:");
  ch=getche();

  printf("\nThe ASCII code for \'%c\' is %d",ch,ch);

}




Q5.Write a program that displays the value stored in a string variable name......

#include<stdio.h>
#include<conio.h>

void main()

{
     clrscr();
    char name[ ]="ZAIN";
   printf("Name is %s",name);

   getch();

}


Q6.Write a program to display the following output using single printf statement.......

      *
      *  *

      *   *   * 
      *    *   *   *


#include<stdio.h>
#include<conio.h>

void main()

{
   clrscr();

  printf("*\n**\n***\n****");

getch();

}

Q7.Write a program that inputs base and height from the user and calculates area of a triangle by using the formula  Area=1/2*Base*Height........

#include<stdio.h>
#include<conio.h>


void main()


{
   clrscr();


    float base,height;
   double area;
   printf("Enter base:");
  scanf("%f",&base);
  printf("Enter height:");
  scanf("%f",&height);
  area=0.5*base*height;
 printf("Area=%5.2f",area);



getch();


}



Q8.Write a program that inputs name,age and address from the user and displays it on the screen......

#include<stdio.h>
#include<conio.h>

void main()

{

clrscr();

 char name[25],address[30];
 int age;
 printf("Enter your age:");
 scanf("%d",&age);
 printf("Enter your name:");
 scanf("%s",name);
 printf("Enter your address:");
 scanf("%s",address);
 printf("your name is %s\n",name);
 printf("your address is %s\n",address);
 printf("your age is %d\n",age);

getch();

}

Q9.Write a program that gets temperature from the user in celsius and converts it into Fahrenheit using the formula F=9/5*C+32.......

#include<stdio.h>
#include<conio.h>

void main()

{

clrscr();

float cel,faren;
printf("Enter temperature in celcius:");
scanf("%f",&cel);
faren=9.0/5.0*cel+32;
printf("Temperature in Fahrenheit is %5.2f",faren);

getch();

}


Q10.Write a program that gets a three-digit number from the user and displays it in reverse order.For example if the user enter 123,it displays 321.....

#include<stdio.h>
#include<conio.h>

void main()

{

clrscr();

 int n,a,b;
 printf("Enter 3-digit number:");
 scanf("%d",&n);
 a=n/100;
 n=n%100;
 b=n/10;
 n=n%10;
 printf("Number in reverse order is %d%d%d",n,b,a);

getch();

}


Q11.Write a program that inputs 4 numbers and calculates the sum,average, and product  of all the numbers....


#include<stdio.h>
#include<conio.h>

void main()

{

clrscr();

 int a,b,c,d,sum,product;
 float avg;
 printf("Enter 4 numbers:");
 scanf("%d%d%d%d",&a,&b,&c,&d);
 sum=a+b+c+d;
 product=a*b*c*d;
 avg=sum/4.0;
 printf("Sum of all numbers:%d\n",sum);
 printf("Product of all numbers:%d\n",product);
 printf("Average of all numbers:%5.2f",avg);

getch();

}


Q12.Write a program that converts a person's height from inches to centimeters using the formula 2.54*height.....

#include<stdio.h>
#include<conio.h>

void main()

{

clrscr();

int height;
float height_in_cent;
printf("Enter height in inches:");
scanf("%d",&height);
height_in_cent=height*2.54;
printf("Your height in centimeters is:%f",height_in_cent);

getch();

}

Q13.Write a program that inputs two numbers and exchanges their values.The program should display the values of variables before and after exchange.....

#include<stdio.h>
#include<conio.h>

void main()

{

clrscr();

 int x,y,temp;
 printf("Enter two numbers:");
 scanf("%d%d",&x,&y);
 printf("Value in x:%d\n",x);
 printf("Value in y:%d\n",y);
 temp=x;
 x=y;
 y=temp;
 printf("Value in x after exchange:%d\n",x);
 printf("Value in y after exchange:%d\n",y);

getch();

}


Q14.Write a program to find out the area of triangle when three sides a, b and c of the triangle are given.Use appropriate statements to input the values of a, b and c from the keyboard.Formula for the area or triangle is Area=(s(s-a)(s-b)(s-c))1/2  where s=(a+b+c)/2...


#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()

{

clrscr();

 float a,b,c;
 double s,area;
 printf("Enter side A:");
 scanf("%f",&a);
 printf("Enter side B:");
 scanf("%f",&b);
 printf("Enter side C:");
 scanf("%f",&c);
 s=(a+b+c)/2.0;
 area=sqrt(s*(s-a)*(s-b)+(s-c));
 printf("Area of triangle is %f",area);

getch();

}


Q15.Write a program that inputs radius of sphere from the user.Calculates its volume and surface area using the formula Area=4*3.14*r*r and circumference=4/3*3.14*r*r*r....

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
float r,area,cir;
printf("Enter radius:");
scanf("%f",&r);
area=4.0*3.14*r*r;
cir=(4.0/3.0)*3.14*r*r*r;
printf("\nArea is %5.25f\n",area);
printf("Circumference is %5.2f\n",cir);
getch();

}

Q16.Write a program that converts a temperature in degrees.Farhenheit to degrees Celsicus.For conversion,use the following formula C=5/9(F-32)....

#include<stdio.h>
#include<conio.h>

void main()

{

 clrscr();
 float cel,faren;
 printf("Enter temperature in farenheit:");
 scanf("%d",&faren);
 cel=5.0/9.0*(faren-32);
 printf("Temperature in celcius is %f",cel);

getch();

}

Q17.Write a program that displays the following shape using nested loops.The outer loop should be for loop and inner loop should be while loop..

*******
******
*****
****
***
**
*


#include<stdio.h>
#include<conio.h>

void main()
{
   int i,j;
   clrscr();
  for(i=1;i<=7;i++)
{
   j=i;
   while(j<=7)
{
   printf("*");
   j++;
}
   printf("\n");
}

getch();

}

Wednesday 14 November 2012

Looping Structures

Q1.Write a program to display the following format using while loop:
      -------------
      a           b
      -------------
      1           5
      2           4
      3           3
      4           2
      5           1
     -------------


#include<iostream.h>
#include<conio.h>
void main()

{
 clrscr();

   int a=1,b=5;
   cout<<"----------\n";
   cout<<("a\tb\n");
   cout<<"----------\n";
   while(a<=5)
  {
  
    cout<<a<<"\t"<<b<<endl;
   a++;
   b--;
}
 cout<<"----------\n";

getch();

}


Q2. Write a program to display the following  format using while loop:
      ----------------
      num         sum
      ----------------
      1           1
      2           3
      3           6
      4           10
      5           15
      --------------



#include<iostream.h>
#include<conio.h>
void main()

{
 clrscr();

   int num=1,sum=0;
   cout<<"----------\n";
   cout<<("num\tsum\n");
   cout<<"----------\n";
   while(num<=5)
  {
  
    sum=sum+num;
  cout<<num<<"\t"<<sum<<"\n";
   num++;
}
 cout<<"----------\n";

getch();

}

Q3. Write a program that displays the sum of the following series using do-while loop.
1+1/4+1/8+......+1/100

#include<iostream.h>
#include<conio.h>
void main()

{
  clrscr();

   float c,r;
   c=4.0;
   r=1.0;
   do
{
    r=r+1.0/c;
    c=c+4;
}
   while(c<=100);
   cout<<"Result is "<<r;

getch();

}


Q4.Write a program to display alphabets from A to Z using for loop...

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();

     char ch;
     for(ch='A';ch<='Z';ch++)
      cout<<ch;

getch();

}






Conditional Structures

Q1.Writing a program that accepts a character and determines whether the character is a lowercase letter.Alowercase letter is any character that is greater than equal to 'a' nd less than or equal to 'z' .If tha entered character is a lowercase letter,display the message"Enter character is a lowercase letter",otherwise display the message "Entered character is not a lowercase letter"....


#include<iostream.h>
#include<conio.h>

void main()

{

 clrscr();
 char letter;
 cout<<"enter a letter:";
 cin>>letter;
 if(letter>='a'&& letter<='z')

   cout<<"enter character is a lowercase letter\n";
 else
 cout<<"enter character is not a lowercase letter\n";

getch();

}



Q2.Senior salesperson is paid Rs.400 a week, and a junior salesperson is paid Rs.275 a week .Write a program that accepts as input a salesperson's status in the character variable status.If status is 's' or 'S',the senior person's salary should be displayed;if status is'j' or 'J',the junior person's salary should be displayed,otherwise display error message......


#include<iostream.h>
#include<conio.h>

void main()

{

clrscr();
char status;
int senior=400,junior=275;
cout<<"S and s is for senior person salary\n";
cout<<"J and j is for junior person's salary\n";
cout<<"enter status:";
cin>>status;
if(status=='S'|| status=='s')
cout<<"Senior person salary is rs."<<senior;
else if(status=='J'||status=='j')
cout<<"Junior person salary is rs."<<junior;
else
cout<<"you should select senior or junior, please try again ";

getch();

}


Q3.Write a program to get three  numbers from user for integer variables a,b and c.If a is not zero,find out whether it is the commom divisor of b and c....
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<"enter the values of a,b,c";
cin>>a>>b>>c;
if(a==0)
{
cout<<"divisor cannot be 0"<<endl;
}
else
{
if(b%a==0&& c%a==0)
cout<<a<<"is a common divisor of"<<b<<"and"<<c<<endl;
else
cout<<a<<"is not a common divisor of"<<b<<"and"<<c<<endl;

}


getch();

}


Q4.Write a program that contains an if statement  that may be used to compute the area of a square(area=side*side) or a triangle(area=1/2*base*height) after  prompting the user to type the first character  of the figure name (SorT).....



#include<iostream.h>
#include<conio.h>

void main()

{

clrscr();
char op;
int side,base,height;
float area;
cout<<"enter choice(S for square,T for triangle:)";
cin>>op;
if(op=='s')
{
cout<<"enter side:";
cin>>side;
area=side*side;
cout<<"Area="<<area;
}
else if(op=='T')
{
cout<<"enter base:";
cin>>base;
cout<<"enter height:";
cin>>height;
area=base*height*0.5;
cout<<"Area="<<area;
}

getch();

}



Q5.Write a program that gets the number and a letter.If the  letter is'f' ,the program should treat the number entered as temperature in degrees Fahenheit and convert it  to the temperature in degree Celsius and print a suitable message.If the letter is'c',the program should consider the number as Celsius temperature and convert it to Fahrenheit temperature and print  a suitable message.The program should display error message and then exit if the user enters any other letter.......


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float temp,temp2;
char choice;
cout<<"enter temperature";
cin>>temp;

cout<<"f is used to convert fahrenheit into celsius";
cout<<"c is used to convert celsius into fahrenheit";
cout<<"enter choice";
cin>>choice;

if(choice=='f')
{
temp2=5.0/9.0*(temp-32);
cout<<"Temperature in celcius is"<<temp2<<endl;
}
else if(choice=='c')
{
temp2=9.0/5.0*temp+32;
cout<<"Temperature in fahrenheit is "<<temp2;
    }
else
cout<<"Invalid choice:";

getch();

}



Q6..Write a program that accepts the code number as an input and display the correct disk drive manufacture as follows:

Code        disk drive manufacture

1              Western Digital
2              3M Corporation
3              Maxell Corporation
4              Sony Corporation
5              Verbatim Corporation


#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
  int code;
 cout<<"**********\n";
 cout<<"code numbers\n";
 cout<<"**********\n";
 cout<<"1 for western digital\n";
 cout<<"2 for 3M corporation \n";
 cout<<"3 for maxil corporation\n";
 cout<<"4 for sony corporation\n";
 cout<<"5 for verbatim corporation\n";
 cout<<"\n enter any code:";
 cin>>code;
switch(code)
{
 case 1:
 cout<<"western digital";
 break;
 case 2:
 cout<<"3M corporation";
 break;
 case 3:
  cout<<"maxil corporation";
 case 4:
 cout<<"sony corporation";
 case 5:
 cout<<"verbatim corporation";

}

getch();

}


Q7..Write a program that uses the following categories of movies:

A for Adventure movies
C for Comedy movies
F for Family movies
H for Horror movies
S for Science Friction movies


The program tnputs code for movie type and displays its category.For example if the user enters H,it displays "Horror Movies".The program should also display a menu of movie categories....


#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 char ch;
 cout<<"**********\n";
 cout<<"Movies Categories\n";
 cout<<"**********\n";
 cout<<"A for adventure movie\n";
 cout<<"C for comedy movie\n";
 cout<<"F for family movie\n";
 cout<<"H for horro movie\n";
 cout<<"S for science fiction movie\n";
 cout<<"enter code of movie category:";
 cin>>ch;
switch(ch)
{
 case 'A':
 cout<<"adventure movie";
 break;
 case'C':
 cout<<"comedy movie";
 break;
 case'F':
 cout<<"family movie";
 case 'H':
 cout<<"horror movie";
 break;
 case 'S':
 cout<<"science fiction movies";
 break;
 default:
 cout<<"invalid category";


}

getch();

}


Q8.Write a program that inputs a value and type of conversion.The program should then output the value after conversion.The program should  include the following conversions:

1 inch=2.54centimeters
1 gallon=3.785liters
1 mile= 1.609kilometers
1 pound=.4536 kilograms


Make sure that program acceptsonly valid choices for type of conversion to perform....

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 float value;
 char con;
 cout<<"$$$$$$$$$$\n";
 cout<<"enter conversion type:\n";
 cout<<"$$$$$$$$$$\n";
 cout<<"C for centimeters\n";
 cout<<"L for liters\n";
 cout<<"K for kilometers\n";
 cout<<"G for kilograms\n";
 cin>>con;
 cout<<"enter a value:";
 cin>>value;
switch(con)
{
 case 'C':
 case 'c':
 cout<<"value"<<value*2.54;
 break;
 case 'L':
 case 'l':
 cout<<"value"<<value*3.785;
 case 'K':
 case'k':
 cout<<"value"<<value*1.609;
 break;
 case'G':
 case 'g':
 cout<<"value"<<value*.4536;
 break;
 default:
 cout<<"invalid conversion type";

}


getch();

}

Q9.A year is a leap year if it is divisible by four,except that any year divisible by 100 is a leap year only if it is divisible by 400.Write a program that inputs a year such as 1996,1800 and 2010 and display "if it is a leap year otherwise displays "Not a leap year".
#include<iostream.h>
#include<conio.h>
void main()

{
 clrscr();
 int year;
 cout<<"enter a year:";
 cin>>year;
 if(year%4==0)
 cout<<"leap year";
 else if(year%100==0&&year%400==0)
 cout<<"leap year" ;
 else
 cout<<"not a leap year";
 getch();

}


Q10.Write a program that inputs temperature and displays a message follows:
 

Temperature                                        Message
Greater then 35                                    Hot day
Between 25 and 35 (inclusive)               Pleasant day
Less than 25                                        Cool day



#include<iostream.h>
#include<conio.h>
void main()

{
  clrscr();
  int t;
  cout<<"enter temperature";
  cin>>t;
  if(t>35)
  cout<<"hot day";
  else if(t>=25)
  cout<<"cool day";
  getch();

}



Q11.Write a program that inputs obtained marks of a student,calculates the percentage.(assuming total marks are 1100) and displays grade according to the following rules.
   
   Percentage                                Grade
  
   More than or equal to 80              A+
   Between 70(inculsive) and 80        A
   Between 60(inculsive) and 70        B
   Between 50(inculsive) and 60        C   
   Between 40(inculsive) and 50        D
   Between 330(inculsive) and 40      E
   Less than 33                                  F


#include<iostream.h>
#include<conio.h>
void main()

{
 clrscr();
 int m;
 //char g;
 float a;
 cout<<"enter marks";
 cin>>m;
 a=(m*100.0)/1100.0;
 if(a>=80)
 cout<<"A+";
 else if(a>=70)
 cout<<"A";
 else if(a>=60)
 cout<<"B";
 else if(a>=50)
 cout<<"C";
 else if(a>=40)
 cout<<"D";
 else if(a>=33)
 cout<<"E";
 else
 cout<<"F";
 getch();

}

Q12.Write a program tha converts MILITARY time to STANDARD time.



#include<iostream.h>
#include<conio.h>
void main()

{
 clrscr();
 int h;
 double m;
 cout<<"enter 24 hours military hours:";
 cin>>h;
 cout<<"enter 24 houes military minutes:";
 cin>>m;
 if(h>12&&h<24)
{
 h=h-12;
 cout<<"standard time:" <<h<<":"<<m<<"PM" <<endl;
}
 else if(h==24)
{
 h=h-12;
 cout<<"standard tim:"<<h<<":"<<m<<"AM"<<endl;
}
 else if (h==12)
 cout<<"standard time:"<<h<<":"<<m<<"PM"<<endl;
 else
 cout<<"standard time:"<<h<<":" <<m<<"AM"<<endl;
 getch();

}



Q13.Write a program that will take three values a,b and c and print the roots, if real, of the quadratic equation ax*X+b*x+c=0.Sample input 1:(a=1, b=1,c=-6 the output should be "Roots of the equation are 2 and -3").Sample input 2(if the input is a=1,b=0,c=9, the output should be "Sorry the roots are not real.")


#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<iomanip.h>
void main()

{
 clrscr();
 int a,b,c,d,e;
 double f,x1,x2;
 cout<<"enter value of a";
 cin>>a;
 cout<<"enter value of b";
 cin>>b;
 cout<<"enter value of c";
 cin>>c;
 d=b*b-4*a*c;
 e=2*a;
if(a==0)
 cout<<"not a eq A cannot be zero"<<endl;
 else if(d<0)
 cout<<"sorry the roots are not real"<<endl;
else
{
  f=pow(d,0.5);
  x1=(-b+f)/e;
  x2=(-b-f)/e;
  cout<<"the roots of eq are"<<x1<<","<<x2<<endl;

 }
 getch();

}

Q14.Write a program that inputs the salary of an employee from the user.It deducts the income tax from the salary on the following basis:

. 20% income tax if the salary is above Rs.30000.
.15%  income tax if the salary is between Rs.20000 and Rs.30000
.10% income if the salary is below Rs.20000.


The program finally displays salary,income tax and net salary.

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()

{
 int sal;
 float net,in;
 clrscr();
 cout<<"enter salary:";
 cin>>sal;
if(sal>30000)
{
 in=(20/100);
 net=sal-(sal*in);
 cout<<"net salary is:"<<setprecision(2)<<net;
 cout<<"and income tax is:"<<in;
}
 else if(sal>20000&&sal<30000)
{
 in=15/100;
 net=sal-(sal*in);
 cout<<"net salary is:"<<net<<"income tax is:"<<in;;
}
else
 sal=net;
 cout<<"net salary is"<<net;
getch();

}


Q15.Write a program that inputs year and month.It displays the number of days in the month of the year entered by the user.For example,if the user enters 2010 in year and 3 in month, the program should display"March 2010 has 31 days".

#include<iostream.h>
#include<conio.h>
void main()

{
clrscr();
int year,month,number_of_days_in_month=0;
cout<<"enter a year\n";
cin>>year;
cout<<"enter a month in the year";
cin>>month;
switch(month)
{
case1:
cout<<"january"<<year;
number_of_days_in_month=31;
break;
case2:
cout<<"feburay"<<year;
if(year%400==0||(year%4==0&& year%100!=0))
number_of_days_in_month=29;
else
number_of_days_in_month=28;
break;
case 3:
cout<<"march"<<year;
number_of_days_in_month=31;
break;
case 4:
cout<<"april"<<year;
number_of_days_in_month=30;
break;
case 5:
cout<<"may"<<year;
number_of_days_in_month=31;
case 6:
cout<<"june"<<year;
number_of_days_in_month=30;

break;

case 7:
cout<<"july"<<year;
number_of_days_in_month=31;

break;
case 8:
cout<<"august"<<year;
number_of_days_in_month=31;
break;
case 9:
cout<<"september"<<year;
number_of_days_in_month=30;
break;

case 10:
cout<<"october"<<year;
number_of_days_in_month=31;
break;
case 11:
cout<<"november"<<year;
number_of_days_in_month=30;
break;
case 12:
cout<<"december"<<year;
number_of_days_in_month=31;
break;
}
cout<<"has"<<number_of_days_in_month<<"days";

getch();
}



Q16.Write a program that displays the following menu for a parking area:

. M  =Motorcycle
. C  =Car
. B  =Bus


The program inputs the type of vehicle and number of day to park the vehicle.It finally displays the total charges for the parking according to the following:

.Motorcycle    Rs. 10 per day
.Car           Rs.20 per day 
.Bus           Rs.30 per day




#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int days;
 char p;
 cout<<"M=motorcycle\n";
 cout<<"C=car\n";
 cout<<"B=bus\n";
 cout<<"enter parking choice:";
 cin>>p;
 cout<<"how many days to park?";
 cin>>days;
switch(p)
{
 case'M':
 case'm':
 cout<<"parking charges: Rs."<<days*10;
 break;
 case 'C':
 case 'c':
 cout<<"parking charges: Rs."<<days*20;
 break;
 case 'B':
 case 'b':
 cout<<"parking charges:Rs."<<days*30;
 break;
 default:
 cout<<"invalid parking choice";

}

getch();

}


Q17.Write a program that inputs a value and type of conversion.The program should then display the output after conversion.The program should include the following conversions.

. 1cm = .394 inches
. 1 liter = .264 gallons
. 1 kilometer =.622 nmiles
. 1 kilogram = 2.2 pounds


Make sure that the program accepts only valid choices for the type of conversion...
#include<iostream.h>
#include<conio.h>

void main()

{
 clrscr();
 float value;
 char conversion;
 cout<<"enter the value"<<endl;
 cin>>value;
 cout<<"enter the type of conversion"<<endl;
 cout<<"I inches"<<endl;
 cout<<"G gallons"<<endl;
 cout<<"M miles"<<endl;
 cout<<"p pounds"<<endl;
 cin>>conversion;
switch(conversion)
{
 case 'I':
 case 'i':
 cout<<value*.394<<endl;
 break;
 case 'G':
 case 'g':
 cout<<value*.264<<endl;
 break;
 case 'M':
 case 'm':
 cout<<value*.622<<endl;
 case 'P':
 case 'p':
 cout<<value*2.2<<endl;
 break;
 default:
 cout<<"illegal input"<<endl;
 break;
}
getch();

}