C ++ exercises about IF ELSE

In the previous C ++ exercise, we introduced you to read C ++ exercises about variables and data types. This time, there are 8 C ++ exercises about IF ELSE for you to practice and proficiently use if, else in C ++, please watch it.

In the previous C ++ exercise, we introduced you to read C ++ exercises about variables and data types. This time, there are 8 C ++ exercises about IF ELSE for you to practice and proficiently use if, else in C ++, please watch it.

C ++ exercises about IF ELSE 1

Write a C ++ program to enter employee salaries, calculate income taxes and net wages (the actual salary that the employee receives). With the following assumptions (not according to the salary law, only a hypothetical number for easy calculation):

  1. 30% income tax if the salary is 15 million.
  2. 20% income tax if the salary is between 7 and 15 million.
  3. 10% income tax if salary is below 7 million.

Sample code:

 #include 
#include
using namespace std;

int main ()
{

int thuesuat;
float luong, sothue, luongrong = 0;
cout << "Enter salary amount:";
cin >> luong;
if (luong> 15)
{
sothue = luong * 0.3;
thuesuat = 30;
}
else if (length> = 7)
{
sothue = luong * 0.2;
thuesuat = 20;
}
else
{
sothue = luong * 0.1;
thuesuat = 10;
}
luongrong = luong-sothue;
cout << "Salary =" << luong << endl;
cout << "Income tax" << thuesuat << "% =" << sothue << endl;
cout << "Real wages =" << in << endl;
return 0;
}

Results when running the above code:

Picture 1 of C ++ exercises about IF ELSE

C ++ exercises about IF ELSE 2

Write a C ++ program to enter age and print out the results if the age of the student is not eligible for 10th grade. Know the student's age of entering the 10th grade is 16.

Sample C ++ code:

 #include 
#include
using namespace std;
int main ()
{

int tuoi;
cout << "Enter student age:" << endl;
cin >> tuoi;
if (tuoi == 16)
{
cout << "Student's age is:" << tuoi << "tuoi." << endl;
cout << "Students are old enough to enter 10th grade!" << endl;
}
else
{
cout << "Student's age is:" << tuoi << "tuoi." << endl;
cout << "Students are not old enough to enter 10th grade!" << endl;
}
return 0;
}

Output result:

Picture 2 of C ++ exercises about IF ELSE

C ++ exercises about IF ELSE 3

Write a C ++ program to enter any integer from the keyboard and print the results to the screen to notify the user of whether the number is greater or less than 100.

Sample C ++ code:

 #include 
#include
using namespace std;

int main () {
int a;
cout << "Enter integer:" << endl;
cin >> a;
if (a> 100)
{
cout << a << "greater than 100." << endl;
}
else
{
cout << a << "less than 100." << endl;
}
return 0;
}

Output result:

Picture 3 of C ++ exercises about IF ELSE

C ++ exercises about IF ELSE 4

Write a C ++ program for users to enter 3 integers and find the largest number of those 3 numbers.

Sample C ++ code:

 #include 
#include
#include

using namespace std;

