Programming in C++

Start programming with zero level, Here you will found following basic programs,

Printing of two number total

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

void 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)

 getch();
}
OUTPUT:-
Enter any two number: 4 5
Addition: 9

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,

add-two-number

But, if we want to add three numbers instead of two.

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

a

add three number of total given by the user

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

void 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
 
 getch();
}
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,

add-three-number-in-cpp

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

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

 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

 getch();

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

 getch();
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

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

 void 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<<"\nAddition: "<<a<<"+"<<b<<" = "<<sum;
   cout<<"\nSubstrac: "<<a<<"-"<<b<<" = "<<sub;
   cout<<"\nMultiply: "<<a<<"x"<<b<<" = "<<mul;
   cout<<"\nAverage : ("<<a<<"/"<<b<<")/2 = "<<av;

 getch();
}
OUTPUT:-
Enter any two number: 4 5

Addition: 4+5 = 9
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 variables value.

Thus the program will successfully execute.


Related Program

store a full length of name in C++