Click on any exercise below to see the source code, program output and explanation of the code.  Programs in this section make use of functions, arrays and pointers.

In C, functions are building blocks from which larger programs are constructed. They provide a convenient way to encapsulate some computation, which can be used without worrying about its implementation.

An array in C is a composite data structure comprising items of the same type. An array can be one-dimensional, two-dimensional or multi-dimensional.

Pointers are used to access memory and manipulate memory addresses.

Available Exercises

Exercise 3.1. Write a program which, given a positive integer n, returns 1 if n is even and 0 if n is odd

Exercise 3.2.Write a program which requests two integers, m and n, and produces a table of squares from m to n inclusive.

Exercise 3.3.Write a program which converts an integer into Roman numerals.

Exercise 3.4.Write a program which, given a positive integer n, returns 1 if n is prime and 0 otherwise.

Exercise 3.5.Write a function which, given a positive integer n, prints the nth Fibonacci number.
   f(n) = f(n - 1) + f(n - 2).   F(1) = f(2) = 1

Exercise 3.6. Write a program which, given a positive integer n, returns the value of n!
   n! = n*(n – 1)!   0! = 1

Exercise 3.7. Write a program which, given a positive integer n and a real number x, returns the value of ex.
   Ex = 1 + x/1 + x2/2! +...+xn/n!

Exercise 3.8. Write a program which, given an integer array and its size, determines whether the array contains any value that is repeated at least once.

Exercise 3.9. Write a program to count the vowels and letters in free text given as standard input. Read a text character at a time until you encounter end-of-data.

Exercise 3.10. Write a function which gets the next input line (up to the next \n) and stores the result in a character array.

Exercise 3.11. Write a function which, given a string, returns 0 if all its letters are distinct otherwise returns 1.

Exercise 3.12. Write a function which, given a string and an integer n, adds n to all the characters of the string.

Exercise 3.13. Write a function which, given a string containing roman numerals, returns the equivalent decimal integer or -1 if the string contains an invalid character.

Exercise 3.14. Write a function which, given 2 strings, determines whether one is an anagram of the other i.e. one string is a permutation of characters in the other e.g. team and mate.

Exercise 3.15. Write a C program which reads a text, and print it out one word per line, all punctuation and non-alpha characters being omitted. When typing input, end the data by typing the end-of-file character usually control+D.

Exercise 3.16. Write a recursive function which returns the gcd of two integers m and n given that
   gcd(n, m) = gcd(n, m mod n)

Exercise 3.17. Write a function which interchanges the value of two integers passed to it.

Exercise 3.18. Write a program to read an integer from 1 to 999 and print the integer in words. E.g if 897 is read, the program prints eight hundred and ninety-seven.

Exercise 3.19. Write a function which, given a number, prints it digit by digit with 1 blank space between digits.

Exercise 3.20. Write a program to read two names and print them in alphabetic order.

Exercise 3.21. Write a program which, given a string, converts all lower case letters to upper case and leaves the others unchanged.

Exercise 3.22. Write a program to read English text to end­-of-­data (type control-­D to indicate end of data at a terminal, see below for detecting it), and print a count of word lengths, i.e. the total number of words of length 1 which occurred, the number of length 2, and so on. Define a word to be a sequence of alphabetic characters. You should allow for word lengths up to 25 letters.
Typical output should be like this:

length 1 : 10 occurrences
length 2 : 19 occurrences
length 3 : 127 occurrences
length 4 : 0 occurrences
length 5 : 18 occurrences

Exercise 3.23. What is the difference between passing an argument by value and passing an argument by reference?

Exercise 3.24. Write a function sum such that the call sum(a,b,c) will store the sum of a and b in c

Exercise 3.25. Write a program to find out for a class of students, the number of families with 1, 2, 3, … up to 7 children. Data consists of number of children in each student's family. Design an appropriate format for the output.

Exercise 3.26. What is the difference between character pointer and an array of characters.

Exercise 3.27. What happens when a pointer msgPtr which points to a string is assigned the value of a different pointer oldPtr?

Exercise 3.28. Write a program which drills a user in English - French vocabulary. Two arrays, english and french are used to hold the words such that french[i] contains the French equivalent of the word in english[i]. The program must display an English word and requests the user to enter the French equivalent for it. Incorporate a suitable scoring mechanism and give the user the opportunity to "give­ up". Modify the program to enable the user choose either English - French drill or French - English drill.

Exercise 3.29. Write a function “replace” which takes a pointer to a string as a parameter, which replaces all spaces in that string by minus signs, and delivers the number of spaces it replaced. Thus char *cat = "The cat sat"; n = replace( cat ); should set cat to "The-cat-sat" and n to 2.

Exercise 3.30. Write a recursive function which given an integer n, prints it with its digits reversed. E.g given 23998, it prints 89923

Exercise 3.31. Write a function which, given an integer, returns the number of 1­bits in its representation.

Exercise 3.32. The ASCII character set uses 7 bits for storing characters. When such a character is stored in a byte, the 8 th bit (high­order) is used as a parity bit in the even­parity scheme. i.e the bit is set such that the number of 1­s in the byte are even. Write a function which accepts the character and sets the parity bit for even parity.

Exercise 3.33. Write a function toupper which converts its char argument, ch to upper case if ch is lower case and leaves it unchanged otherwise.

Exercise 3.34. Write a function tolower which converts its char argument, ch to lower case if ch is uppercase and leaves it unchanged otherwise.