

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to sum two integers without using arithmetic operators in C/C++?
The following is an example to add two numbers without using arithmetic operators.
Example
#include <iostream> #include <cmath> using namespace std; int add(int val1, int val2) { while(val2 != 0) { int c = val1 & val2; val1 = val1 ^ val2; val2 = c << 1; } return val1; } int main() { cout <<"The sum of two numbers : "<< add(28, 8); return 0; }
Output
The sum of two numbers : 36
In the above program, a function add() is defined with two int type arguments. The addition of two numbers is coded in add()
int add(int val1, int val2) { while(val2 != 0) { int c = val1 & val2; val1 = val1 ^ val2; val2 = c << 1; } return val1; }
In the main() function, the result is printed by calling function add()
cout <<"The sum of two numbers : "<< add(28, 8);
- Related Questions & Answers
- How to sum two integers without using arithmetic operators in C/C++ Program?
- Arithmetic Operators in C++
- What are arithmetic operators in C#?
- How do I add two numbers without using ++ or + or any other arithmetic operator in C/C++?
- Simple Arithmetic Operators Example Program In C++
- Sum of array using pointer arithmetic in C++
- Sum of array using pointer arithmetic in C
- How to get the product of two integers without using * JavaScript
- C Program to find sum of two numbers without using any operator
- Divide Two Integers in C++
- Java arithmetic operators
- Perl Arithmetic Operators
- Python Arithmetic Operators
- C Program to Add two Integers
- Explain the concept of Arithmetic operators in C language
Advertisements