addition

C++ program to add two numbers without using arithmetic operators

C++ program to add two numbers with out using arithmetic operators

In this tutorial, we will discuss the concept of C++ program to add two numbers without using arithmetic operators

In this post, we will learn how to make the addition of two number using without the arithmetic operator in C++ programming language

 

Addition

Using for loop – program 1

This program allows the user to enter two numbers and displays the sum of two numbers entered by the user using for loop

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num1,num2,i;  //variable declaration
    cout<<"Enter the first number: ";
   cin>>num1;
    cout<<"Enter the second number: ";
     cin>>num2;
    for(i=0; i<num2; i++){
   num1++;
    }
cout<<"Sum of two numbers is: "<<num1;
getch();
}

When the above code is executed, it produces the following results

Enter the first numb er: 345
Enter the second number: 155
Sum of two numbers is: 500

Methods

  • The user inputs the first number
  • Store  the number in variable num1
  • The user inputs the second number
  • Store  the number in variable num2
  • for loop” used for the addition of two numbers.
  • Display the sum of the numbers on the screen.

 

Using while loop – Program 2

This program allows the user to enter two numbers and displays the sum of two numbers entered by the user using while loop

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num1,num2,i;  //variable declaration
    cout<<"Enter the first number: ";
   cin>>num1;
    cout<<"Enter the second number: ";
    cin>>num2;
    while(num2 != 0){
int num3=(num1 & num2);
num1=num1^num2;
num2=num3<<1;
}
cout<<"\nSum of two numbers is: "<<num1;
getch();
}

 

When the above code is executed, it produces the following results

Enter the first numb er: 123
Enter the second number: 432
Sum of two numbers is: 555

Methods

  • The user inputs the first number
  • Store  the number variable num1
  • The user inputs the second number
  • Store  the number variable num2
  • while loop” used for the addition of two numbers.
  • Display the sum of the numbers on the screen.

 

Using for loop inside the method

This program allows the user to enter two numbers and displays the sum of two numbers entered by the user using for loop inside the method

#include <iostream>
#include <conio.h>
using namespace std;
int add(int ,int);//Function prototype
int main()
{
    int n1,n2;
    cout << "Enter the first number" << endl;
    cin>>n1;
   cout << "Enter the second number" << endl;
    cin>>n2;
     cout<<"Sum of two  numbers is:"<<add(n1,n2);  //function call
     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 results

Enter the first numb er: 
125
Enter the second number: 
235
Sum of two numbers is: 360

Method

  • The user inputs the first number
  • Store  the number in variable n1
  • The user inputs the second number
  • Store  the number in variable n2
  • Create a function with  “for loop” to find the sum of two numbers.
  • Call the function inside the main method.
  • Display the sum of the number

 

using while loop inside the method

This program allows the user to enter two numbers and displays the sum of two numbers entered by the user using while loop inside the method

#include <iostream>
#include <conio.h>
using namespace std;
int add(int ,int);   //function prototype
int main()
{
    int n1,n2;
    cout << "Enter the first number" << endl;
    cin>>n1;
   cout << "Enter the secone number" << endl;
    cin>>n2;
     cout<<"Sum of two  numbers is:"<<add(n1,n2);  //function call
     getch();
    return 0;
}

int add(int num1, int num2){   //function definition
int i;
while(num2 != 0){
int num3=(num1 & num2);
num1=num1^num2;
num2=num3<<1;
}
return num1;


}

 

When the above code is executed, it produces the following results

Enter the first numb er: 
234
Enter the second number: 
456
Sum of two numbers is: 690

Method

  • The user inputs the first number
  • Store  the number in variable n1
  • The user inputs the second number
  • Store  the number in variable n2
  • Create a function with  “while loop” to find the sum of two numbers.
  • Call the function inside the main method.
  • Display the sum of the number

 

Similar post

C program to add two numbers without using arithmetic operators

Java program to add two numbers without using arithmetic operators

Python program to add two numbers without using arithmetic operators

 

Suggested for you

For loop in C++language

While loop in C++ language

The operator in C++ language

Variable in C++ language

Datatype in C++ language

 

 

 

 

 

 

C program to find sum of two numbers without using arithmetic operators
Java program to add two numbers without using arithmetic operators
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago