C++ Programming Examples and Solutions

c++ program

This post describes the basic C++ program with a solution(Coding). The Common terms used in the Code of C++ which we need to know before starting the programming are as follows:

  • #include: It is a preprocessors directive that tells the system to include header files in the program.
  • using namespace std: this means that we use namespace named “std” which is a short form of standard. So, this specifies that the keywords we use are under std namespace. If this code line is not used in the program then we use the things in a namespace like “std::cout”, “std::endl”
  • Header Files: A file that contains inbuilt library functions are called as header files. There are many header files used in C++ but for following programs, we used 2 header files which are as follows :
    1. iostream – iostream stands for standard input-output stream. As the name suggests it contain input-output functions used in c++ program. These functions are as follows:
      • cout: It is used to display output on the console screen from the standard output device.
      • cin: It is used to take input from standard input device.
    2. math.h– math.h header file is used to perform a mathematical function in the program. For example sqrt() – to find the square root, pow() – to find the power of the number.
  • Angular Brackets (<>): Indicate the start & end of the header file to be included in the program.
  • int main():“int” specifies that the program will return an integer value at the end of the execution of the program and we specify this by writing “return integer number” at the end of the program. “main()” specifies that the execution of program start from here.

Some of the basic C++ programs with solutions along with approach and output are explained one by one which is as follows:

Write a program to print “My first C++ program”

Code of The Above Question:

#include <iostream>
using namespace std;
int main() {
	cout<<"My First C++ program";
	return 0;
}

Output:

My First C++ program

Description of Above Code :

  • The First line #include<iostream> is used for providing the inbuilt function cout in the program.
  • In the next line using namespace std it specifies the namespace “std” for the functions used in the program
  • Then int main() is used to start the body of the program & specifies that the program must return an integer value.
  • The ; is used to end the line.
  • The body of the program starts and end with curly brackets {}.
  • In the body,
    • Statement cout<<"My First C++ program" is used to display the output on the console i.e. My First C++ Program
    • In the last line return 0 is used to specify that the program is returning integer value.

Addition of two numbers in c++

Code of The Above Question:

#include<iostream>
using namespace std;
int main()
{ 
int a,b,add;
cout<<"enter the values of two number for addition";
cin>>a>>b;
add=a+b;
cout<<"addition of two numbers is"<<add;
return 0;
}

Output:

enter the values of two numbers for addition 5
7
addition of two numbers is 12

Description of Above Code:

  • The First line #include<iostream> is used for providing the inbuilt function cout & cin in the program.
  • In the next line using namespace std it specifies the namespace “std” for the functions used in the program.
  • Then int main() is used to start the body of the program & specifies that the program must return an integer value.
  • The ; is used to end the line.
  • The body of the program starts and end with curly brackets {}.
  • In the body,
    • It is used to enter the type of variables used int a,b,add
    • Statement cout<<"enter the values of two number for addition" is used to display the output enter the values of two number for addition on the console.
    • cin>>a>>b This is used to take input of the variables from the user.
    • add=a+b This is used to find the division.
    • This statement is used to display the addition calculated on the console cout<<"addition of two numbers is"<<add
    • In the last line return 0 is used to specify that the program is returning integer value.

Subtract two variables in c++

Code of The Above Question:

#include<iostream>
using namespace std;
int main()
{ 
int a,b,sub;
cout<<"enter the values of two number for subtraction";
cin>>a>>b;
sub=a-b;
cout<<endl;
cout<<"subtraction of two variable is"<<sub;
return 0;
}

Output:

enter the values of two numbers for subtraction 5
3
subtraction of two variable is 2

Description of Above Program:

  • The First line #include<iostream> is used for providing the inbuilt function cout & cin in the program.
  • In the next line using namespace std it specifies the namespace “std” for the functions used in the program.
  • Then int main() is used to start the body of the program & specifies that the program must return an integer value.
  • The ; is used to end the line.
  • The body of the program starts and end with curly brackets {}.
  • In the body,
    • It is used to enter the type of variables used int a,b,sub
    • Statement cout<<"enter the values of two number for subtraction" is used to display the output enter the values of two numbers for subtraction on the console.
    • cin>>a>>b This is used to take input of the variables from the user.
    • sub=a-b This is used to find the subtraction.
    • This statement is used to display the subtraction calculated on the console cout<<"subtraction of two variable is"<<sub
    • In the last line return 0 is used to specify that the program is returning integer value.

C++ program for multiplication of two numbers

Code of The Above Question:

#include<iostream>
using namespace std;
int main()
{
int a,b,mul;
cout<<"enter the values of two number for multiplication";
cin>>a>>b;
mul=a*b;
cout<<"multiplication of two numbers is"<<mul;
return 0;
}

Output:

enter the values of two numbers for multiplication 5
5
multiplication of two numbers is 25

Description of Above Code:

  • The First line #include<iostream> is used for providing the inbuilt function cout & cin in the program.
  • In the next line using namespace std it specifies the namespace “std” for the functions used in the program.
  • Then int main() is used to start the body of the program & specifies that the program must return an integer value.
  • The ; is used to end the line.
  • The body of the program starts and end with curly brackets {}.
  • In the body,
    • It is used to enter the type of variables used int a,b,mul
    • Statement cout<<"enter the values of two number for multiplication" is used to display the output enter the values of two numbers for multiplication on the console.
    • cin>>a>>b This is used to take input of the variables from the user.
    • mul=a*b This is used to find the division.
    • This statement is used to display the multiplication calculated on the console cout<<"multiplication of two numbers is"<<mul
    • In the last line return 0 is used to specify that the program is returning integer value.

