1. Write a program that reads two integers and prints the sum of the
integers, the difference of the integers, the product of the integers,
the quotient of the first integer divided by the second (discarding the
remainder), the remainder of the first integer divided by the second,
the smaller integer, and the larger integer. Your program must print
these results in the order listed here. You may assume that the integers
are between 1 and 1000.
For example, if the user types 10 3, your program will print 13 7 30 3 1
3 10, in that order.
2. Write a program that reads a sequence of integers, discards the
negative integers, and prints the sum of the remaining integers. You may
assume that the integers are between -10000 and 10000, and that there
are at most 10000 integers.
For example, if the user types 3 -1 -4 1 -5 9 2 -6 ^D, your program will
print 15.
3. Write a program that reads a sequence of integers and prints the last
5 integers. You may assume that each integer is between 0 and 99. You
may assume that there are at least 5 integers.
For example, if the user types 37 82 59 84 87 26 1 x, your program will
display 59 84 87 26 1.
4. Write a program that reads a sequence of integers and prints the last
5 integers. You may assume that each integer is between 0 and 99. There
may be fewer than 5 integers; in that case, print all the integers.
For example, if the user types 37 82 59 84 87 26 1 ^D, your program will
display 59 84 87 26 1. If the user types 37 82 59 y, your program will
display 37 82 59.
(This is a more difficult version of the previous problem.)
5. Write a program that reads a series of printing commands and follows
the commands. A printing command is an integer followed by a character;
following the command means printing the character several times, the
number of times being given by the integer. Your program must stop if
the integer is 0, or if no integer is available, or if no character is
available.
For example, if the user types 1H1e2l1o0x, your program will print
Hello. If the user types 13x0-5y, your program will print xxxxxxxxxxxxx.
6. What does the following program print?
main()
{
int i;
for (i = 10;i > 0;i /= 2)
printf("%d\n",i);
}
7. What does the following program print?
#include
int f(int n)
{
printf("f %d\n",n);
return n + n;
}
int g(int n)
{
printf("g %d\n",n);
return n + 3;
}
int main(void)
{
printf("main %d\n",f(g(7)));
return 0;
}
8. What does the following program print?
#include
int f(int n)
{
int i;
i = 2;
for (;;) {
printf("%d %d\n",n,i);
if (i * i >= n) break;
if ((n % i) == 0) return 0;
++i;
}
return 1;
}
main()
{
int m;
for (m = 0;m < 10;++m) {
printf("%d %d\n",m,f(m));
}
}
9. What does the following program print?
#include
int main()
{
int i;
int j;
for (i = 0;i < 3;++i)
for (j = 0;j < 3;++j)
switch (i - j) {
default:
printf("%d %d default\n",i,j);
case -1:
printf("%d %d -1\n",i,j);
break;
case 1:
printf("%d %d 1\n",i,j);
continue;
}
printf("%d %d\n",i,j);
}
10. The user runs the following program and types 5. What does the
program print?
#include
int main(void)
{
int i;
int j;
int n;
scanf("%d",&n);
for (i = 0;i < n;++i) {
for (j = 0;j < n;++j)
if ((i + j) % 2)
printf("-");
else
printf("+");
printf("\n");
}
return 0;
}