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.
C++
#include <iostream>
usingnamespacestd;
intadd(intx, inty)
{
returnprintf("%*c%*c", x, ' ', y, ' ');
}
// Driver code
intmain()
{
printf("Sum = %d", add(3, 4));
return0;
}
// This code is contributed by shubhamsingh10
C
#include <stdio.h>
intadd(intx, inty)
{
returnprintf("%*c%*c", x, ' ', y, ' ');
}
// Driver code
intmain()
{
printf("Sum = %d", add(3, 4));
return0;
}
Python3
# Python code for the above approach
defadd(x, y) :
return(x +y);
# Driver code
if__name__ =="__main__":
print("Sum = ", add(3, 4))
# This code is contributed by splvel62.
Output:
Sum = 7
Time Complexity: O(1)
Auxiliary Space: O(1)
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.
C++
#include <iostream>
usingnamespacestd;
intadd(intx, inty)
{
returnprintf("%*c%*c", x, '\r', y, '\r');
}
// Driver code
intmain()
{
printf("Sum = %d", add(3, 4));
return0;
}
// This code is contributed by shubhamsingh10
C
#include <stdio.h>
intadd(intx, inty)
{
returnprintf("%*c%*c", x, '\r', y, '\r');
}
// Driver code
intmain()
{
printf("Sum = %d", add(3, 4));
return0;
}
Java
classGFG {
staticintadd(intx, inty) {
return(x + y);
}
// Driver code
publicstaticvoidmain(String[] args) {
System.out.printf("Sum = %d", add(3, 4));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python program for the above approach
defadd(x, y) :
return(x +y);
# driver code
print("Sum = ", add(3, 4));
# This code is contributed by sanjoy_62
C#
// C# program for the above approach
usingSystem;
publicclassGFG {
staticintadd(intx, inty)
{
return(x + y);
}
// Driver Code
publicstaticvoidMain(String[] args) {
Console.WriteLine("Sum = "+ add(3, 4));
}
}
// This code is contributed by code_hunt.
Javascript
<script>
// JavaScript code for the above approach
functionadd(x, y)
{
return(x + y);
}
// Driver Code
document.write("Sum = "+ add(3, 4));
// This code is contributed by avijitmondal1998.
</script>
Output:
Sum = 7
Time Complexity: O(1)
Auxiliary Space: O(1)
Another Method :
C++
#include <iostream>
usingnamespacestd;
intmain()
{
inta = 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;
return0;
}
// This code is contributed by SHUBHAMSINGH10
// This code is improved & fixed by Abhijeet Soni.
C
#include <stdio.h>
intmain()
{
inta = 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);
return0;
}
// This code is contributed by Abhijeet Soni
Java
// Java code
classGfG {
publicstaticvoidmain(String[] args)
{
inta = 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
Python3
# Python 3 Code
if__name__ =='__main__':
a =10
b =5
ifb > 0:
whileb > 0:
a =a +1
b =b -1
ifb < 0:
whileb < 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
usingSystem;
classGFG {
staticpublicvoidMain()
{
inta = 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.
?>
Javascript
<script>
// Javascript program for the above approach
// Driver Code
let a = 10, b = 5;
if(b > 0) {
while(b > 0) {
a++;
b--;
}
}
if(b < 0) { // when 'b' is negative
while(b < 0) {
a--;
b++;
}
}
document.write("Sum = "+ a);
</script>
Output:
sum = 15
Time Complexity: O(b)
Auxiliary Space: O(1)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy