Conditional Structures in C++
Thursday, 26 May 2016
Q: 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".
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int year, month, numofdaysinmonth=0;
cout<<"Enter a year:";
cin>>month;
cout<<"Enter a month in the year( e.g 1 for Jan): ";
cin>>month;
switch(month)
{
case 1:
cout<<" January "<<year;
numofdaysinmonth=31;
break;
case 2:
cout<<" February "<<year;
if(year%400==0 || year%4==0 || year%100!=0)
numofdaysinmonth=29;
else
numofdaysinmonth=28;
break;
case 3:
cout<< " March "<<year;
numofdaysinmonth=31;
break;
case 4:
cout<<" April "<<year;
numofdaysinmonth =30;
break;
case 5:
cout<<" May "<<year;
numofdaysinmonth= 31;
break;
case 6:
cout<<" June "<<year;
numofdaysinmonth= 30;
break;
case 7:
cout<<" July "<<year;
numofdaysinmonth = 31;
break;
case 8:
cout<< " August "<<year;
numofdaysinmonth = 31;
break;
case 9:
cout<<" September "<<year;
numofdaysinmonth = 30;
break;
case 10:
cout<<" October "<<year;
numofdaysinmonth = 31;
break;
case 11:
cout<<" November " <<year;
numofdaysinmonth = 30;
break;
case 12:
cout<<" December "<<year;
numofdaysinmonth =31;
break;
}
cout<<" has "<<numberofdaysinmonth<< " days ";
getche();
}
Wednesday, 25 May 2016
Conditional Structures
Q: 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 salesperson's salary should be displayed, otherwise display error message.
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
char status;
int senior=400, junior=500;
clrscr();
cout<<" Enter Status:";
cin>>status;
if(status='s' || status='S')
cout<<"Senior Salesperson's salary is: "<<senior;
else if(status='j' || status='J')
cout<<"Junior Salesperon's salary is: "<<junior;
else
cout<<"Selected status is wrong, Try Again!";
getche();
}
Q: Write a program to get three numbers from the user for integer variables a,b and c. if a is not zero, find out whether it is the common divisor of b and c.
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
cout<<"Enter the value of a,b and c:";
cin>>a>>b>>c;
if(a==0)
cout<<"Divisor can not be zero!";
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;
}
getche();
}
Q: 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 ( S or T)
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
char op;
int side, base, height;
float area;
clrscr();
cout<<"Enter choice (S for Square, T for Triangle) ";
cin>>op;
if(op=='S)
{
cout<<"Enter Side:";
cin>>side;
area=side*side;
cout<<" Area of Square is: "<<area;
}
else if (op=='T')
{
cout<<"Enter base:";
cin>>base;
cout<<"Enter height:";
cin>>height;
area=base*height*0.5;
cout<<"Area of Triangle is: "<<area;
}
getche();
}
Q: 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.54 centimeters
1 gallon = 3.785 liters
1 mile = 1.609 kilometers
1 pound = 0.4536 kilograms
Make sure the program accepts only valid choices for type of conversion to perform
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
float value;
char con;
clrscr();
cout<<"Enter conversion type:\n";
cout<<"C for Centimeters \n";
cout<<"L for Litres\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;
break;
case 'K':
case'k':
cout<<"Value: "<<value*1.609;
break;
case 'G':
case 'g':
cout<<"Value: "<<value*0.4536;
break;
default;
cout<<"Invalid conversion type!";
}
getche();
}
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
char status;
int senior=400, junior=500;
clrscr();
cout<<" Enter Status:";
cin>>status;
if(status='s' || status='S')
cout<<"Senior Salesperson's salary is: "<<senior;
else if(status='j' || status='J')
cout<<"Junior Salesperon's salary is: "<<junior;
else
cout<<"Selected status is wrong, Try Again!";
getche();
}
Q: Write a program to get three numbers from the user for integer variables a,b and c. if a is not zero, find out whether it is the common divisor of b and c.
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
cout<<"Enter the value of a,b and c:";
cin>>a>>b>>c;
if(a==0)
cout<<"Divisor can not be zero!";
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;
}
getche();
}
Q: 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 ( S or T)
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
char op;
int side, base, height;
float area;
clrscr();
cout<<"Enter choice (S for Square, T for Triangle) ";
cin>>op;
if(op=='S)
{
cout<<"Enter Side:";
cin>>side;
area=side*side;
cout<<" Area of Square is: "<<area;
}
else if (op=='T')
{
cout<<"Enter base:";
cin>>base;
cout<<"Enter height:";
cin>>height;
area=base*height*0.5;
cout<<"Area of Triangle is: "<<area;
}
getche();
}
Q: 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.54 centimeters
1 gallon = 3.785 liters
1 mile = 1.609 kilometers
1 pound = 0.4536 kilograms
Make sure the program accepts only valid choices for type of conversion to perform
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
float value;
char con;
clrscr();
cout<<"Enter conversion type:\n";
cout<<"C for Centimeters \n";
cout<<"L for Litres\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;
break;
case 'K':
case'k':
cout<<"Value: "<<value*1.609;
break;
case 'G':
case 'g':
cout<<"Value: "<<value*0.4536;
break;
default;
cout<<"Invalid conversion type!";
}
getche();
}
Tuesday, 24 May 2016
Writing a program that accepts a character and determines whether the character is a lowercase letter. a lowercase letter is any letter that is greater than equal to 'a' and less than or equal to 'z'. if the entered character is a lowercase letter, display the message "Entered character is a lowercase letter" , otherwise display the message "Entered character is not a lowercase letter".
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
char letter;
clrscr();
cout<<"enter a character:";
cin>>letter;
if(letter>='a' && letter<='z')
cout<<"Entered character is a lowercase letter\n";
else
cout<<"Entered character is not a lowercase letter";
getche();
}
Subscribe to:
Posts (Atom)