int main (int argc, char * argv [])
{
cout << "Enter any 3 numbers:";
int a1, a2, a3, max;
cin >> a1 >> a2 >> a3;

max = a1; // Assume the largest first number

if (max
if (max
cout << "The maximum number of 3 numbers is:" <

return 0;
}

Output result:

Picture 4 of C ++ exercises about IF ELSE

C ++ exercises about IF ELSE 5

Write a C ++ program that ranks students' performance based on midterm test scores and test scores and final exam scores. If:

  1. Average score> = 9.0 is grade A.
  2. Average score> = 7.0 and
  3. Average score> = 5.0 and
  4. Medium score

Sample C ++ code:

 #include 
#include
#include

using namespace std;

int main (int argc, char * argv [])
{
float a;
float b;
float c;
float dtb;
cout << "Enter test scores, midterm, final grades corresponding:";
cin >> a >> b >> c;
dtb = (a + b + c) / 3;
cout << "Average score is:" <

if (dtb> = 9.0) cout << "Grade A";
else if ((dtb> = 7.0) && (dtb <9.0)) cout << "Class B";
else if ((dtb> = 5.0) && (dtb <7.0)) cout << "Class C";
else if (dtb <5.0) cout << "Class F";
else cout << "Invalid score";

cout << "n";
cout << "Award by TipsMake.com";
return 0;
}

Output result:

Picture 5 of C ++ exercises about IF ELSE

C ++ exercises about IF ELSE 6

Write a C ++ program to find the solution of the quadratic equation ax2 + bx + c = 0. Know that:

  1. If a and b are equal to 0, then the equation is null.
  2. If a = 0, the equation has one solution is (-c / b).
  3. If b2-4ac <0, then the equation is null.
  4. Otherwise, the equation has two solutions, using the experimental formula to calculate.

Sample code:

 #include 
#include
#include
using namespace std;

int main ()
{

float a, b, c, d, x1, x2;
cout << "Enter the coefficient a, b, c of the corresponding equation of the second order:" << endl;
cin >> a >> b >> c;
if (! a) {
if (! b)
cout << "Both a and b cannot be equal to 0 in equation ax ^ 2 + bx + c = 0" << "n";
else
{
d = -c / b;
cout << "The equation has a unique solution:" << d << endl;
}
}
else
{
d = b * b-4 * a * c;
if (d> 0)
x1 = (- b + sqrt (d)) / (2 * a);
x2 = (- b-sqrt (d)) / (2 * a);
cout << "First solution x1 =" << x1 << endl;
cout << "Second solution x2 =" << x2 << endl;
}
cout << "Award by TipsMake.com";
return 0;
}

Output result:

Picture 6 of C ++ exercises about IF ELSE

C ++ exercises about IF ELSE 7

Your store accepts to sell products to another company and earn commissions, with commissions according to sales volume as follows:

  1. 5% if total sales are less than or equal to 100 million.
  2. 10% if total sales are less than or equal to 300 million.
  3. 20% if total sales are greater than 300 million.

Write a C ++ program to calculate the commission you will receive based on sales.

Sample C ++ code:

 #include 
#include
using namespace std;
int main ()
{

long int doanhso;
hoahong float;
cout << "Total sales:" << endl;
cin >> doanhso;
if (doanhso <= 100)
{
hoahong = doanhso * 5/100;
cout << "With total sales is" << sales << ",";
cout << "the commission received is" << hoahong;
}
else if (doanhso <= 300)
{
hoahong = doanhso * 10/100;
cout << "With total sales is" << sales << ",";
cout << "the commission received is" << hoahong;
}
else if (doanhso> 300)
{
hoahong = doanhso * 20/100;
cout << "With total sales is" << sales << ",";
cout << "the commission received is" << hoahong;
}
cout << "n";
cout << "Award by TipsMake.com";
return 0;
}

Output result:

Picture 7 of C ++ exercises about IF ELSE

C ++ exercises about IF ELSE 8

Write a C ++ program to calculate the phone bill for a household with the following parameters:

  1. Required subscription fee is 25 thousand.
  2. VND 600 for every minute of the first 50 minutes.
  3. VND 400 for every minute of the next 150 minutes.
  4. VND 200 for any minute call after the first 200 minutes.

Sample C ++ code:

 #include 
#include
using namespace std;
int main ()
{

long int sophut, phi = 0;
float tong;
const int phicodinh = 25000;
cout << "Number of minutes to call in the month:";
cin >> sophut;
if (sophut> 200)
phi = (sophut-200) * 200 + 150 * 400 + 50 * 600;
else if (sophut> 50)
phi = (sophut-50) * 400 + 50 * 600;
else
phi = sophut * 600;
tong = phi + phicodinh;
cout << "You called" << sophut << "minutes." << endl;
cout << "Phone amount to be paid is << << endl;
cout << "Award by TipsMake.com";

return 0;
}

Output result:

 

Picture 8 of C ++ exercises about IF ELSE

If you are learning Python and Java programming, don't ignore the Python exercises and Java exercises.

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile