- On
- By
- 0 Comment
- Categories: addition, Calculations
Cpp program to sum two numbers
Cpp program to sum two numbers
In this tutorial, we will discuss the topic of Cpp program to sum two numbers
In this topic, we will learn how to add two numbers in C++ programming language
Find Sum to numbers Example- 1
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int f_no=45, s_no=35,sum;//variable declaration sum=f_no+s_no; //find sum of integers cout << "sum of two numbers: "<<sum<< endl;//display result getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Sum of two numbers: 80
Find Sum to numbers(Get input from user) – Example- 2
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { int f_no, s_no,sum; cout << "Enter the first number: "<< endl; cin>>f_no;//get input from user for f_no cout << "Enter the second number: "<< endl; cin>>s_no; //get input from user for s_no sum=f_no+s_no; //find sum of numbers cout << "sum of two numbers: "<< endl; cout <<f_no<<" + "<<s_no<<" = "<<sum<< endl; //display the result getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Enter the first number: 75 Enter the second number: 65 sum of two numbers 75 + 65 = 140
In this program, the user has entered two integer values. These two values are stored in variables f_no, s_no.
Then both of values are added using plus (+) operator and stored in sum variable.
Finally, the sum is displayed on the screen
Suggested for you