C++ basic programs

Programming in C++ we will start from zero level, Here you will found following basic programs,

Let’s starting Programming in C++

printing of Two number Total in C++

``````<pre">#include<iostream>
using namespace std;

int main(void) // because function void type so void as argument in function
{
int num1,num2,sum; // three variable declare
cout<<"Enter any two number: ";
cin>>num1>>num2;

sum = num1+num2; // variable sum hold the total of variable a and b total

cout<<"Addition: "<<sum; // printing value of sum(which is total of a and b)

return 0;
}``````

OUTPUT

``````Enter any two number: 4 5

Explanation

In the program, first we have declare three int type variables,

``int num1,num2,sum;``

where variable num1 and variable num2 will take input from the user while variable sum will store the total of these two variables.

``sum = num1+num2;``

such as,

Here is another example where we add three number given by the user.

add three number of Total given by the user in C++

``````#include<iostream>
using namespace std;

int main(void)
{
int num1,num2,num3,sum;
cout<<"Enter any four number: ";
cin>>num1>>num2>>num3;

sum = num1+num2+num3;

cout<<"nTotal: "<<sum;
// cout<<"Total: "<<num1+num2+num3; // also work
return 0;
}``````

OUTPUT

``````Enter any three number: 4 3 5
Total: 12``````

Explanation

In the program, first we have declare four int type variables,

``int num1,num2,num3,sum;``

where variables num1, num2 and num3 will take input from the user while variable sum will store the total of these two variables.

``sum = num1+num2+num3;``

means,

``sum = 3+4+5``

such as,

The above both program shows that we have to declare as many variables as we need to total. Meaning if we want to total two numbers, then we have to declare two variables first and if we want to total 3 numbers, first we have to declare three variables.

multiply any three number in C++

``````#include<iostream>
using namespace std;

int main()
{
int a,b,c,mul;
cout<<"Enter any three number: ";
cin>>a>>b>>c;

mul = a*b*c; // multiply variables a,b and c. and store result variable mul

cout<<"Total: "<<mul; // print variable mul value

return 0; // using return because function main() is not void type
}``````

OUTPUT

``````Enter any three number: 4 5 3
Multiply: 60``````

to find out any three number average

``````#include<iostream.h>
#include<conio.h>

int main()
{
int a,b,c,av;
cout<<"Enter any three number: ";
cin>>a>>b>>c;

av = (a+b+c)/3; // store average of a,b and c

cout<<"nAverage: "<<av; // print variable av value
return 0;
}``````

OUTPUT

``````Enter any three number: 3 4 2
Average: 3``````

Here is another one program,

Here is another program where we declare two variable and used them with different operation,

In a way, we can say that all the tasks performed in the above program are given in a single program.

all in one find out any two number total,subtraction ,multiply and average in c++

``````#include<iostream.h>
using namespace std;

int main(void) // no value return so void as argument(optional)
{
clrscr(); // to clear previous screen
int num1,num2,sub,sum,mul,av;

cout<<"Enter any two number: "<<endl; // endl used for next line like enter button
cin>>num1>>num2;

// storing result one by one of variable a and b
sum = num1+num2;
sub = num1-num2;
mul = num1*num2;
av = sum/2;

//printing values one by one
cout<<"nSubstrac: "<<a<<"-"<<b<<" = "<<sub;
cout<<"nMultiply: "<<a<<"x"<<b<<" = "<<mul;
cout<<"nAverage : ("<<a<<"/"<<b<<")/2 = "<<av;

return 0;
}``````

OUTPUT

``````Enter any two number: 4 5
Substrac: 4-5 = -1
Multiply: 4x5 = 20
Average : (4/5)/2 = 4``````

Explanation

In the program, firstly we have declared 6 variables,

``int num1,num2,sum,mul,av;``

Where variables num1 and num2 will store user input. Suppose user input is 4 and 5 then,

``````num1 = 4
num2 = 5``````

and other variables sum,mul,av store their result.such as,

``````sum = a + b;
sub = a - b;
mul = a * b;
av = sum /2;``````

and user input is 4 and 5 then,

``````sum = 4 + 5;
sub = 4 - 5;
mul = 4 * 5;
av = 4 + 5 /2``````

after all, we print each variable’s value.

Thus the program will successfully execute.

how to store full name in C++

C++ Examples

Like it?