EmbLogic's Blog

Adding two numbers without using operators

we have provided many utilities in printf ,in which one of it is adding two positive number (only for positive numbers).here it goes like this,

we all now that printf() On success,returns the total number of characters written. On failure, a negative number is returned.now the new thing might some of you know that ,The “%*d” format specifier causes printf() to read two integers from its arguments, the first to set the field width and the second here it is a decimal number(depends on specifier).

now using           printf(“%*c%*c”,a,’  ‘,b,’  ‘);     we can get the sum of the number of a and b;

but how:- the first specifier * will be width (suppose a=3 and b=2) which gives the width 3 and 2 respectively and the second is blank spaces that means the blank spaces will print on 3rd and 2nd widths and printf will return 5 is is the sum (a+b=5)

sample program

printf(“enter the 1st number\n”);
scanf(“%d”,&a);
printf(“enter the 2nd number\n”);
scanf(“%d”,&b);

printf(“%d”,printf(“%*c%*c”,a,’ ‘,b,’ ‘));
By Mukund and Abhilash

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>