In this Post we will do some more if else Practice Programs in C++. To Check Part 1 of if else statements practice in C++ Visit Part 1
Video
Task 3
Write a program that takes 3 numbers a,b,c as input from the user and if a is not equal to zero then check if a is the common divisor of b and c or not.
Common Divisor: means b and c both are divisible by a
Code
#include<iostream>
using namespace std;
int main()
{
//input three variables a,b,c;
int a,b,c;
cout<<"Enter Three Numbers : \n";
cin>>a>>b>>c;
//conditions to check if a is zero or not
if(a==0)
{
cout<<"a is zero ";
}
else
{
//if a is not zero we have to check if b and c are divisibe by a or not
//for that purpose once again conditions
if(b%a==0 && c%a==0)
//if a number is divisible by other then it means it's
//remainder is zero % operator returns the remainder
cout<<"\na is common divisor of b and c";
else
cout<<"\na is not common divisor of b and c";
}
}
Output
Enter Three Numbers :
4
16
56
a is common divisor of b and c
Task 4
Write a program that ask the user total marks and obtained marks.calculate the percentage and then display the grade according to this.
if obt_m greater then ttl_m invalid
else if p greater then 80 A
else if p greater then 70 B
else if greater then 60 C
else if greater then 55 D
else if greater then 50 E
else if greater then 50 F
Code
#include<iostream>
using namespace std;
int main()
{
//variables required
float obt_m , ttl_m,perc;
char grade;
//input total marks and obtained marks
cout<<"Enter Total Marks : ";
cin>>ttl_m;
cout<<"\nEnter Obtained Marks : ";
cin>>obt_m;
//calculating percentage
perc=(obt_m/ttl_m)*100;
//checking conditions..
if(obt_m>ttl_m)
{ cout<<"\nInvalid Input "; return 0;}
else if(perc>=80)
grade='A';
else if(perc>=70)
grade = 'B';
else if(perc>=60)
grade='C';
else if(perc>=55)
grade = 'D';
else if(perc>=50)
grade='E';
else if(perc<50)
grade = 'F';
//printing grade
cout<<"\nYour Grade is : "<<grade;
cout<<"\nYour Percentage is : "<<perc;
}
Output
Enter Total Marks : 1100
Enter Obtained Marks : 904
Your Grade is : A
Your Percentage is : 82.1818