Please open the menu to show more

Friday, July 21, 2017

How to add two positive integers in C and C++ without using any operator except comma

This program can be used to add two positive integers without using any operator.

<= How this code works, lets understand =>

- in this code we are passing %*c inside printf function
star will be replaced by the value of variable a and variable b
 - c will print spaces- so printf will be evaluated like printf("%10c%20c",' ',' ');
- which means the printf will show 10 spaces then 30 spaces, so total 30 spaces will be printed
- then printf will return the no of characters those are printed, the returned value will be stored inside variable result



#include<stdio.h>
int main()
{
int a = 10;
int b = 20;
int result;

result = printf("%*c%*c",a,' ',b,' ');

printf("\raddition of %d and %d is %d",a,b,result);
}

5 comments:

  1. i tried this code as.. a,' ',b,' in 7th line..but its not working
    help me out with correct code syntax!!

    ReplyDelete
    Replies
    1. Dear Prerna, the code is absolute correct. But, It will work for postive values only. If possible please post your code as comment. I will help for sure.

      Delete