In this tutorial you will learn about the C Program to Add numbers without using arithmetic Operators and its application with practical example.
In this tutorial, we will learn to create a C program that add two number without using arithmetic operator(+) using C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- C Operators
- Loop statements
- C while Loop.
Program to Add two numbers without using arithmetic Operators.
Easiest way for adding two numbers without using arithmetic operator ‘+’ in C is with using increment(++) and decrement(–) operator .
In this program we will add two number using while loop.
Firstly we declare required header file and variable and also initiates required values in variable. Next we take value from user at run time and then after we will add these two values without using arithmetic operator.
Let see the code for our program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main() { int a,b; //declaring variable printf("Enter two values"); // taking two values from user scanf("%d%d",&a,&b); while(b!=0) // traversing the loop { a++; // incrementing by 1 b--; // decrementing by 1 } printf("Sum of two number is =%d" ,a); //output return(0); } |
Output
In the above program, we have first declared and initialized a set variables required in the program.
- a = taking value from user in first variable.
- b = taking value from user in second variable.
For adding 2 integer values without using arithmetic operators(+,-*,/), we can do this with using increment and decrement operator.
Take a look in below image.
The above condition tells you what is actually we have done , we used increment and decrement operator instead of ( + ) or ( – ) to add two numbers
Firstly we got two numbers from user in a variable a and b . Then, we check the condition until the second variable b is not equal to 0. If it’s zero, we terminate the loop . If it’s a not zero number, then execute the following statement .
in which in each iteration the value of a increment to 1 and the value of b decrement to 1.The above statement will run until while (b!=0).
then in each iteration value of a is added and this process continues and we get final required output.
C Program to Add two numbers without using arithmetic Operators also possible Using logarithm and exponential functions and either using pointers or using bit-wise operators.