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>
using namespace std;
int add( int x, int y)
{
return printf ( "%*c%*c" , x, ' ' , y, ' ' );
}
int main()
{
printf ( "Sum = %d" , add(3, 4));
return 0;
}
|
C
#include <stdio.h>
int add( int x, int y)
{
return printf ( "%*c%*c" , x, ' ' , y, ' ' );
}
int main()
{
printf ( "Sum = %d" , add(3, 4));
return 0;
}
|
Java
public class Add
{
public static void main(String[] args)
{
int x = 3 , y = 4 ;
System.out.print( "Sum = " + add(x, y));
}
public static int add( int x, int y)
{
String str = String.format( "%" + x + "c%" + y + "c" , ' ' , ' ' );
return str.length();
}
}
|
Python3
def add(x, y) :
return (x + y);
if __name__ = = "__main__" :
print ( "Sum = " , add( 3 , 4 ))
|
C#
using System;
namespace Add
{
class Program
{
static void Main( string [] args)
{
int x = 3, y = 4;
Console.WriteLine( "Sum = " + Add(x, y));
}
static int Add( int x, int y)
{
string str = string .Format( "{0," + x + "}{1," + y + "}" , ' ' , ' ' );
return str.Length;
}
}
}
|
Javascript
function add(x, y)
{
return console.log( "%*c%*c" , x, ' ' , y, ' ' );
}
function main()
{
console.log( "Sum = %d" , add(3, 4));
return 0;
}
main();
|
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>
using namespace std;
int add( int x, int y)
{
return printf ( "%*c%*c" , x, '\r' , y, '\r' );
}
int main()
{
printf ( "Sum = %d" , add(3, 4));
return 0;
}
|
C
#include <stdio.h>
int add( int x, int y)
{
return printf ( "%*c%*c" , x, '\r' , y, '\r' );
}
int main()
{
printf ( "Sum = %d" , add(3, 4));
return 0;
}
|
Java
class GFG {
static int add( int x, int y) {
return (x + y);
}
public static void main(String[] args) {
System.out.printf( "Sum = %d" , add( 3 , 4 ));
}
}
|
Python3
def add(x, y) :
return (x + y);
print ( "Sum = " , add( 3 , 4 ));
|
C#
using System;
public class GFG {
static int add( int x, int y)
{
return (x + y);
}
public static void Main(String[] args) {
Console.WriteLine( "Sum = " + add(3, 4));
}
}
|
Javascript
<script>
function add(x, y)
{
return (x + y);
}
document.write( "Sum = " + add(3, 4));
</script>
|
Output:
Sum = 7
Time Complexity: O(1)
Auxiliary Space: O(1)
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) {
while (b < 0) {
a--;
b++;
}
}
cout << "Sum = " << a;
return 0;
}
|
C
#include <stdio.h>
int main()
{
int a = 10, b = 5;
if (b > 0) {
while (b > 0) {
a++;
b--;
}
}
if (b < 0) {
while (b < 0) {
a--;
b++;
}
}
printf ( "Sum = %d" , a);
return 0;
}
|
Java
class GfG {
public static void main(String[] args)
{
int a = 10 , b = 5 ;
if (b > 0 ) {
while (b > 0 ) {
a++;
b--;
}
}
if (b < 0 ) {
while (b < 0 ) {
a--;
b++;
}
}
System.out.println( "Sum is: " + a);
}
}
|
Python3
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)
|
C#
using System;
class GFG {
static public void Main()
{
int a = 10, b = 5;
if (b > 0) {
while (b > 0) {
a++;
b--;
}
}
if (b < 0) {
while (b < 0) {
a--;
b++;
}
}
Console.Write( "Sum is: " + a);
}
}
|
PHP
<?php
$a = 10;
$b = 5;
if ( $b > 0) {
while ( $b > 0)
{
$a ++;
$b --;
}
}
if ( $b < 0) {
while ( $b < 0)
{
$a --;
$b ++;
}
}
echo "Sum is: " , $a ;
?>
|
Javascript
<script>
let a = 10, b = 5;
if (b > 0) {
while (b > 0) {
a++;
b--;
}
}
if (b < 0) {
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
Please Login to comment...