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.
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):
- 30% income tax if the salary is 15 million.
- 20% income tax if the salary is between 7 and 15 million.
- 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:
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:
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:
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:
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:
- Average score> = 9.0 is grade A.
- Average score> = 7.0 and
- Average score> = 5.0 and
- 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:
C ++ exercises about IF ELSE 6
Write a C ++ program to find the solution of the quadratic equation ax2 + bx + c = 0. Know that:
- If a and b are equal to 0, then the equation is null.
- If a = 0, the equation has one solution is (-c / b).
- If b2-4ac <0, then the equation is null.
- 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:
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:
- 5% if total sales are less than or equal to 100 million.
- 10% if total sales are less than or equal to 300 million.
- 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:
C ++ exercises about IF ELSE 8
Write a C ++ program to calculate the phone bill for a household with the following parameters:
- Required subscription fee is 25 thousand.
- VND 600 for every minute of the first 50 minutes.
- VND 400 for every minute of the next 150 minutes.
- 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:
If you are learning Python and Java programming, don't ignore the Python exercises and Java exercises.
You should read it
- Set of multiple choice questions for programming with prizes
- Please download VnDoc's Exercises app to make learning easier
- Basic Java exercises, with sample decoding
- Summary of JavaScript exercises with sample code
- Set of multiple choice questions for programming with P15 prize
- Set of multiple choice questions about programming with P12
- Set of multiple-choice questions on award-winning programming P5
- Set of multiple choice questions about programming with P6
May be interested
- Summary of JavaScript exercises with sample codein order to make your javascript learning easier, tipsmake.com has compiled a number of javascript exercises with sample solutions for you to practice.
- Some simple stress relief and eye relaxation exercisessitting in front of the computer for a long time will not only affect the spine and nervous system but the eyes will also affect quite a lot. instead of sitting for hours in front of the computer, why don't you try to relax and relieve your stress with your exercises ...
- Effective shoulder pain treatment for office workersthese are effective shoulder pain remedies that office people should know and apply.
- Daily stretching exercises help you sleep well and feel refreshed in the morningstretching before bed can have a huge impact on your health. these simple stretches can help reduce stress, improve sleep, and improve mobility in just a few minutes.
- Effective exercises to remove wrinkles and rejuvenate the skinthe guide below will introduce 5 effective facial exercises designed to tone muscles, reduce visible signs of aging, and improve the overall appearance of your skin.
- 9 practice exercises to become a professional network administratorthe series of 9 exercises allotted to the 3 basic, intermediate and advanced levels mentioned below will cover different network topics, from basic to advanced.
- Google released Socratic application, supporting students to solve exercises through photosfor instructions, you just need to take an exercise photo with socratic's camera, the application will find the most relevant solution with each step so you can understand the core of the problem.
- Basic Java exercises, with sample decodingto serve your java learning needs, tipsmake.com has synthesized some java exercises from many sources, including sample code (for some articles). hopefully it can be helpful to learn your java programming language.
- Summary of Excel exercises from basic to advancedbelow summarizes the excel exercises from basic to advanced, you can refer to the solution to know how to use functions and how to apply functions to each specific problem. you will improve your excel skills with excel exercises from beginner to n
- 7 healthy spine protection exercises for youplease refer to the 7 healthy spine protection exercises for you below!