Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

C++ program to addition of two numbers | 5 different Methods

Posted on October 10, 2019October 10, 2019

Table of Contents

  • C++ program to addition of two numbers | 5 different Methods
    • Sum of two numbers – stranded method
    • Sum of two numbers – Entered by user
    • Sum of two numbers – using the function
    • Sum of two numbers – using recursion
    • Sum of two numbers – using the pointer

C++ program to addition of two numbers | 5 different Methods

In this tutorial, we will discuss the  C++ program to addition of two numbers 

C++ program to addition of two numbers
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

Sum of two numbers – stranded method

#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

  • Integer variable num1,num2 are declared and initialized
  • the num1 and num 2 both are added together  and the value is added to the sum
  • Then, the program is Displayed the output (Sum of two integers) using  cout<<

 

Program 2

Sum of two numbers – Entered by user

#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

  1. Declare variables num1,num2,sum
  2. The program requires input from the user
  3. Then the user enters the input value for num1, num2(variables)
  4. The program will read the input using cin>> and store in the variables num1 and num2 respectively
  5. the num1 and num 2 both are added together  and the value is added to the sum
  6. Then, the program Displays the value of the sum using cout<<

Sum of two numbers – using the function

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

  • Declare a function named as sum_Num() with two int parameter
  • Declare variables num1,num2,sum
  • The program requires input from the user
  • Then the user enters the input value for num1, num2
  • The program will read the input using cin>> fand store in the variables num1 and num2 respectively
  • Define the function (sum_Num()) for find sum
  • Call the function to produce output.
  • Then, the program Displays the value of the sum using cout<<

 

Sum of two numbers – using recursion

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

  • Declare a function named as sum_Num() with two int parameter
  • Declare variables num1,num2,sum
  • The program requires input from the user
  • Then the user enters the input value for num1, num2
  • The program will read the input using cin>> and store in the variables num1 and num2 respectively
  • Define the function (sum_Num()) for find sum recursively
  • Call the function to produced output
  • Then, the program Displays the value of the sum using cout<< 

Sum of two numbers – using the pointer

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

 

Operator in C++ language

Function in C++ language

Pointer in C++ language

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes