If Else Statement
If Else Statement
. Create a C++ program that will input two integer numbers and will output if the first is greater, less than or equal to the second integer. Solution: #include<iostream.h> int n1,n2; int main() { cout<<"Input first integer number:"; cin>>n1; cout<<"Input second integer number:"; cin>>n2;
if (n1<n2) { cout<<"first number is less than the second number"; } else if (n1>n2) { cout<<"first number is greater than the second number"; } else { cout<<"first number is equal to the second number"; } system ("PAUSE"); return 0; }
2. Create a C++ program that will input the test score and will output the grade.
Solution: #include<iostream.h> int t; int main() { cout<<"Enter your test score:"; cin>>t; if (t>=90) { cout<<"Your Grade is an A"; } else if (t>=80) { cout<<"Your Grade is a B"; } else if (t>=70) { cout<<"Your Grade is a C"; } else if (t>=60) { cout<<"Your Grade is a D"; } else { cout<<"Your Grade is a E"; } system ("PAUSE"); return 0; }
59 below
3. Create a C++ program that will input age and will output if you can vote. Solution: #include<iostream.h> int age; int main() { cout<<"Input your age:"; cin>>age; if ( age >= 18) { cout<<"YOU CAN VOTE!!!!!"; } else { cout<<"SORRY YOU CAN'T VOTE"; } system ("PAUSE"); return 0; }
4. Create a C++ program that will input the training grade and will output the training grade level. Solution: #include<iostream.h> int grade; int main() { cout<<"Input the Training Grade Level:"; cin>>grade; if ( grade >= 90) { cout<<"Best Level"; } else if ( grade >=80 ) { cout<<"Good Level"; } else { cout<<"Needs Improvement"; } system ("PAUSE"); return 0; }
5. Write a program so that if the clerk enters the number of miles, the program would display the total price owed. Where: Mileage is less than 100 = 0.25 Mileage is more than 100 = 0.15
Solution: #include<iostream.h> int miles; float tp; int main() { cout<<"Enter Number of Miles:"; cin>>miles; if ( grade >= 100) { tc=miles x 0.15; } else { tc=miles x 0.25 } cout<<The Total Cost is:<<tc<<endl; system ("PAUSE"); return 0; }