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
-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;
}

Comments

zanib comment

 zanib

write a program for switch statment & find vowel or inconent?


2017-12-21
LK comment

 LK

Example : EDC calculate

if user used less than 70KW, so 1KW = 350 riel
if user used less than 100KW but greater than 70KW, so 1KW = 500riel
if user used less than 450KW but greater than 100KW, so 1KW = 770riel
if user used greater than 450Kw, so 1KW = 840reil
=========================================================================
=========================================================================

Solution:

#include<iostream>
using namespace std;

int main()
{
int previous, current, consumption;
float total,dollar;
cout<<"Enter Old Number = ";
cin>>previous;
cout<<"Enter New Number = ";
cin>>current;

if(current <= previous)
{
cout<<endl<<"Your data is incorrect, Current Number can't less than Previous Number"<<endl;
cout<<"Please Insert Data Again"<<endl;
cout<<endl<<"Enter New Number = ";
cin>>current;
}

consumption = current - previous;
cout<<endl<<"Consumption = "<<consumption<<" KW"<<endl;

if(consumption < 70)
{
total = consumption * 350;
dollar = total/4100;
cout<<endl<<"Total Due (riel) = "<<total<<" riel";
cout<<endl<<"Total Due (dollar) = "<<dollar<<" dollar"<<endl;
}
else if(consumption < 100)
{
total = (consumption - 70) * 500 + (70*350);
dollar = total/4100;
cout<<endl<<"Total Due (riel) = "<<total<<" riel";
cout<<endl<<"Total Due (dollar) = "<<dollar<<" dollar"<<endl;
}
else if(consumption < 450)
{
total = (consumption - 100) * 770 + (100*500) + (70*350);
dollar = total/4100;
cout<<endl<<"Total Due (riel) = "<<total<<" riel";
cout<<endl<<"Total Due (dollar) = "<<dollar<<" dollar"<<endl;
}
else
{
total = (consumption - 450) * 840 + (450*770) + (100*500) + (70*350);
dollar = total/4100;
cout<<endl<<"Total Due (riel) = "<<total<<" riel";
cout<<endl<<"Total Due (dollar) = "<<dollar<<" dollar"<<endl;
}
return 0;
}




2017-12-19
mina comment

 mina

#include <iostream>

using namespace std;

int main()
{
int x,y,z;
cout<<"Enter 3 numbers : ";
cin>>x>>y>>z;
if(x>y && x>z)
cout<<"The large number is = "<<x<<endl;
else if(y>x && y>z)
cout<<"The large number is = "<<y<<endl;
else
cout<<"The large number is = "<<z<<endl;
}


2017-11-03
sukhveer khokhar comment

 sukhveer khokhar

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
int x,y,z;
int max,min;
cout<<"Enter any number x"<<endl;
cin>>x;
cout<<"enter any number y"<<endl;
cin>>y;
cout<<"enter eny number z"<<endl;
cin>>z;

if(x>y && x>z)
{
max=x;
if(y>z)
min=z;
else
min=y;
}
if(y>z && y>x)
{
max=y;
if(z>x)
min=x;
else
min=z;

}
if(z>x && z>y)
{
max=z;
if(x>y)
min=y;
else
min=x;

}
cout<<"the maximun number is"<<max<<endl;
cout<<"the minimun number is"<<min<<endl;

return 0;
}







2017-10-04
mamun comment

 mamun

#include <stdio.h>
int main()
{
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}


2017-07-29
vahid comment

 vahid

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main(){
string a,b,c;
string Max,Min;

cout << "Enter a: "<< a <<flush;
cin >> a;

cout << "Enter b: "<< b <<flush;
cin >> b;

cout << "Enter c: "<< c <<flush;
cin >> c;


if(a>b){
Max=a;
Min=b;
} else{
Max=b;
Min=a;
}

if(a>c){
Max=a;
Min=c;
} else{
Max=c;
Min=a;
}
if(b>c){
Max=b;
Min=c;
} else{
Max=c;
Min=b;
}

cout << "Maximum value is: "<< Max <<endl;
cout << "Minimum value is: "<< Min <<endl;
cout << "size of string: "<< sizeof(string) <<endl;

return 0;
}


2017-04-30
Eng.Mirna Magdy comment

 Eng.Mirna Magdy

#include <iostream>

using namespace std;

