- On
- By
- 0 Comment
- Categories: addition, Calculations
C++ code to Add two integer using without + operator
C++ code to Add two integer using without + operator
In this article, we will discuss the concept of the C++ code to Add two integer using without + operator
In this post, we are going to learn how to write a program to find the sum of two numbers using without plus operator in C++ programming language
Code to find the sum of two numbers
Sum of two integer using with – operator
The program allows the user to enter two integers and then calculates the sum of given numbers using minus operator in C++ language
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int num1,num2; cout<<"Enter two number for addition:\n"; cin>>num1>>num2; //get input from the user for num1 and num2 int result =num1-(-num2); //Calculate addition using - operator cout<<"Sum of two numbers "<<num1<<" and "<<num2<<" is "<<result; //display result on the screen getch(); return 0; }
When the above code is executed, it produces the following result
Enter two number for addition 2222 111 Sum of two numbers 2222 and 111 is 2333
Sum of two integer using for loop
The program allows the user to enter two integers and then calculates the sum of given numbers using for loop in C++ language
Program 2
#include <iostream> #include <conio.h> using namespace std; int add(int ,int);//function prototype int main() { int n1,n2; cout<<"Enter the first number: "; cin>>n1; //get input from the user for n1 cout<<"Enter the second number: "; cin>>n2; //get input from the user for n2 cout<<"Sum of two numbers is: "<<add(n1,n2); //Calling the function and display result on the screen getch(); return 0; } int add(int num1, int num2){//function definition int i; for(i=0; i<num2; i++){ num1++; } return num1; }
When the above code is executed, it produces the following result
Enter the first number: 321 Enter the first number:432 Sum of two numbers is: 753
Sum of two integer using while loop
The program allows the user to enter two integers and then calculates the sum of given numbers using while loop in C++ language
Program 3
#include <iostream> #include <conio.h> using namespace std; int add(int ,int);//function prototype int main() { int n1,n2; cout<<"Enter the first number: "; cin>>n1; //get input from the user for n1 cout<<"Enter the second number: "; cin>>n2; //get input from the user for n2 cout<<"Sum of two numbers is:"<<add(n1,n2); //Calling the function and display result on the screen getch(); return 0; } int add(int num1, int num2){//function definition int i; i=0; while(i<num2){ num1++; i++; } return num1; }
When the above code is executed, it produces the following result
Enter the first number: 456 Enter the first number: 678 Sum of two numbers is: 1134
Sum of two integer using increment,decrement operator
The program allows the user to enter two integers and then calculates the sum of given numbers using increment,decrement operator in C++ language
Program 4
#include <iostream> #include <conio.h> using namespace std; int add(int ,int);//function prototype int main() { int n1,n2; cout<<"Enter the first number: "; cin>>n1; //get input from the user for n1 cout<<"Enter the second number: "; cin>>n2; //get input from the user for n2 cout<<"Sum of two numbers is: "<<add(n1,n2); //Calling the function and display result on the screen getch(); return 0; } int add(int n1,int n2){//function definition while(n1>0){ n2++; n1--; } while(n1<0){ n2--; n1++; } return n2; }
When the above code is executed, it produces the following result
Enter the first number: 2345 Enter the first number: 3456 Sum of two numbers is: 5801
Suggested for you
Similar post
Subtract two numbers in C++ language
Subtract two numbers using function in C++ language
Subtract two numbers using recursion in C++ language
Find sum of two numbers in C++
Find sum of two numbers in C++ using recursion
Find sum of two numbers in C++ without Arithmetic operator