Sunday, January 24, 2016

C++ if else and logical operators

1. Write a C++ program that prompts the user to input three integer values and find the greatest value of the three values. Example:   En... thumbnail 1 summary

1. Write a C++ program that prompts the user to input three integer values and find the greatest value of the three values.

Example: 
Enter 3 integer vales separated by space: 10 15 20 
The greatest value is: 20

Solution:
#include <cstdlib> 
#include <iostream> 
#include<iomanip> 
using namespace std; 
int main(int argc, char *argv[]) 
{ 
      int a,b,c,max;
     cin>>a>>b>>c;
     max=a; //let max take the first value
   if(max<b) max=b; // compare max with b and update max
  if(max<c) max=c; //compare max with c and take c
   cout<<"Max: "<<max; //output max
   system("PAUSE"); 
   return EXIT_SUCCESS; 
}

2. Write a program that determines a student’s grade. The program will read three types of scores (quiz, mid-term, and final scores) and determine the grade based on the following rules:

-if the average score =90% =>grade=A 
-if the average score >= 70% and <90% => grade=B 
-if the average score>=50% and <70% =>grade=C 
-if the average score<50% =>grade=F
Solution:
#include <cstdlib> 
#include <iostream> 
#include<iomanip> 
  
using namespace std; 
int main(int argc, char *argv[]) 
{ 
   float x; 
   float y; 
   float z; 
   float avg; 
   cout<<"Enter 3 score(quiz, mid-term, and final) vales separated by space:"; 
   cin>>x>>y>>z; 
   avg=(x+y+z)/3; 
   if(avg>=90)cout<<"Grade A"; 
   else if((avg>=70) && (avg<90)) cout<<"Grade B"; 
   else if((avg>=50) && (avg<70))cout<<"Grade C"; 
   else if(avg<50) cout<<"Grade F"; 
   else cout<<"Invalid"; 
   cout<<"\n"; 
   system("PAUSE"); 
   return EXIT_SUCCESS; 
}

No comments

Post a Comment