Thứ Tư, Tháng Ba 5, 2025
spot_img
HomeC++ exercises on IF ELSE

C++ exercises on IF ELSE

If else C++ exercises will help you practice using it fluently. Here it is C++ if else exercises.

if-else statement in C++ conditional branch control. Statements in the if branch are executed only if the condition evaluates to non-zero (or true). If the value of the condition is non-zero, the following statement will be executed and the statement following the else option will be ignored. Otherwise, the following statement will be ignored and if there is an else then the statement following the else will be executed.

The conditional expression that evaluates to non-zero is:

  • true
  • a non-null pointer,
  • any arithmetic value other than zero or
  • a class type that defines an explicit conversion to an arithmetic, boolean, or pointer type.

In any form of if statement, the condition, which can have any value except the construct, is evaluated, including all side effects. Control is transferred from the if statement to the next statement in the program unless the if-branch or else-branch being executed contains a break, continue, or goto.

The else clause of an if…else statement is linked to the nearest previous if statement in the same scope that does not have a corresponding else statement.

C++ exercises on IF ELSE 1

Write a C++ program to enter employee salary, calculate income tax and net salary (the actual amount of salary that employee receives). With the following assumed parameters (not according to salary law, just assumed numbers for ease of calculation):

  • 30% income tax if salary is 15 million.
  • 20% income tax if salary is from 7 to 15 million.
  • 10% income tax if salary is less than 7 million.

Sample code:

#include 
#include 
using namespace std;

int main()
{
	int thuesuat;
	float luong, sothue, luongrong = 0;
	cout > luong;
	if (luong > 15)
	{
		sothue = luong *0.3;
		thuesuat = 30;
	}
	else if (luong >= 7)
	{
		sothue = luong *0.2;
		thuesuat = 20;
	}
	else
	{
		sothue = luong *0.1;
		thuesuat = 10;
	}

	luongrong = luong - sothue;
	cout 

Result when running the above code:

Results of running the code in C++ 1

C++ exercises on IF ELSE 2

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

Sample C++ code:

#include 
#include 
using namespace std;
int main()
{
	int tuoi;
	cout > tuoi;
	if (tuoi == 16)
	{
		cout 

Output:

Results of running the C++ 2 code

C++ exercises on IF ELSE 3

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

Sample C++ code:

#include 
#include 
using namespace std;

int main()
{
	int a;
	cout > a;
	if (a > 100)
	{
		cout 

Output:

Results of running the C++ 3 code

C++ exercises on IF ELSE 4

Write a C++ program so that the user enters 3 integers and finds the largest number among those 3 numbers.

Sample C++ code:

#include 
#include 
#include 

using namespace std;
int main(int argc, char *argv[])
{
	cout > a1 >> a2 >> a3;
	max = a1;	//Giả sử số đầu tiên lớn nhất
	if (max 

Output:

Results of running the C++ 4 code

C++ exercises on IF ELSE 5

Write a C++ program to rank students' academic performance based on test scores, midterm exam scores, and final exam scores. If:

  • Average score >= 9.0 is grade A.
  • Average score >=7.0 and
  • Average score >=5.0 and
  • Average 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 > a >> b >> c;
	dtb = (a + b + c) / 3;
	cout = 9.0) cout = 7.0) && (dtb = 5.0) && (dtb 

Output:

Results of running the C++ 5 code

C++ exercises on 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 both equal to 0, then the equation has no solution.
  • If a=0 then the equation has one solution which is (-c/b).
  • If b2-4ac
  • If not, the equation has two solutions, use the solution calculation formula to calculate.

Sample code:

#include 
#include 
#include 
using namespace std;

int main()
{
	float a, b, c, d, x1, x2;
	cout > a >> b >> c;
	if (!a)
	{
		if (!b)
			cout  0)
			x1 = (-b + sqrt(d)) / (2 *a);
		x2 = (-b - sqrt(d)) / (2 *a);
		cout 

Output:

Results of running the C++ 6 code

C++ exercises on IF ELSE 7

Your store accepts to sell products to another company and earn commissions, with commission rates based on sales 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 is 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;
	float hoahong;
	cout > doanhso;
	if (doanhso  300)
	{
		hoahong = doanhso *20 / 100;
		cout 

Output:

Result of running C++ 7 code

C++ exercises on IF ELSE 8

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

  • The mandatory subscription fee is 25 thousand.
  • 600 VND for each calling minute for the first 50 minutes.
  • 400 VND for each calling minute for the next 150 minutes.
  • 200 VND for any call minutes 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 > 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 

Output:

Results of running the C++ 8 code

C++ exercises on IF ELSE 9

Write a program to calculate electricity bill in C++. This program will take the quantity as input from the user and the quantity rate chosen by the programmer. The program will calculate the remaining electricity bill.

Solution:

First, you need to determine how many values ​​need to be stored in a variable?

There are two different prices for each customer. The exchange rate is high for customers who consume less, and low for customers who buy at low unit prices. Therefore, we need two variables:

  • Rate1 and rate2
  • Intrate1 and inrate2 mean that these two variables contain only integer values.
  • When the user enters unit (row), we need to store the quantity in a unit variable.
  • Once the invoice total is calculated, save the invoice in a variable bill.

Sample code:

#include
using namespace std;
int main()
{
	int rate1, rate2, units, bill;
	rate1=5;
	rate2=10;
	cout>units;
	if(units

Result:

Nhập số lượng hàng khách đã mua

20

Tổng hóa đơn = 100
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments