Algorithm
Algorithm: Addition of Two Numbers
Start:
- Initialize variables for the first number (
num1
), the second number (num2
), and the sum (sum
).
-
Input:
- Read the first number (
num1
) from the user. - Read the second number (
num2
) from the user.
- Read the first number (
-
Process:
- Add the values of
num1
andnum2
.bashsum = num1 + num2;
- Add the values of
-
Output:
- Display the sum to the user.
mathematica
Display "The sum of num1 and num2 is: sum".
- Display the sum to the user.
-
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
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
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
Result = 40
Demonstration
C++ Programing Example to Add Two Numbers-DevsEnv