Find Sum of Two Numbers Without Using Any Operator in C



In this article, we Implement the C program to find the sum of two numbers without using any Operator.

Find Sum of Two Numbers Without Using Any Operator in C

This problem is tricky. To solve this problem we are using the minimum width features of the printf() statement. We can use '*' which indicates the minimum width of output.

For example, in this statement "printf("%*d", width, num);", the specified 'width' is substituted as *, and 'num' is printed within the minimum width specified. If the number of digits in 'num' is less than the provided 'width', the output will be padded with blank spaces. If the number of digits is more, the output is written as is (untruncated).

Example

In the programme below, add() returns the sum of x and y. It prints two spaces inside the provided width using x and y. So, the total number of characters written equals the sum of x and y. That is why add() returns x + y.

#include<stdio.h>
int add(int x, int y) {
   int len;
   len = printf("%*c%*c", x, ' ', y, ' ');
   return len;
}
int main() {
   int x = 10, y = 20;
   int res = add(x, y);
   printf("\nThe result is: %d", res);
}

Following is the output of the code:

The result is: 30

You can also use this statement with carriage return return printf("%*c%*c", x, '\r', y, '\r'); to avoid the leading space.

Updated on: 2025-06-09T18:40:20+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started