int main()
{
double quiz,mid_term,fina;
cout<<"Plz Enter The Quiz degree for the Student : ";
cin>>quiz;
cout<<"\n";

cout<<"Plz Enter The Mid_Term degree for the Student : ";
cin>>mid_term;
cout<<"\n";

cout<<"Plz Enter The Final degree for the Student : ";
cin>>fina;
cout<<"\n";
double avg =(quiz+mid_term+fina)/3;
cout<<"The Average Grade for This Student = "<<avg;
cout<<"\n";

if(avg == 90){
cout<<"The Grade for this Student is A "<<endl;
}
else if (avg >=70 && avg < 90)
{
cout<<"The Grade For this Student is B"<<endl;
}

else if (avg >=50 && avg < 70)
{
cout<<"The Grade For this Student is C"<<endl;
}
else if (avg == 50)
{
cout<<"The Grade For this Student is F"<<endl;

}
else{
cout<<"Invalid Input "<<endl;
}

return 0;
}


2017-02-26
sozai comment

 sozai

can anyone help me ?? write a c++ program based on this
HAPPY Holiday Tour Agency will give a special rate for a holiday in Langkawi Island, Pangkor Island,
and Tioman Island during the school holiday. Full cost for 3 days for an adult are as follows:
Destination Flight(2 ways ) Hotel(3 days)
Langkawi RM 120.00 RM 260.00
Pangkor RM 90.00 RM 330.00
Tioman RM 150.00 RM 390.00
For a family to go for a vacation, discount is given based on the following:
Bed Room ( Hotel )
1 adult - no discount given
2 adults - 20% discount each adult
3 or more adults - 30% discount each adult
children - 50% discount each children
Flight
No discount for adult
50% discount for each child
Design the above problem using the problem solving tools mentioned below - that will report the total
cost needed by a family to go for this holiday package. Assume that your inputs are destination, total
adults, and total children.


2017-02-13
sozai comment

 sozai

can anyone help me ?? write a c++ program based on this
HAPPY Holiday Tour Agency will give a special rate for a holiday in Langkawi Island, Pangkor Island,
and Tioman Island during the school holiday. Full cost for 3 days for an adult are as follows:
Destination Flight(2 ways ) Hotel(3 days)
Langkawi RM 120.00 RM 260.00
Pangkor RM 90.00 RM 330.00
Tioman RM 150.00 RM 390.00
For a family to go for a vacation, discount is given based on the following:
Bed Room ( Hotel )
1 adult - no discount given
2 adults - 20% discount each adult
3 or more adults - 30% discount each adult
children - 50% discount each children
Flight
No discount for adult
50% discount for each child
Design the above problem using the problem solving tools mentioned below - that will report the total
cost needed by a family to go for this holiday package. Assume that your inputs are destination, total
adults, and total children.


2017-02-13
imran israr comment

 imran israr

/* Write a program that determines a student’s grade.
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 */
#include <iostream>
using namespace std;
int main()
{
char A,B,C,D,F;
int grade;
cout<<"please enter your persentage:"<<"\t"<<"\n";
cin>>grade;
if(grade>=90)
{
cout<<"The grade is:"<<"\t"<<"A"<<"\n";
}
else if(grade<90 && grade>=70)
{
cout<<"The grade is:"<<"\t"<<"B"<<"\n";
}
else if(grade<70 && grade>=50)
{
cout<<"The grade is:"<<"\t"<<"C"<<"\n";
}
else if(grade<50)
{
cout<<"The grade is:"<<"\t"<<"F"<<"\n";
}
system("pause");
}


2017-01-17
imran israr comment

 imran israr

/*Write a C++ program that prompts the user to input three integer values and find the greatest value of the three values.
Enter 3 integer vales separated by space: 10 15 20 */
#include <iostream>
using namespace std;
int main()
{
int x,y,z;
cout<<"please enter the value of x:"<<"\t"<<"\n";
cin>>x;
cout<<"please enter the value of y:"<<"\t"<<"\n";
cin>>y;
cout<<"please enter the value of z:"<<"\t"<<"\n";
cin>>z;
if(x>y && x>z)
{
cout<<"x is greater than:"<<x<<"\n";
}
else if(y>x && y>z)
{
cout<<"y is greater than:"<<y<<"\n";
}
else if(z>x && z>y)
{
cout<<"z is greater than :"<<z<<"\n";
}
system("pause");
}


2017-01-17
Taram Nayab comment

 Taram Nayab

IN SIMPLE AND EASY FROM:
#include <iostream>

using namespace std;
int main()
{
float quiz,mid,finals,avg;
cout<<"Enter your marks\n";
cout<<"Quiz:";cin>>quiz;
cout<<"Mids:";cin>>mid;
cout<<"Finals:";cin>>finals;
avg=(quiz+mid+finals)/3;
cout<<"Average marks :"<<avg;
if(avg>=90)cout<<"\nGrade A";
else if((avg>=70) && (avg<90)) cout<<"\nGrade B";
else if((avg>=50) && (avg<70))cout<<"\nGrade C";
else if(avg<50) cout<<"FAIL";
else cout<<"Invalid";

return 0;



}


