| Exercise 1 | Exercise 2 | Exercise 3 | Exercise 4 | Exercise 5 | |
| Solution 1 | Solution 2 | Solution 3 | Solution 4 | Solution 5 | |
| Show all | Hide all | |||||
Click on the links above
Exercise 1
Write a C program that reads a positive integer n and prints the decimal representation of the integer in reverse order. For example, upon input 72945, your program should print 54927. Do not write a function (well, other than the main() function).
Solution 1
#include <stdio.h> #include <stdlib.h> int main () { unsigned long int n, s; printf("n = "); scanf("%lu", &n); s = 0; while (n != 0) { s = s * 10 + (n % 10); n /= 10; } printf("Reversed integer is %lu\n", s); exit(0); }Exercise 2
Write a C program that finds an approximate value of a definite integral. Let l be the left and r the right boundary for the integral. Also let h be the step-size. The idea is to break the interval [l,r] into sub-intervals [l,l+h], [l+h,l+2h], [l+2h,l+3h], ..., [r-h,r]. Assume that r-l is an integral multiple of h. Your program evaluates the function to be integrated at the center of each interval, multiplies these values by the width h, and computes the sum of these products as the approximate integral. As a concrete realization, integrate the function as x2. The quantities l,r,h should be supplied by the user.
Solution 2
#include <stdio.h> #include <stdlib.h> int main () { double l, r, h, x, s; printf("Enter left boundary : "); scanf("%lf", &l); printf("Enter right boundary : "); scanf("%lf", &r); printf("Enter step size : "); scanf("%lf", &h); s = 0; for (x = l; x < r; x += h) s += h * (x+h/2) * (x+h/2); printf("Integral = %lf\n", s); exit(0); }Exercise 3
Write a C program that reads a sequence of positive integers and outputs the maximum of the integers read. The input integers must not be stored in an array. The number of integers to read is not known a priori. The reading of the integers stops when the user enters an integer <= 0.
Solution 3
#include <stdio.h> #include <stdlib.h> int main () { int max, n; max = 0; do { printf("Input integer: "); scanf("%d", &n); if (n > max) max = n; } while (n > 0); printf("Maximum = %d\n", max); exit(0); }Exercise 4
A positive integer is called a perfect number if it equals the sum of its proper divisors. For example, 6=1+2+3 is a perfect number.
Write a C program that reads a bound B from the user and prints all perfect numbers between 1 and B.
Solution 4
#include <stdio.h> #include <stdlib.h> int main () { unsigned long int B, n, s, d; printf("Enter a bound : "); scanf("%lu", &B); for (n = 1; n <= B; ++n) { s = 0; for (d = 1; d < n; ++d) if (n % d == 0) s += d; if (s == n) { printf("%lu is a perfect number.\n", n); } } exit(0); }Exercise 5
The following C program reads a positive integer n and outputs a sum s. Express s as a (mathematical) function of n.
#include <stdio.h> #include <stdlib.h> int main () { int n, s, i, j; printf("Enter a posiive integer: "); scanf("%d", &n); s = 0; for (i=1; i<=n; ++i) { j = 1; while (j <= i) { ++s; ++j; } while (j <= n) { --s; ++j; } } printf("The computed sum is %d\n", s); exit(0); }Solution 5
For each value of i, the sum s is incremented for j = 1,2,...,i and decremented for j = i+1,i+2,...,n. That is, the total contribution in s for a given i is i-(n-i) = 2i-n. Summing this quantity for all values of i in the range 1,...,n gives the final sum s = (n2+n)- n2 = n.