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)
Approach: Bit Manipulation
Steps:
- Calculate the sum of the numbers without taking into account the carry.
- Calculate the carry using bitwise AND and shift left operator.
- Add the carry to the sum.
- Repeat steps 1-3 until there is no carry left.
C++
#include <iostream>
int add_without_operator( int a, int b)
{
while (b != 0) {
int sum = a ^ b;
int carry = (a & b) << 1;
a = sum;
b = carry;
}
return a;
}
int main()
{
int a = 5;
int b = 7;
int result = add_without_operator(a, b);
std::cout << "The sum of " << a << " and " << b
<< " is: " << result << std::endl;
return 0;
}
|
Java
public class AddWithoutOperator {
public static int add( int a, int b)
{
while (b != 0 ) {
int sum = a ^ b;
int carry
= (a & b)
<< 1 ;
a = sum;
b = carry;
}
return a;
}
public static void main(String[] args)
{
int a = 5 ;
int b = 7 ;
int result = add(a, b);
System.out.println( "The sum of " + a + " and " + b
+ " is: " + result);
}
}
|
Python3
def add_without_operator(a: int , b: int ) - > int :
while b ! = 0 :
sum = a ^ b
carry = (a & b) << 1
a = sum
b = carry
return a
a = 5
b = 7
result = add_without_operator(a, b)
print (f "The sum of {a} and {b} is: {result}" )
|
C#
using System;
class Program
{
static int AddWithoutOperator( int a, int b)
{
while (b != 0)
{
int sum = a ^ b;
int carry = (a & b) << 1;
a = sum;
b = carry;
}
return a;
}
static void Main( string [] args)
{
int a = 5;
int b = 7;
int result = AddWithoutOperator(a, b);
Console.WriteLine( "The sum of {0} and {1} is: {2}" , a, b, result);
}
}
|
Javascript
function addWithoutOperator(a, b) {
while (b !== 0) {
let sum = a ^ b;
let carry = (a & b) << 1;
a = sum;
b = carry;
}
return a;
}
let a = 5;
let b = 7;
let result = addWithoutOperator(a, b);
console.log(`The sum of ${a} and ${b} is: ${result}`);
|
Output
The sum of 5 and 7 is: 12
Time Complexity: O(log N), where N is the maximum number of bits in a or b.
Auxiliary Space: O(1)
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!