

- 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++ Program?
In this tutorial, we will be discussing a program to understand how to sum two integers without using arithmetic operators in C/C++.
For adding two integers without using arithmetic operators, we can do this with either using pointers or using bitwise operators.
Example
Using pointers
#include <iostream> using namespace std; int sum(int a, int b){ int *p = &a; return (int)&p[b]; } int main() { int add = sum(2,3); cout << add << endl; return 0; }
Output
5
Example
Using bitwise operators
#include <iostream> using namespace std; int sum(int a, int b){ int s = a ^ b; int carry = a & b; if (carry == 0) return s; else return sum(s, carry << 1); } int main() { int add = sum(2,3); cout << add << endl; return 0; }
Output
5
- Related Questions & Answers
- How to sum two integers without using arithmetic operators in C/C++?
- Arithmetic Operators in C++
- Simple Arithmetic Operators Example Program In C++
- C Program to Add two Integers
- C Program to find sum of two numbers without using any operator
- What are arithmetic operators in C#?
- C++ Program for sum of arithmetic series
- How do I add two numbers without using ++ or + or any other arithmetic operator in C/C++?
- Sum of array using pointer arithmetic in C++
- Sum of array using pointer arithmetic in C
- C# program to determine if any two integers in array sum to given integer
- How to get the product of two integers without using * JavaScript
- Binary Search for Rational Numbers without using floating point arithmetic in C program
- Divide Two Integers in C++
- C program to find the sum of arithmetic progression series
Advertisements