int n = 9535;
int s = 0;
while (n > 0)
{
s += n % 10;
n /= 10;
}
Replace the while with an equivalent do loop :
+--------+
| /10 |
+--------+
#include <stdio.h>
int main(void)
{
int d,l,n = 4395;
for (l = 0, d = n; d > 0; l++, d /= 10);
printf("%d\n",l);
return 0;
}
-------------------------
| step | d | l |
=========================
| 0 | 4395 | 0 |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
-------------------------
So, what does the program finally print?
+--------+
| /30 |
+--------+
int is_prime ( int n );
/* returns 1 if n is prime, 0 otherwise */
Write the definition of is_prime below :
int is_prime ( int n )
+--------+
| /15 |
+--------+
hat(x) = 0 if x < 1 or x < -1
1+x if x >= -1 and x < 0
1-x if x >= 0 and x < 1
The function hat takes a
float x as input and returns hat(x) as defined above.
Give the definition of hat in C :
+--------+
| /15 |
+--------+
The coins are represented by a sequence of characters, like pnqdpqnnppdqddpp.
Each character stands for a coin:
-------------------------------
| character | meaning | value |
===============================
| p | penny | $0.01 |
| n | nickel | $0.05 |
| d | dime | $0.10 |
| q | quarter | $0.25 |
-------------------------------
If the example input pnqdpqnnppdqddpp is put in the file
/tmp/input and the program has the name coins, then
the program runs like
[jan@galois]% ./coins < /tmp/input
value of coins : $1.36
[jan@galois]%
Observe that the output value is printed with two digits after the
decimal point.
+--------+
| /30 |
+--------+