To find sum of two numbers without using any operator. Only use of printf() is allowed.
2014-11-30 14:05
429 查看
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 ‘wodth’, the output is padded with blank spaces(右对齐). If number
of digits are more, the output is printed as it is (not truncated).
solution:
Output:
Output:Sum = 7
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 ‘wodth’, the output is padded with blank spaces(右对齐). If number
of digits are more, the output is printed as it is (not truncated).
solution:
Sum = 7
注意:Sum前面还会输出7个空格,因为add函数里面还会输出。如果想不输出前面的空格,可以用'\r'代替' ','\r'是使光标移到一行的开头。
相关文章推荐
- Your task is to find for a given phone number any of its divisions into groups of two or three digit
- Unable to find a value for "字段名" in object of class java.lang.String using operator "."
- Distribute numbers to two “containers” and minimize their difference of sum
- There is integer array like {1,2,4,5,6,1,2,4,3,5,7,2,1}. I want to find the possible combination of pair which sum is 4.
- Only the Kotlin standard library is allowed to use the 'kotlin' package
- Write one line C function to find whether a no is power of two
- Partition a set of numbers into two so that difference between their sum is minimum with equal size
- SUMO使用:Vehicle '**' is not allowed to depart on any lane of its first edge.
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
- perfect squares find the least number of perfect square numbers (1, 4, 9, 16, ...) which sum to n
- WARNING: Only bash is supported, use of other shell would lead to erroneous results
- When to use Class.isInstance() & when to use instanceof operator?
- When to use Class.isInstance() & when to use instanceof operator
- Find the number of subsets such that the sum of numbers in the subset is a prime number
- Question 12: In C++, which of the following is the best declaration for an overloaded operator[] to allow read-only access (and
- use MyButton is simple,only add two file and then add some code just bellow in any dlg src declear the button control and will ok!
- The only thing I can think of is to use something like "http://www.dyndns.org/cgi-bin/check_ip.cgi" others may have a be
- Unable to find a value for "tStatus" in object of class org.entity.Passport using operator "."
- [zz]Determine the maximum of two integers using macro without any operators.
- Given the array of digits (0 is also allowed), what is the minimal sum of two integers that are made