2016-12-26
Taram Nayab comment

 Taram Nayab

IN SIMPLE AND EASY FROM:
#include <iostream>

using namespace std;
int main()
{
float quiz,mid,finals,avg;
cout<<"Enter your marks\n";
cout<<"Quiz:";cin>>quiz;
cout<<"Mids:";cin>>mid;
cout<<"Finals:";cin>>finals;
avg=(quiz+mid+finals)/3;
cout<<"Average marks :"<<avg;
if(avg>=90)cout<<"\nGrade A";
else if((avg>=70) && (avg<90)) cout<<"\nGrade B";
else if((avg>=50) && (avg<70))cout<<"\nGrade C";
else if(avg<50) cout<<"FAIL";
else cout<<"Invalid";

return 0;



}


2016-12-26
Taram Nayab comment

 Taram Nayab

using namespace std;
int main()
{
float quiz,mid,finals,avg;
cout<<"Enter your marks\n";
cout<<"Quiz:";cin>>quiz;
cout<<"Mids:";cin>>mid;
cout<<"Finals:";cin>>finals;
avg=(quiz+mid+finals)/3;
cout<<"Average marks :\n"<<avg;
if(avg>=90)cout<<"\nGrade A";
else if((avg>=70) && (avg<90)) cout<<"\nGrade B";
else if((avg>=50) && (avg<70))cout<<"\nGrade C";
else if(avg<50) cout<<"FAIL";
else cout<<"Invalid";

return 0;



}


2016-12-26
iamme comment

 iamme

#include "stdafx.h"
#include <iostream>

using namespace std;

void menu()
{
cout << "============================================================================="<<"\n";
cout << "\t\t" << "This Program Help You To Calculate Your Grade !!" << "\t\t"<<"\n";
cout << "============================================================================="<<"\n";
}

void calculation(float marks)
{
if (marks >= 90)
cout << "Your Grade is >> A ";
else if (marks <= 89 && marks >= 70)
cout << "Your Gradde is >> B";
else if (marks <= 79 && marks >= 50)
cout << "Your Gradde is >> C";
else
cout << "Your Gradde is >> F";
}


int main()
{
float x, x1,y, z, tt;
char conf;
do
{
system("cls");
menu();
do
{
cout << "Enter Your Quiz Marks: ";
cin >> x;
} while (x > 100 || x < 0);
do
{
cout << "Enter Your Mid-Term Makrs: ";
cin >> y;
} while (y>100 || y < 0);
do
{
cout << "Enter Your Final Exam Marks:";
cin >> z;
} while (z>100 || y < 0);

tt = (x + y + z) / 3;

calculation(tt);
cout << "\n";
cout << "Do You Still Want To Continue?(Y/N)";
cin >> conf;
} while (conf == 'Y' || conf == 'y');

return 0;
system("pause");
}


2016-11-24
iamme comment

 iamme

#include "stdafx.h"
#include <iostream>

using namespace std;

void menu()
{
cout << "============================================================================="<<"\n";
cout << "\t\t" << "This Program Help You To Calculate Your Grade !!" << "\t\t"<<"\n";
cout << "============================================================================="<<"\n";
}

void calculation(float marks)
{
if (marks >= 90)
cout << "Your Grade is >> A ";
else if (marks <= 89 && marks >= 70)
cout << "Your Gradde is >> B";
else if (marks <= 79 && marks >= 50)
cout << "Your Gradde is >> C";
else
cout << "Your Gradde is >> F";
}


int main()
{
float x, x1,y, z, tt;
char conf;
do
{
system("cls");
menu();
do
{
cout << "Enter Your Quiz Marks: ";
cin >> x;
} while (x > 100 || x < 0);
do
{
cout << "Enter Your Mid-Term Makrs: ";
cin >> y;
} while (y>100 || y < 0);
do
{
cout << "Enter Your Final Exam Marks:";
cin >> z;
} while (z>100 || y < 0);

tt = (x + y + z) / 3;

calculation(tt);
cout << "\n";
cout << "Do You Still Want To Continue?(Y/N)";
cin >> conf;
} while (conf == 'Y' || conf == 'y');

return 0;
system("pause");
}


2016-11-24
Kapil comment

 Kapil

I need a help to solve c++ programs


2016-10-07
nafees comment

 nafees

#include<iostream>
#include<cstdlib>
//#include<iomanip>
using namespace std;
int main()
{
double quiz;
double mid;
double final;
cout<<"enter obtained marks in quizzes: ";
cin>>quiz;
cout<<"\n";
cout<<"enter obtained marks in midterm: ";
cin>>mid;
cout<<"\n";
cout<<"enter obtained marks in finalterm: ";
cin>>final;
double total=(quiz+mid+final)/3;
cout<<"\n";
if(total>=90)
{
cout<<"the student got grade A"<<endl;
}
else if(total>=70)
{
cout<<"the student got grade B"<<endl;
}
else if(total>=50)
{
cout<<"the student got grade C"<<endl;
}
else
{
cout<<"the student got grade F"<<endl;
}

}



2016-09-01
bilal mughal comment

 bilal mughal

#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
int n1,n2,n3;


cout<<"ehter the first number\n";
cin>>n1;
cout<<"enter the second number\n";
cin>>n2;
cout<<"enter the third number\n";
cin>>n3;

cout<<"you entered first number is:"<<n1;
cout<<"\nyou entered second number is:"<<n2;
cout<<"\nyou entered third number is:"<<n3;

if(n1>n2)
{
if(n1>n3)
{
cout<<"\n";
cout<< n1<<" is greater number";
}
else
{
cout<< n3<<" is greater number";

}

}
else if(n2>n1)
{
if(n2>n3)
{
cout<<"\n";
cout<<n2<<" is greater number";

}
else
{
cout<<"\n";
cout<<n3<<" is greater number";

}

}
else if (n3>n1)
{
if(n3>n2)
{
cout<<"\n";
cout<<n3<<" is greater number";
}
else
{
cout<<"\n";
cout<<n2<<" is greater number";

}
}
else
{
cout<<"\nall numbers are equal";

}

getch();
return 0;



}


2016-06-12
bilal mughal comment

 bilal mughal

#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
int n1,n2,n3;


cout<<"ehter the first number\n";
cin>>n1;
cout<<"enter the second number\n";
cin>>n2;
cout<<"enter the third number\n";
cin>>n3;

cout<<"you entered first number is:"<<n1;
cout<<"\nyou entered second number is:"<<n2;
cout<<"\nyou entered third number is:"<<n3;

if(n1>n2)
{
if(n1>n3)
{
cout<<"\n";
cout<< n1<<" is greater number";
}
else
{
cout<< n3<<" is greater number";

}

}
else if(n2>n1)
{
if(n2>n3)
{
cout<<"\n";
cout<<n2<<" is greater number";

}
else
{
cout<<"\n";
cout<<n3<<" is greater number";

}

}
else if (n3>n1)
{
if(n3>n2)
{
cout<<"\n";
cout<<n3<<" is greater number";
}
else
{
cout<<"\n";
cout<<n2<<" is greater number";

}
}
else
{
cout<<"\nall numbers are equal";

}

getch();
return 0;



}


2016-06-12
Bruce comment

 Bruce

#include <iostream>

using namespace std;

int main()

{

double Quiz(0), Midterm(0), Finalterm(0);
double Avg(0);

cout << "Please enter Quiz, Midterm and Final Term values" << endl;

cin >>Quiz >> Midterm >> Finalterm;
Avg= (Quiz+Midterm+Finalterm)/3;

if (Avg>= 90)
cout << "Your Score is "<< Avg <<" ---> Grade A" << endl;

if (Avg>=70 && Avg<90)
cout << "Your Score is "<< Avg <<" ---> Grade B" << endl;

if (Avg>=50 && Avg<70)
cout << "Your Score is "<< Avg <<" ---> Grade C" << endl;

if (Avg< 50)
cout << "Your Score is "<< Avg <<" ---> Grade F" << endl;



}


2016-02-28
Julius comment

 Julius

here is another solution to question 1, with a while loop.


#include <iostream>

using namespace std;

int main(int argc, const char * argv[]) {
char answer = 'c';
while (answer == 'c') {


int firstNumber, secondNumber, thirdNumber;
cin >> firstNumber;
cin.get();
cin >> secondNumber;
cin.get();
cin >> thirdNumber;
cin.get();



if (firstNumber == secondNumber || secondNumber == thirdNumber || firstNumber == thirdNumber) {
cout << "wrong number";
} else if (firstNumber > secondNumber && firstNumber > thirdNumber) {
cout << firstNumber;
} else if (secondNumber > firstNumber && secondNumber > thirdNumber) {
cout << secondNumber;
} else if (thirdNumber > firstNumber && secondNumber > firstNumber) {
cout << thirdNumber;
}
cout << endl;
cout << "C to continue, Q to quit" << endl;
cin >> answer;

cin.get();
}
return 0;
}


2016-01-07
Qadir Khan comment

 Qadir Khan

nice learning process for beginners.


2015-02-20
jeje comment

 jeje

great tutorial


2014-05-03



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.