1. Create a C++ program to accept two integers and check if it is greater than or equal to 20.
2. Build a C++ program to read the temperature in centigrade and display a suitable message according to the temperature state below: (nested-if else/switch case)
Temp < 0 then Freezing weather
Temp 0-10 then Very Cold weather
Temp 10-20 then Cold weather
Temp 20-30 then Normal in Temp
Temp 30-40 then it is Hot
Temp >=40 then it is Very Hot
Example input: 42
Expected Output:
It is very hot.
#include <iostream>
using namespace std;
int main() {
int Temp;
cout<<"Enter the temperature: ";
cin>> Temp;
if(!cin){ // validation
cout<<"Invalid input."<<endl;
cin.clear(); // clears the error flag
exit(1);
}
if(Temp<0)
cout << "Freezing weather";
else if(Temp<10)
cout << "Very cold weather";
else if(Temp<20)
cout<<"Cold weather";
else if(Temp<30)
cout<<"Normal in Temp";
else if(Temp<40)
cout<<"It is hot";
else
cout<<"It is very hot";
cout<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n1, n2;
cout<<"Enter n1: ";
cin>> n1;
if(!cin){ // validation
cout<<"Invalid input. Must be int"<<endl;
cin.clear(); // clears the error flag
exit(1);
}
if(!cin){ // validation
cout<<"Invalid input. Must be int"<<endl;
cin.clear(); // clears the error flag
exit(1);
}
cout<<"Enter n2: ";
cin>> n2;
if(!cin){ // validation
cout<<"Invalid input. Must be int"<<endl;
cin.clear(); // clears the error flag
exit(1);
}
// check
if(n1>20)
cout<<"n1 > 20"<<endl;
if(n1==20)
cout<<"n1 == 20"<<endl;
if(n2>20)
cout<<"n2 > 20"<<endl;
if(n2==20)
cout<<"n2 == 20"<<endl;
cout<<endl;
return 0;
}
Comments
Leave a comment