Table of Contents
In this tutorial, we will discuss the C++ program to addition of two numbers
In this post, we are going to learn how to find the sum of two numbers through different 5 ways in C++ programming language
Program 1
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num1=15,num2=35, sum;//Declare variable num1,num2,sum
sum=num1+num2;
cout<<"Sum of two integer : "<<sum;
getch();
return 0;
}
When the above code is executed it produces the following output
Sum of two number : 50
In this program
Program 2
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num1,num2, sum;//Declare variable num1,num2,sum
cout<<"Enter two integer for find sum: ";
cin>>num1>>num2;
sum=num1+num2;
cout<<"Sum of given integer : "<<sum;
getch();
return 0;
}
When the above code is executed it produces the following output
Enter two numbers for find sum: 123 321 Sum of given integers : 444
Method
Program 3
#include <iostream>
#include <conio.h>
using namespace std;
int sum_Num(int, int);
int main()
{
int num1,num2, sum;//Declare variable num1,num2,sum
cout<<"Enter two integer for find sum: ";
cin>>num1>>num2;
sum=sum_Num(num1, num2);
cout<<"Sum of given integer : "<<sum;
getch();
return 0;
}
int sum_Num(int a, int b){
int result=a+b;
return result;
}
When the above code is executed it produces the following output
Enter two integer for find sum: 135 425 Sum of given integer:50
Method
Program 4
#include <iostream>
#include <conio.h>
using namespace std;
int sum_Num(int,int);//function prototype
int main()
{
int num1,num2, sum;//Declare variables num1,num2,sum
cout<<"Enter two integer to find sum: ";
cin>>num1>>num2;
sum=sum_Num(num1, num2);//function call
cout<<"Sum of given integer : "<<sum;
getch();
return 0;
}
int sum_Num(int a, int b){//Function definition
if(b==0) //recursive function
return a;
else
return(1+sum_Num(a,b-1));
}
When the above code is executed it produces the following output
Enter two integer to find sum: 212 188 Sum of given integr : 400
Method
Program 5
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int *ptr1,*ptr2;//declare pointer variables
int num1,num2,tot;//Declare normal variable
cout<<"Enter two integer for find sum: ";
cin>>num1>>num2;
ptr1=&num1;
ptr2=&num2;
tot = *ptr1 + *ptr2;
cout<<"Sum of given integer :"<<tot;
getch();
return 0;
} When the above code is executed it produces the following output
Enter two integer for find sum: 345 543 Sum of given integers : 888
Similar post
Java program to the addition of two numbers
Python program to the addition of two numbers
C program to the addition of two numbers
Suggested for you
Function in C++ language
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.