➤ Hello World! in C++
➤ Print Number in C++
➤ Add 2 Numbers C++
➤ Arithmetic Operation
➤ Sum Avg of 3 Number
➤ Area Program in C++
➤ Simple Interest in C++
➤ Find ASCII value in C++
➤ Swap 2 Number in C++
Flow Control Programs
➤ Even-Odd in C++
➤ +ve, -ve, 0 in C++
➤ Vowel-Consonant
➤ Greatest of 3 no.
➤ Check Leap Year
➤ Calculator Program
➤ Reverse a Number
➤ Sum of Natural Number
➤ GCD of 2 Number
➤ LCM of 2 Number
➤ Find Power in C++
➤ Fibonacci Series in C++
➤ Palindrome Number
➤ Find Factorial in C++
➤ Factorial Using Recursion
➤ Prime Number in C++
➤ Prime Number b/w 1-N
Array
➤ Linear Search in C++
➤ Binary Search in C++
Others
➤ Introduction to C++
➤ Data Types in C++
➤ Range of Data Types
➤ Void main, main vs int main
In this post, we will develop the C++ program for the addition of two numbers. First, we will develop a simple program for the addition of two numbers in C++, and then we will develop a C++ program for the addition of two numbers using functions.
To add two numbers we have to declare two variables. The data type of these variables can be int, float, or double type. Since using double data type we can store both integer and floating-point numbers so we will take double data type. We can take input directly (hardcode) or we can ask the end-user for the input.
// C++ program to add two numbers
#include<iostream>
using namespace std;
int main()
{
// declare two variables to store numbers
double num1, num2;
// declare variable to store sum value
double sum;
// ask input from end-user
cout << "Enter Two Numbers : ";
cin >> num1 >> num2;
// calculate sum value
sum = num1 + num2;
// display result
cout << "Sum = " << sum << endl;
return 0;
}
Output:-
Enter Two Numbers: 12 13
Sum = 25
C++ Program for Addition of Two Numbers Using Functions
Here we will write a C++ program for the addition of two numbers using functions. For this develop a function add() to calculate the addition of two integers and display sum value in the main() function.
A function is a block of code that performs a specific task. For example, the main is function and every program execution starts from main function in C++ programming.
// C++ Program for Addition of Two Numbers Using Functions
#include<iostream>
using namespace std;
// function prototype (declaration)
double add(double, double);
// main method
int main()
{
// declare variables
double num1, num2, sum;
// take input from end-user
cout << "Enter Two Numbers : ";
cin >> num1 >> num2;
// calculate sum value
// function call
sum = add (num1, num2);
// display sum value
cout << "Sum = " << sum << endl;
return 0;
}
// function definition
double add(double x, double y)
{
double z;
z = x + y;
//return statement
return z;
}
Output:-
Enter Two Numbers: 45.8 12.5
Sum = 58.3
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
Learn More C++ Programming Examples,
- Introduction to C++ Programming
- Data Types in C++ Programming
- Find Range of Data Types in C++
- Void main(), main() vs int main() in C++
- Hello World! Program in C++
- Addition of Two Numbers in C++
- Add Subtract Multiply Divide 2 Numbers
- Sum and Average of three numbers in C++
- Area of Circle Triangle Rectangle in C++
- Calculate Simple Interest in C++
- C++ program to find ASCII value
- C++ program to swap two Number
- Check Even or Odd Number in C++
- Find Positive Negative Zero in C++
- Check Vowel or Consonant in C++
- Greatest of Three Numbers in C++
- Program to Check Leap Year in C++
- Simple Calculator Program in C++
- Program to Reverse a Number in C++
- Sum of Natural Numbers in C++
- C++ Program for GCD of Two Numbers
- C++ Program for LCM of Two Numbers
- Find Power of a Number in C++
- Fibonacci Series Program in C++
- Check Palindrome Number in C++
- Factorial of a Number Program in C++
- Factorial Using Recursion in C++
- Prime Number Program in C++
- Prime Number b/w 1 to N in C++