My Visitors

Flag Counter

Wednesday 14 November 2012

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();

}





11 comments: