26 Loop Problems
26 Loop Problems
1. Write a program that displays your name 10 times. (Hint: Use while loop structure)
2. Write a program that take a name and a number, then displays that name the number of times user inputs.
Sample output:
3. Write a program to display numbers exists between 0 and 100 in ascending order (both inclusive). Display
10 numbers in each line.
4. Write a program to display numbers exists between 0 and 100 in descending order (both inclusive). Display
10 numbers in each line.
5. Write a program to display all even numbers exists between 0 and 100 in ascending order (both inclusive).
Display 5 numbers in each line.
6. Write a program to display all odd numbers exists between 0 and 100 in ascending order (both inclusive).
Display 5 numbers in each line.
7. Prompt the user to enter two positive numbers (in any order). Compute and display counting from the lower
number to the higher number both inclusive.
8. Write a program that will print out the numbers from 200 to 150 using any loop, backwards, all on one line.
Print the messages "Starting" and "Done" before and after the line of numbers.
Sample output:
Starting
200 199 198 197 196 195 … 150
Done
9. Prompt a user to enter a Key value and some numbers, you have to find the point at which he inputs that
key value, user enter 0 to specify that no more numbers to input.
10. Input two positive numbers, display even numbers between these two numbers (both inclusive).
12. Write a program that takes ten numbers from user and displays the maximum number. Note: Use only two
variables.
13. Write a program that takes ten numbers from user and displays the minimum number. Note: Use only two
variables.
14. Write a program that takes ten numbers from user and displays the maximum and second maximum
number.
15. Write a program that takes ten numbers from user and displays the minimum and second minimum number.
16. Write a program that takes inputs as an integer and displays its 1st ten multiples.
17. Write a program to calculate SUM of 1st ten integers using any loop.
18. Write a program to calculate the sum and average of 1st ten integers with the help of at most 2 variables.
19. Write a program that keeps printing multiples of 2, your loop should not terminate. See what happen
20. Prompt the user to enter two parameters, one will be the floating number and the other one will be the
power of that number in integer form. The program performs some calculation and of the display the power
of that particular number as specified by the user.
21. Write a program that prompts the user to enter a temperature in degrees Centigrade, calculate the
corresponding temperature in degrees Fahrenheit and finally prints the given temperature and the converted
value. The user enters 0 to indicate that he has no more numbers to enter.
22. Given a number, write a program using while loop to reverse the digits of the number.
23. Identify and print the name and age of the oldest student and the youngest student in a class. The input
records contain the name and age of the students. Assume the sentinel value of 99 for the age field of the
trailer record.
24. A set of examination papers graded with scores from 0 to 100 is to be searched to find how many of them
are above 90. The total has to be printed. Assume a suitable sentinel value for the trailer record.
2 of 4
Arif Butt
25. Write a program that will take three values of integer type from user and display the table of the required
value from the starting limit to ending limit. The first parameter will be the table value, second will be the
starting limit and third will be the ending limit.
26. The process of finding the smallest number is used frequently in computer applications. For example, a
program that determines the loser of a sales contest would input the number of units sold by each
salesperson. The salesperson who sells the lowest units lost the contest. Write a program that input a series
of 10 numbers, and determine and prints the smallest of the numbers.
¾ counter: Counters to count to 10, keep track of how many numbers have been input, and to
determine when all 10 numbers have been processed
¾ number: The current number input to the program
¾ smallest: The smallest number found so far
27. Write a program that reads two integers and computes and display the GCD of them(Hint: Repeatedly
subtract the smaller number from larger number until you end up with zero.)
28. The first two numbers of Fibonacci series are zero and one. Every next number is generated by adding the
previous two. Write programs that takes an input N then prints:
29. In mathematics, the factorial of a positive integer n, denoted by n!, is the product of all positive integers
less than or equal to n. For example, 5! = 1 x 2 x 3 x 4 x 5 = 120. 0! is a special case that is explicitly
defined to be 1.
So write a program that reads a positive integer and computes and display its factorial. Your program
should place a proper check if user entered a negative number. After each calculation, the program should
ask the user either he/she wants to continue or not, the program should continue its execution unless user
entered n.
31. A user is asked to enter a set of positive numbers, one at a time. The user enters a 0 to indicate that he has
no more numbers to enter.
3 of 4
Arif Butt
32. Write a program that generates arithmetic sequence; for this please take first term (a1), common difference
(d) & number of terms (n) from user.
33. Write a program that generates geometric sequence; for this please take first term (a1), common ratio (r) &
number of terms (n) from user.
34. A class of n students take an annual examination in m subjects. Write a program to read the marks obtained
by each student in various subjects and to compute and print the total marks obtained by each of them.
35. Write a program that takes obtained marks of five courses of student and calculates and displays his/her
GPA according to the following Grading System. Also mention that whether the student is on probation or
not. Repeat the input until user type ‘0’ to mention no more student’s input is left.
4 of 4
Arif Butt