- C++ Programming Examples
- C++ Programming Examples
- C++ Hello World
- C++ Get Input
- C++ Print Integer
- C++ Add Two Numbers
- C++ Add Sub Mul Div
- C++ Add Digits
- C++ Find Average Perc
- C++ Find Arithmetic Mean
- C++ Sum of n Natural Numbers
- C++ Sum of n Numbers
- C++ Area Perimeter of Square
- C++ Area Perimeter of Rectangle
- C++ Area Perimeter of Triangle
- C++ Area Circum of Circle
- C++ Find Simple Interest
- C++ Fahrenheit to Celsius
- C++ Celsius to Fahrenheit
- C++ Print Prime Numbers
- C++ Reverse a Number
- C++ Swap Two Numbers
- C++ Print Multiplication Table
- C++ Find Factorial of Number
- C++ Find Factors of Number
- C++ Find HCF & LCM
- C++ Make Calculator
- C++ Count Digits in Number
- C++ Sum of First & Last Digit
- C++ Product of Digits of Number
- C++ Sum of Squares of Digits
- C++ Interchange Digits of Number
- C++ if else Programs
- C++ Check Even or Odd
- C++ Check Prime or Not
- C++ Check Alphabet or Not
- C++ Check Vowel or Not
- C++ Check Leap Year or Not
- Check Reverse equal Original
- C++ Check Perfect Number
- C++ Check Palindrome or Not
- C++ Check Armstrong or Not
- C++ Divisibility Test
- C++ Find Wage of Labor
- C++ Find Discounted Price
- C++ Find Shipping Charge
- C++ Find Telephone Bills
- C++ Calculate Student Grade
- C++ Largest of Two Numbers
- C++ Largest of Three Numbers
- C++ Number Conversion
- C++ Decimal to Binary
- C++ Decimal to Octal
- C++ Decimal to Hexadecimal
- C++ Binary to Decimal
- C++ Binary to Octal
- C++ Binary to Hexadecimal
- C++ Octal to Decimal
- C++ Octal to Binary
- C++ Octal to Hexadecimal
- C++ Hexadecimal to Decimal
- C++ Hexadecimal to Binary
- C++ Hexadecimal to Octal
- C++ Pattern Programs
- C++ Pattern Programs
- C++ Print Diamond Pattern
- C++ Print Floyd's Triangle
- C++ Print Pascal's Triangle
- C++ Array Programs
- C++ 1D Array Program
- C++ Linear Search
- C++ Binary Search
- Find Largest Element in Array
- Find Smallest Element in Array
- Find Second Largest Element
- Find Second Smallest Element
- C++ Sum of All Elements
- C++ Multiply All Elements
- C++ Element on Even Position
- C++ Element on Odd Position
- C++ Print Even Numbers in Array
- C++ Print Odd Numbers in Array
- C++ Count Even/Odd Numbers
- C++ Sum of Even/Odd Numbers
- C++ Count Positive Negative Zero
- C++ Reverse an Array
- C++ Insert Element in Array
- C++ Delete Element from Array
- C++ Merge two Arrays
- C++ Bubble Sort
- C++ Selection Sort
- C++ Insertion Sort
- C++ Common Elements
- C++ 2D Array Programs
- C++ Add Two Matrices
- C++ Subtract Two Matrices
- C++ Transpose Matrix
- C++ Multiply Two Matrices
- C++ 3D Array Programs
- C++ String Programs
- C++ Print String
- C++ Find Length of String
- C++ Compare Two Strings
- C++ Copy String
- C++ Concatenate String
- C++ Reverse a String
- C++ Delete Vowels from String
- C++ Delete Word from String
- C++ Count Character in String
- C++ Count Word in String
- C++ Frequency of Word
- C++ Remove Spaces from String
- C++ Sort a String
- C++ Uppercase to Lowercase
- C++ Lowercase to Uppercase
- C++ Swap Two Strings
- C++ Check Anagram or Not
- C++ Capitalize All Words in String
- C++ Capitalize Specific Character
- C++ Get Numbers from String
- C++ File Programs
- C++ Read a File
- C++ Write Content to File
- C++ Append Data in File
- C++ Read & Display File
- C++ Copy a File
- C++ Merge Two Files
- Count Characters, Words in File
- C++ Capitalize All Words in File
- C++ List Files in Directory
- C++ Delete a File
- C++ Encrypt & Decrypt a File
- C++ Misc Programs
- C++ Print ASCII Value
- C++ Add Binary Numbers
- C++ Generate Random Numbers
- C++ Print Smiling Face
- C++ Days into Years, Months
- Add Two Numbers using Pointer
- C++ Print Fibonacci Series
- Generate Armstrong Numbers
- C++ Find nCr and nPr
- C++ Get IP Address
- C++ Print Date/Time
- C++ Shutdown, Restart Computer
- C++ Programming Tutorial
- C++ Tutorial
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Program to Add Two Numbers
In this article, you will learn and get code about addition of any two number given by user (at run-time). Here are the list of programs. All these programs does the same job, that is to add two numbers given by user, but using different-different approach:
- Add two Numbers of Integer Type
- Add two Numbers of Floating-point Type
- Add two Numbers using user-defined Function
- Add two Numbers using Recursion
- Add two Numbers using Friend Function
- Add two Numbers using Class
Add two Numbers of Integer Type
Let's first start with very simple program for the addition of two number. The dry run of this program is given just after the output.
#include<iostream> using namespace std; int main() { int num1, num2, add; cout<<"Enter Two Numbers: "; cin>>num1>>num2; add = num1+num2; cout<<"\nResult = "<<add; cout<<endl; return 0; }
This program was build and run under Code::Blocks IDE. Here is the sample run:
Now supply any two number as input say 50 and 60. Press ENTER
to see the addition
result of given two numbers as output, as shown in the snapshot given below:
Now let's take a look at the brief explanation of the code along with dry run using 10 and 20 as input
- Three variables namely num1, num2, add of type int gets declared
- These variables hold only integer type value. That is, a whole number. A number, that does not contains any decimal part
- Now ask from user to enter any two numbers
- Get and store these two numbers in variables num1 and num2
- Now just initialize num1+num2 to the variable add
- Finally print the value of add as output
- For example, if user enters 10 and 20 as input
- Then, 10 gets stored in num1 and 20 gets stored in num2
- Using num1+num2, 10+20 or 30 gets initialized to add
- And 30 is the addition result of 10 and 20. Thats it
What if User enters Real Numbers ?
The above program is correct only when user supplies the two number input as an integer type value. That is, a value that does not contains any decimal part. But what if user supply any value that contains decimal part. In other words, what if user enters real-numbers. The solution to this problem is given in the following program.
#include<iostream> using namespace std; int main() { float num1, num2, add; cout<<"Enter Two Numbers: "; cin>>num1>>num2; add = num1+num2; cout<<"\nResult = "<<add; cout<<endl; return 0; }
As you can see from the above program. To add any two real numbers, just change the data type to float (floating-point type) before declaring all the three variables say num1, num2, and add. So that these variables becomes able to handle floating-point value (value that contains decimal part). Here is the sample run of above program:
Add two Numbers in C++ using Function
Let's approach function to create a program that adds two given number by user. The output of this program is totally same as the output of very first program given over here. To learn more about function, refer to Function in C++ tutorial.
#include<iostream> using namespace std; int addFun(int, int); int main() { int num1, num2, add; cout<<"Enter Two Numbers: "; cin>>num1>>num2; add = addFun(num1, num2); cout<<"\nResult = "<<add; cout<<endl; return 0; } int addFun(int a, int b) { return (a+b); }
Add two Numbers in C++ using Recursion
This program uses recursive function to apply the addition of given two number. Recursive function, means a function that calls itself from its definition part.
#include<iostream> using namespace std; int addFun(int, int); int main() { int num1, num2, add; cout<<"Enter Two Numbers: "; cin>>num1>>num2; add = addFun(num1, num2); cout<<"\nResult = "<<add; cout<<endl; return 0; } int addFun(int a, int b) { if(b==0) return a; else return (1+addFun(a, b-1)); }
Add two Numbers in C++ using Friend Function
The question is, write a program in C++, that uses friend function to add two numbers entered by user. The answer to this question is given below. To learn more about friend function, then refer to its seperate tutorial Friend Function in C++
#include<iostream> using namespace std; class CodesCracker { private: int a, b, sum; public: void getData() { cout<<"Enter Two Numbers: "; cin>>a>>b; } void showResult() { cout<<"\nResult = "<<sum; } friend void add(CodesCracker &c); }; int main() { CodesCracker c; c.getData(); add(c); c.showResult(); cout<<endl; return 0; } void add(CodesCracker &c) { c.sum = (c.a) + (c.b); }
Add Two Numbers in C++ using Class
This is the last program of this article, here we have applied class approach to create a program in C++ for the addition of two given number. To learn more about class, refer to class in C++ tutorial.
#include<iostream> using namespace std; class CodesCracker { private: int x, y; public: void getData(); int add(); }; void CodesCracker::getData() { cout<<"Enter Two Numbers: "; cin>>x>>y; } int CodesCracker::add() { return (x+y); } int main() { CodesCracker c; int sum; c.getData(); sum = c.add(); cout<<"\nResult = "<<sum; cout<<endl; return 0; }
Same Program in Other Language
You may like the same program in other programming languages:
« Previous Program Next Program »