﻿ C++ exercises and solutions: C++ if else and logical operators

# C++ exercises and solutions: C++ if else and logical operators

## 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:
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

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;

else if((avg>=70) && (avg<90)) cout<<"Grade B";
else cout<<"Invalid";

cout<<"\n";
system("PAUSE");
return EXIT_SUCCESS;
}

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.