c++ lab sheet kuppiya- 4

10.Write a program to input 2 integers and find the smaller number

#include<iostream.h>

int main()
{
int a;
int b;
cout<<“Please enter your 1st number: “;
cin>>a;
cout<<“Please enter your 2nd number: “;
cin>>b;
if(a<b)
{
cout<<“The smaller is…………..: “<<a<<endl;
}
else
{
cout<<“The smaller is…………..: “<<b<<endl;
}

return 0;
}

11.Write c++ program to input an integer and tell whether the number is odd or even.

#include <iostream.h>

int main()
{
int a;
int rem;
cout<<“Please enter number: “;
cin>>a;
rem=a%2;
if(rem==0)
{
cout<<“The number is Even”<<endl;
}
else
{
cout<<“The number is Odd”<<endl;
}

return 0;
}

12.Write a program to create a simple calculator. The program should allow the user to enter two numbers and an option. The options are

1.+

2.–

3.*

4./

You should print an appropriate error message if the user enters invalid data.

Use nested if-else statements in your program.

#include <iostream.h>

int main()
{
float num1,num2;
char option;
cout<<“Input No 1..: “;
cin>>num1;
cout<<“Input option: “;
cin>>option;
cout<<“Input No 2..: “;
cin>>num2;
if(option==’+’)
{
cout<<“Answer is…: “<<num1+num2<<endl;
}
else if(option==’-‘)
{
cout<<“Answer is…: “<<num1-num2<<endl;
}
else if(option==’*’)
{
cout<<“Answer is…: “<<num1*num2<<endl;
}
else if(option==’/’)
{
cout<<“Answer is…: “<<num1/num2<<endl;
}
else
{
cout<<“\aERROR”;
}
return 0;
}

13.Write a program to get the Number as a key board input and check whether this number is greater than 100. if number is greater than 100 print “Number is greater than 100” otherwise print “Number is less than or equal 100”.

#include <iostream.h>

int main()
{
int num1;
cout<<“Enter Number: “;
cin>>num1;
if(num1>100)
{
cout<<“Number is greater than 100″<<endl;
}
else
{
cout<<“Number is less than or equal 100″<<endl;
}
return 0;;
}

14.Write a program that reads the user’s age and then prints “you are a child” if the age <18 , “you are an adult” if 18<=age<<<65, and you are a senior citizen if age>=65.

#include <iostream.h>

int main()
{
int age;
cout<<“Please Enter your age: “;
cin>>age;
if(age<18)
{
cout<<“you are a child”<<endl;
}
else if(age<65)
{
cout<<“you are an adult”<<endl;
}
else
{
cout<<“you are a senior citizen”<<endl;
}
return 0;
}

  1. Write a program that will help the driver of a truck that delivers and picks up compressed-gas cylinders to determine the contents of the cylinder based on the color of the cylinder. The program will print out a menu of colors to be entered. The user should then be instructed to enter the first letter of the color of the tank. Upon reading the letter corresponding to the color of the tank the program will print a message, which states the contents of the tank. The program should accept the letter in either upper or lower case. Cylinder colors associated contents are as follows.

Orange                        ammonia

Brown                         carbon monoxide

Yellow                        hydrogen

Green                          oxygen

The program should be written using simple if statements. These if statements can have compound conditions. (Conditions which have logical operators). Have the program print a message prior to ending such as “Glad to be of service”. If the user enters a color other than one of the four colors specified in the program, then print only the ending message.

Note:- use a char type variable for the input.

#include <iostream.h>

int main()
{
char clr,O,B,Y,G;
cout<<“Color Menu\n”
“———-\n”
“Orange ammonia\n”
“Brown carbon monoxide\n”
“Yellow hydrogen\n”
“Green oxygen\n”
“”<<endl;
cout<<“Please enter the first letter of the color of the tank”<<endl;
cout<<“Enter Cylinder Colour(O,B,Y,G): “;
cin>>clr;
cout<<“”<<endl;
if(clr==’O’){
cout<<“You Selected Cylinder content Ammonia”<<endl;
}
else if(clr==’B’)
{
cout<<“You Selected Cylinder content Carbon Monoxide”<<endl;
}
else if(clr==’Y’)
{
cout<<“You Selected Cylinder content Hydrogen”<<endl;
}
else if(clr==’G’)
{
cout<<“You Selected Cylinder content Oxygen”<<endl;
}
else
{
cout<<“Glad to be of service”<<endl;
}

return 0;
}

  1. Write an appropriate control structure that will examine the value of a following point variable called temp and print one of the following messages, depending on the value assigned to temp.
  • ICE, if the value of temp is less than 0.
  • WATER, if the value of temp lies between 0 and 100,
  • STEAM, if the value of temp exceeds 100.

#include <iostream.h>

int main()
{
int temp;
cout<<“Please Enter temperature: “;
cin>>temp;
if(temp<0)
{
cout<<“ICE”<<endl;
}
else if(temp<=100)
{
cout<<“WATER”<<endl;
}
else
{
cout<<“STEAM”<<endl;
}
return 0;
}

  1. Write a program that reads a grade A, B, C, D or F and then print “Excellent”, “Good”, “Fair”, “Poor”, and “Fail”.

#include <iostream.h>

int main()
{
char grade,A,B,C,D,F;
cout<<“Enter Grade (A,B,C,D,F): “;
cin>>grade;
if(grade==’A’)
{
cout<<“Excellent”<<endl;
}
else if(grade==’B’)
{
cout<<“Good”<<endl;
}
else if(grade==’C’)
{
cout<<“Fair”<<endl;
}
else if(grade==’D’)
{
cout<<“Poor”<<endl;
}
else
{
cout<<“Fail”<<endl;
}
return 0;
}

  1. Write a program to sort the three integers a, b and c in ascending order. The number will be given from the keyboard

#include <iostream.h>

int main()
{
int a,b,c;
cout<<“Please Enter 3 Numbers:\n”;
cin>>a>>b>>c;
cout<<“Your Numbers Are Sorting:”;
if(a<b&&b<c)
{
cout<<a<<“,”<<b<<“,”<<c<<endl;
}
else if(a<c&&c<b)
{
cout<<a<<“,”<<c<<“,”<<b<<endl;
}
else if(b<a&&a<c)
{
cout<<b<<“,”<<a<<“,”<<c<<endl;
}
else if(b<c&&c<a)
{
cout<<b<<“,”<<c<<“,”<<a<<endl;
}
else if(c<a&&a<b)
{
cout<<c<<“,”<<a<<“,”<<b<<endl;
}
else
{
cout<<c<<“,”<<b<<a<<“,”<<endl;
}
return 0;

}

19.Create a program that test whether a given integer is zero or positive or negative.

#include <iostream.h>

int main()
{
int num;
cout<<“Enter Number:”;
cin>>num;
if(num<0)
{
cout<<“Negative”<<endl;
}
else if(num==0)
{
cout<<“Zero”<<endl;
}
else
{
cout<<“Positive”<<endl;
}
return 0;
}

Please comment.Thanks

Leave a Reply to Anonymous Cancel reply

Your email address will not be published. Required fields are marked *