# Techtopz

(@techtopz)

Techtopz is a blog on programming and tech stuff.

# C++ | Sum of numbers

First program will directly calculate the sum which is not flexible. So the second program is introduced which will let you enter the numbers from keyboard and calculate the sum of it. The later programs are modified forms of second program and lets you add more variables to the program and lets you calculate sum of more numbers. The same way you can extend your program. Go enjoy the program. Lets begin……

First program

``````#include<iostream.h>
#include<conio.h>
void main()
{
//clears the screen.
clrscr();
//declare variable type int
int a,b,c;
//The two numbers are declared which makes program non-flexible.
a=12;
b=54;
//calculates sum and save them in 'c'
c=a+b;
//show the output c
cout<<"Sum of two numbers are  "<<c;
//get character
getch();
}
``````

output will be
Sum of two numbers are 66.

Second program

``````#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int a,b,c;
//Input the two numbers and save them in 'a' and 'b'
cout<<"Enter the first no."<<endl;
cin>>a;
cout<<"Enter the second no."<<endl;
cin>>b;
//calculate sum and save them in 'c'
c=a+b;
//show the output c
cout<<"Sum of two numbers are "<<c;
//get character
getch();
}
``````

output will be

Enter the first no.
12
Enter the second no,
54
Sum of two numbers are 66.

Third program

``````#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int a,b,c,d;
//Input the three numbers and save them in 'a' and 'b' and 'c'.
cout<<"Enter the first no."<<endl;
cin>>a;
cout<<"Enter the second no."<<endl;
cin>>b;
cout<<"Enter the third no."<<endl;
cin>>c;
//calculate sum and save them in 'd'
d=a+b+c;
//show the output d
cout<<"Sum of three numbers are "<<d;
//get character
getch();
}
``````

output will be

Enter the first no.
10
Enter the second no.
22
Enter the third no.
30

Sum of three numbers are 62.

Fourth program

``````#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int a,b,c,d,e;
//Input the four numbers and save them in 'a' , 'b','c' and'd'
cout<<"Enter the first no."<<endl;
cin>>a;
cout<<"Enter the second no."<<endl;
cin>>b;
cout<<"Enter the third no."<<endl;
cin>>c;
cout<<"Enter the fourth no."<<endl;
cin>>d;
//calculate sum and save them in 'e'
e=a+b+c+d;
//show the output e
cout<<"Sum of four numbers are "<<e;
//get character
getch();
}
``````

output will be

Enter the first no.
10
Enter the second no.
30
Enter the third no.
1
Enter the fourth no.
3
Sum of four numbers are 44.

How does it work.
1. you enter the numbers that are saved in their respective variables.
2. all are added and saved in one variable.
3. sum is displayed on screen.

Extending it.
You can extend it by adding number of variables and their cout and cin and modifying addition variable.

Explanation.
1. Include ‘iostream.h’ and ‘conio.h’ files.