C Programming
© Copyright Brian
Brown/Peter Henry, 1984-1999. All rights reserved.
| Notes | Tests
| Home Page |
Sample Programming Problems
1. Enter 2 numbers and print their sum.
2. Enter 3 numbers and print their average.
3. Enter the temperature in Centigrade, convert it to Fahrenheit and
print it out.
To convert to Fahrenheit, mulitply by 1.8 and add 32.
E.g. 10C = 10*1.8+32 = 50F
4. Enter your height in feet and inches and convert to metres, given
1 inch equals
0.0254 metres. Print out your height in metres.
5. Enter 2 numbers and print out the largest.
6. Enter 3 numbers and print out the smallest.
7. A salesman is paid a commission on the following basis
Sale Value | Commission |
up to $100 | zero |
over $100 to $1000 | 2% |
over $1000 | 3% |
Enter the sale value and print out the commission value (use a maximum entry value of $32000)
8. Hutt Valley Energy charges its customers for electricity as follows.
Kilowatt-Hours | Cost($) |
0 to 500 | 10 |
501 to 1000 | 10 + 0.05 for each kwh over 500 |
over 1000 | 35 + 0.03 for each kwh above 1000 |
Enter the meter reading, calculate the cost and print out how much is charged.
9. Print 8 asterisks (*) down the page.
Output
*
*
*
*
*
*
*
*
10. Enter a character and a number and print that number of copies of that character.
Example Input
Enter a character: F
Enter a number: 4
F
F
F
F
11. Enter a list of numbers, print the number and continue until the
number is greater
than 10.
Example Input
Enter a number: 5
The number is 5
Enter a number: 3
The number is 3
Enter a number: 7
The number is 7
Enter a number: 11
12. Enter a list of numbers, print out that number of asterisks (*)
on that line until
the number entered is 0.
Example Input
Enter a number: 5
*****
Enter a number: 1
*
Enter a number: 0
13. Enter a number between 2 and 20 and print a filled square with
sides of that number
of asterisks (*).
Example Input
Enter a number: 3
***
***
***
14. Enter a number and print out its multiplication table from 1 to 10.
Example Input
Enter a number: 3
1 times 3 = 3
2 times 3 = 6
.
.
10 times 3 = 30
15. Enter a list of 5 numbers and print their total.
16. Enter a list of numbers terminated by a -1 and print their total.
17. Enter a list of numbers terminated by a -1 and print how many
numbers
where entered in.
18. Enter a list of numbrs terminated by a -1 and print the smallest
number
(assume all numbers entered are positive).
19. Enter a list of names terminated by a Z, and print out the alphabetically smallest.
20. Enter a string of results for a History exam terminated by a -1.
The pass mark
is 50. Print the number of passes and the
number of fails.
21. Input the time started and finished at work in hours and minutes,
then print out
the time spent at work in hours and minutes.
Example Input
Enter start time: 8 30
Enter finish time: 11 15
2 hours 45 minutes
22. Enter a persons weight in kilograms and height in metres.
Calculate the persons
Quetelet Index (kilos / (metres*metres) ). Print out the Quetelet Index and an
appropriate message as indicated by the table below.
Below 20 | Underweight |
20 to below 25 | Healthy weight |
25 to below 30 | Mildly overweight |
30 to below 40 | Very overweight |
40 and above | Extremely overweight |
23. Enter a list of numbers terminated by a -1 and print the
difference between each pair
of numbers.
Example Input
Enter a number: 3
Enter a number: 5
Difference is 2
Enter a number: 6
Difference is 1
Enter a number: 10
Difference is 4
Enter a number: -1
24. Enter 2 numbers and print them out. Then print the next 13
numbers in the sequence, where
the next number is the sum of the previous two.
Example Input
Enter the first number: 1
Enter the second number: 3
1 3 4 7 11 18 29 47 76 123 322 521 843 1364
25. Enter your name and a number and print that number of copies of your name.
Example Input
Enter your name: Fred
Enter a number: 4
Fred
Fred
Fred
Fred
26. Enter your name, convert to uppercase, reverse and print.
Example Input
Enter your name: Fred
DERF
27. Input a sentence, count and print the number of spaces.
Example Input
Enter the sentence: A cat sat on the mat.
Number of spaces = 5
28. Input a sentence and print one word per line. Assume one space
between words
and the sentence is terminated with a period.
Example Input
Enter the sentence: A cat sat on the mat.
A
cat
sat
on
the
mat
29. Input a sentence and print out if it is a palidrome
Example Input
Enter the sentence: Madam I'm Adam
It is a palidrome
Example Input
Enter the sentence: Fred
Not a palidrome
30. Input ten numbers into an array, and print the numbers in reverse order.
Example Input
Enter the numbers: 5 4 6 7 22 19 12 15 2 1
1 2 15 12 19 22 7 6 4 5
31. Input ten numbers into an array, and print these 3 times.
Example Input
Enter the numbers: 5 4 6 7 22 19 12 15 2 1
5 4 6 7 22 19 12 15 2 1
5 4 6 7 22 19 12 15 2 1
5 4 6 7 22 19 12 15 2 1
32. Input ten numbers into an array, calculate and print the average,
and print
out those values below the average.
Example Input
Enter the numbers: 5 4 6 7 22 19 12 15 2 1
Average = 9.3
Those numbers below the average 5 4 6 7 2 1
33. Input ten numbers into an array, and print out the largest.
Example Input
Enter the numbers: 5 4 6 7 22 19 12 15 2 1
The largest number is 22
34. Input ten numbers into an array, using values of 0 to 99, and
print out
all numbers except for the largest number..
Example Input
Enter the numbers: 5 4 6 7 22 19 12 15 2 1
5 4 6 7 19 12 15 2 1
35. Input ten numbers into an array, using values of 0 to 99, and
print the
values in ascending order.
Example Input
Enter the numbers: 5 4 6 7 22 19 12 15 2 1
1 2 4 5 6 7 12 15 19 22
© Copyright Brian Brown/Peter Henry, 1984-1999. All rights reserved.