Algorithm


Algorithm: Addition of Two Numbers

Start:

  • Initialize variables for the first number (num1), the second number (num2), and the sum (sum).
  1. Input:

    • Read the first number (num1) from the user.
    • Read the second number (num2) from the user.
  2. Process:

    • Add the values of num1 and num2.
      bash
      sum = num1 + num2;
  3. Output:

    • Display the sum to the user.
      mathematica
      Display "The sum of num1 and num2 is: sum".
  4. End.

This algorithm is a step-by-step description of the process of adding two numbers. When implementing this algorithm in a specific programming language, you would use the syntax of that language to perform the input, addition, and output operations.

 

Code Examples

#1 Code Example-Add of two numbers in C++ using a user-defined function

Code - C++ Programming

#include<iostream>
using namespace std;
int main()
{
    int A, B, sum;    
    cout << "Please enter the First Number  : "<<endl;
    cin >> A;    
    cout << "Please enter the Second Number : "<<endl;
    cin >> B;    
    sum = A + B;
    cout << "Sum of Two Numbers " << A <<" and " << B << " = " << sum;    
    return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Please enter the First Number: 10
Please enter the Second Number: 20
Sum of Two Numbers 10 and 20 = 30

#2 Code Example- Add of two numbers in C++ using functions

Code - C++ Programming

#include<iostream>
using namespace std;

int add(int x, int y)
{
    return x + y;
}

int main()
{
    int A, B, sum;
    
    cout << "Please enter the First Number  : "<<endl;
    cin >> A;
    
    cout << "Please enter the Second Number : "<<endl;
    cin >> B;
    
    sum = add(A, B);
    cout << "Sum of Two Numbers " << A <<" and " << B << " = " << sum;
    
    return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Please enter the First Number: 10
Please enter the Second Number: 10
Sum of Two Numbers 10 and 10 = 20

#3 cODE eXAMPLE-Add of two numbers in C++ using recursion

Code - C++ Programming

#include<iostream>
using namespace std;

int addFun(int, int);
int main()
{
    int A, B, sum;
    cout<<"Enter Two Numbers: ";
    cin>>A>>B;
    sum = addFun(A, B);
    cout<<"\nResult = "<<sum;
    cout<<endl;
    return 0;
}
int addFun(int a, int b)
{
    if(b==0)
        return a;
    else
        return (1+addFun(a, b-1));
}
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter Two Numbers: 20 20
Result = 40
Advertisements

Demonstration


C++ Programing Example to Add Two Numbers-DevsEnv