Write a program to find sum of positive integers without using any operator. Only use of printf() is allowed. No other library function can be used.
Solution
It’s a trick question. We can use printf() to find sum of two numbers as printf() returns the number of characters printed. The width field in printf() can be used to find the sum of two numbers. We can use ‘*’ which indicates the minimum width of output. For example, in the statement “printf(“%*d”, width, num);”, the specified ‘width’ is substituted in place of *, and ‘num’ is printed within the minimum width specified. If number of digits in ‘num’ is smaller than the specified ‘width’, the output is padded with blank spaces. If number of digits are more, the output is printed as it is (not truncated). In the following program, add() returns sum of x and y. It prints 2 spaces within the width specified using x and y. So total characters printed is equal to sum of x and y. That is why add() returns x+y.
#include <stdio.h> int add(int x, int y) { return printf("%*c%*c", x, ' ', y, ' '); } // Driver code int main() { printf("Sum = %d", add(3, 4)); return 0; } |
Output:
Sum = 7
The output is seven spaces followed by “Sum = 7”. We can avoid the leading spaces by using carriage return. Thanks to krazyCoder and Sandeep for suggesting this. The following program prints output without any leading spaces.
#include <stdio.h> int add(int x, int y) { return printf("%*c%*c", x, '\r', y, '\r'); } // Driver code int main() { printf("Sum = %d", add(3, 4)); return 0; } |
Output:
Sum = 7
Another Method :
C++
#include <iostream> using namespace std; int main() { int a = 10, b = 5; if (b > 0) { while (b > 0) { a++; b--; } } if (b < 0) { // when 'b' is negative while (b < 0) { a--; b++; } } cout << "Sum = " << a; return 0; } // This code is contributed by SHUBHAMSINGH10 // This code is improved & fixed by Abhijeet Soni. |
C
#include <stdio.h> int main() { int a = 10, b = 5; if (b > 0) { while (b > 0) { a++; b--; } } if (b < 0) { // when 'b' is negative while (b < 0) { a--; b++; } } printf("Sum = %d", a); return 0; } // This code is contributed by Abhijeet Soni |
Java
// Java code class GfG { public static void main(String[] args) { int a = 10, b = 5; if (b > 0) { while (b > 0) { a++; b--; } } if (b < 0) { // when 'b' is negative while (b < 0) { a--; b++; } } System.out.println("Sum is: " + a); } } // This code is contributed by Abhijeet Soni |
Python 3
# Python 3 Code if __name__ == '__main__': a = 10 b = 5 if b > 0: while b > 0: a = a + 1 b = b - 1 if b < 0: while b < 0: a = a - 1 b = b + 1 print("Sum is: ", a) # This code is contributed by Akanksha Rai # This code is improved & fixed by Abhijeet Soni |
C#
// C# code using System; class GFG { static public void Main() { int a = 10, b = 5; if (b > 0) { while (b > 0) { a++; b--; } } if (b < 0) { // when 'b' is negative while (b < 0) { a--; b++; } } Console.Write("Sum is: " + a); } } // This code is contributed by Tushil // This code is improved & fixed by Abhijeet Soni. |
PHP
<?php // PHP Code $a = 10; $b = 5; if ($b > 0) { while($b > 0) { $a++; $b--; } } if ($b < 0) { while($b < 0) { $a--; $b++; } } echo "Sum is: ", $a; // This code is contributed by Dinesh // This code is improved & fixed by Abhijeet Soni. ?> |
Output:
sum = 15
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Implementing ternary operator without any conditional statement
- Largest of two distinct numbers without using any conditional statements or operators
- Divide two integers without using multiplication, division and mod operator | Set2
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Smallest string without any multiplication sign that represents the product of two given numbers
- Program to find remainder without using modulo or % operator
- Decimal to Binary using recursion and without using power operator
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Set a variable without using Arithmetic, Relational or Conditional Operator
- Division without using '/' operator
- Maximum OR value of a pair in an Array without using OR operator
- Find two numbers whose sum is N and does not contain any digit as K
- Find HCF of two numbers without using recursion or Euclidean algorithm
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Find the maximum angle at which we can tilt the bottle without spilling any water
- Print substring of a given string without using any string function and loop in C
- Print "Hello World" in C/C++ without using any header file
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?
- To check a number is palindrome or not without using any extra space
- Print Number series without using any loop