C++ program for division of two numbers

Code of The Above Question:

#include<iostream>
using namespace std;
int main()
{ 
float div;
int a,b;
cout<<"enter the values of two number for division";
cin>>a>>b;
div=a/b;
cout<<"division of two numbers is"<<div;
return 0;
}

Output:

enter the values of two numbers for division 4
2
division of two numbers is 2

Description of Above Code:

  • The First line #include<iostream> is used for providing the inbuilt function cout & cin in the program.
  • In the next line using namespace std it specifies the namespace “std” for the functions used in the program.
  • Then int main() is used to start the body of the program & specifies that the program must return an integer value.
  • The ; is used to end the line.
  • The body of the program starts and end with curly brackets {}.
  • In the body,
    • It is used to enter the type of variables used float div & int a,b
    • Statement cout<<"enter the values of two number for division" is used to display the output enter the values of two numbers for division on the console.
    • cin>>a>>b This is used to take input of the variables from the user.
    • div=a/b This is used to find the division.
    • This statement is used to display the division calculated on the console cout<<"division of two numbers is"<<div
    • In the last line return 0 is used to specify that the program is returning integer value.

C++ program for an average of 5 numbers

Code of The Above Question:

#include <iostream>
using namespace std;
int main()
{
float a,b,c,d,e,avg;
cout<<"enter the five numbers to find average";
cin>>a>>b>>c>>d>>e; 
avg=(a+b+c+d+e)/5;
cout<<"Average of five number is"<<avg;
return 0;
}

Output:

enter the five numbers to find average 4
5
6
7
8
Average of five number is 6

Description of Above Code:

  • The First line #include<iostream> is used for providing the inbuilt function cout & cin in the program.
  • In the next line using namespace std it specifies the namespace “std” for the functions used in the program.
  • Then int main() is used to start the body of the program & specifies that the program must return an integer value.
  • The ; is used to end the line.
  • The body of the program starts and end with curly brackets {}.
  • In the body,
    • It is used to enter the type of variables used float a,b,c,d,e,avg
    • Statement cout<<"enter the five numbers to find average"is used to display the output enter the five numbers to find average on the console.
    • cin>>a>>b>>c>>d>>e This is used to take input of the variables from the user.
    • avg=(a+b+c+d+e)/5 This is used to find the average of five numbers entered by the user.
    • This statement is used to display the average of five numbers calculated on the console cout<<"Average of five number is"<<avg
    • In the last line return 0 is used to specify that the program is returning integer value.

C++ program to calculate simple interest and amount

Code of the Above Question:

#include<iostream>
using namespace std;
int main()
{
float p,r,t,si,amount;
cout<<"enter the values principle,rate,interest";
cin>>p>>r>>t;
si=(p*r*t)/100;
cout<<"simple interest is"<<si;
amount=si+p;
cout<<"amount is"<<amount;
return 0;
}

Output:

enter the values principle,rate,interest 10
10
10
simple interest is 10
amount is 20

Description of Above Code:

  • The First line #include<iostream> is used for providing the inbuilt function cout & cin in the program.
  • In the next line using namespace std it specifies the namespace “std” for the functions used in the program.
  • Then int main() is used to start the body of the program & specifies that the program must return an integer value.
  • The ; is used to end the line.
  • The body of the program starts and end with curly brackets {}.
  • In the body,
    • It is used to enter the type of variables used float p,r,t,si,amount
    • Statement cout<<"enter the values principle,rate,interest" is used to display the output enter the values principle, rate, interest on the console.
    • cin>>p>>r>>t This is used to take input of the variables from the user.
    • si=(p*r*t)/100 This is used to find the simple interest.
    • This statement is used to display the simple interest calculated on the console cout<<"simple interest is"<<si
    • amount=si+p This is used to find the amount.
    • This statement is used to display the amount calculated on the console cout<<"amount is"<<amount
    • In the last line return 0 is used to specify that the program is returning integer value.

C++ program to calculate compound interest

Code of The Above Question:

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float p,r,t,ci;
cout<<"enter the principal, rate and time for compound interest";
cin>>p>>r>>t;
ci=p*pow((1+(r/100)),t);
cout<<"compound interest is"<<ci;
return 0;
}

Output:

enter the principal, rate and time for compound interest 5
100
2
compound interest is 20

Description of Above Code:

  • The First line #include<iostream> is used for providing the inbuilt function cout & cin in the program.
  • And, #include <math.h> is used to provide inbuilt function “pow” in the program
  • In the next line using namespace std it specifies the namespace “std” for the functions used in the program.
  • Then int main() is used to start the body of the program & specifies that the program must return an integer value.
  • The ; is used to end the line.
  • The body of the program starts and end with curly brackets {}.
  • In the body,
    • It is used to enter the type of variables used float p,r,t,ci
    • Statement cout<<"enter the principal, rate and time for compound interest" is used to display the output enter the principal, rate and time for compound interest on the console.
    • cin>>p>>r>>t This is used to take input of the variables from the user.
    • ci=p*pow((1+(r/100)),t) This is used to find the compound interest.
    • This statement is used to display the compound interest calculated on the console cout<<"compound interest is"<<ci
    • In the last line return 0 is used to specify that the program is returning integer value.


Discover more from easytechnotes

Subscribe to get the latest posts sent to your email.

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top