﻿ 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;
} zanibwrite a program for switch statment & find vowel or inconent?2017-12-21 LKExample : 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 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< 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 && y>z) cout<<"The large number is = "<>x; cout<<"enter any number y"<>y; cout<<"enter eny number z"<>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"< int main() { // printf() displays the string inside quotation printf("Hello, World!"); return 0; } 2017-07-29 vahid#include #include #include using namespace std; int main(){ string a,b,c; string Max,Min; cout << "Enter a: "<< a <> a; cout << "Enter b: "<< b <> b; cout << "Enter c: "<< c <> 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 < 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 = "<=70 && avg < 90) { cout<<"The Grade For this Student is B"<=50 && avg < 70) { cout<<"The Grade For this Student is C"<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 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/*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 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 && y>z) { cout<<"y is greater than:"<x && z>y) { cout<<"z is greater than :"< 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 :"<=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 NayabIN SIMPLE AND EASY FROM: #include 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 :"<=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 Nayabusing 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"<=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#include "stdafx.h" #include 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#include "stdafx.h" #include 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 KapilI need a help to solve c++ programs2016-10-07 nafees#include #include //#include 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"<=70) { cout<<"the student got grade B"<=50) { cout<<"the student got grade C"< #include 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:"<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<n1) { if(n3>n2) { cout<<"\n"; cout< #include 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:"<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<n1) { if(n3>n2) { cout<<"\n"; cout< 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 Juliushere is another solution to question 1, with a while loop. #include 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 Khannice learning process for beginners.2015-02-20 jejegreat tutorial2014-05-03