html>C-ohjelmointi, laskuharj. 1

C-programming Fall 1999

Problems for Exercise 2 (27.9.-1.10.1999)

Write your C-programs from the very beginning with a computer. Points will be given only for solutions proven by a computer. Make sure of the correct functioning by compiling your program, correcting possible errors and by running program, if necessary using different test inputs. Avoid using, in your solutions, such features that have not been explained in the course.

1. Write a program that calculates the annual instalments and the interest on a loan charged by a bank. The instalment of the debt is the same in each year, and the instalment is payed once a year. The program asks the user to give the loan sum in the beginning, the interest rate and the time to pay the loan.

2. Write a function int samat (eka[], toka[]) that checks if the string eka is the same than the string toka. The function returns 1 if the strings are same and 0 if they are not.

3. Write a function int lueluku() that reads from the input an integer given as digits ( for example 200, 153, 32) and returns the value of the integer. If the input string is not an integer the function returns -1.Your function should be able to read integers up to 8 digits.

4. Write a function int alkuluku(int luku) that tells if the number given as parameter is a prime number or not. A number is a prime if it is divisible only by itself and 1. Write a main program that prints prime numbers between 1-n, where n is read from input. You can use either your function from problem 3 or the example functions from the course book for reading. You can expect n to be of type int.

5. Write the function int itob(int n, char s[], int b) that converts the interger n into a base b character representation in the string s. The base b is between 2-36. The function returns the length of the written string or -1 if the base was illegal. Then write a prgram that prints a number from its input in all bases between 2-36.

6. Write a program that reads two datums in the form dd.mm.yyyy and calculates the difference between them in days. Your solution should also take into account the leap years. A year is a leap year if is divisiple by 4. However from years divisible by 100 only years divisible by 400 are leap years.