How to add Two Number without using Operator in C?

How to add Two Number without using Operator in C?

You may have read my previous article where I have mentioned C is one of the simplest programming languages. It’s for beginners. If you want to be the master of C you have to explore and try new things. Or you should keep exploring for doing the same things in many ways.

In this code, I will share how you can add two number without using the operator in C.

How to add Two Number without using Operator in C?

This is one of the tricky questions asked in many of the placement interviews.

Solution 1: Using printf() Function

If you find the difficulty in writing this code, here is a hint for you.

Hint: You can do this by using printf() function which returns the number of characters written to the output console.

Minimize the browser and try this program by user self first.

#include<stdio.h>
void addTwoNumber(int m, int n)
{
    printf(“%d”,printf(“%*c%*c”,m,’ ‘,n,’ ‘));
}
void main()
{
     addTwoNumber(4,5);
}

Note: This program also prints the white spaces on the output console. You can ignore as it is not visible.

Solution 2: Using math.h library

This solution is provided by Fulvio Baccaglini.

You can use math.h library. Here is an idea, based on math.h atan2 performing a division, and some auxiliary transformations, to turn the division into multiplication, and then multiplication into addition.

Note: This solution is not purely without operates as we are using exp, pow…  operators. We can only say, this solution is without addition operators.

#include<stdio.h>
#include<math.h>

void addTwoNumber(int m, int n)
{
    int nSum;
    nSum = log (tan (atan2 (exp (m), pow (exp (n), floor (log (0.5))))));
    printf("%d", nSum);
}

void main()
{
    addTwoNumber(11,15);
}

If you like to take the challenge and explore the C language, try your best to write a code for following tricky questions.

Happy Programming!

Leave a Reply

Your email address will not be published. Required fields are marked *