0% found this document useful (0 votes)
223 views

26 Loop Problems

The document contains 35 programming problems involving repetition structures like loops. The problems cover a range of concepts like displaying output multiple times, counting, summing values, finding maximum/minimum/odd/even numbers, factorials, Fibonacci sequences, and more. Loops like while and for are to be used to solve the problems. Sample outputs are provided for some problems.

Uploaded by

Abdul Munim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
223 views

26 Loop Problems

The document contains 35 programming problems involving repetition structures like loops. The problems cover a range of concepts like displaying output multiple times, counting, summing values, finding maximum/minimum/odd/even numbers, factorials, Fibonacci sequences, and more. Loops like while and for are to be used to solve the problems. Sample outputs are provided for some problems.

Uploaded by

Abdul Munim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Repetition 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:

Enter your name: Dilawer Hussain.


Enter display number: 3
Hello! Dilawer Hussain
Hello! Dilawer Hussain
Hello! Dilawer Hussain

3. Write a program to display numbers exists between 0 and 100 in ascending order (both inclusive). Display
10 numbers in each line.

a. Write this program using while loop structure


b. Write this program using for loop structure

4. Write a program to display numbers exists between 0 and 100 in descending order (both inclusive). Display
10 numbers in each line.

a. Write this program using while loop structure


b. Write this program using for loop structure

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.

a. Write this program using while loop structure


b. Write this program using for loop structure

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.

a. Write this program using while loop structure


b. Write this program using for loop structure

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.

Output should be:


Enter Key : 45
1 of 4 
    Arif Butt 
 
Enter Num: 4
Enter Num: 15
Enter Num: 92
Enter Num: 45
Enter Num: 77
Enter Num: 0
Key is Entered at position 4.

10. Input two positive numbers, display even numbers between these two numbers (both inclusive).

11. Generate print the following series: 1, 3, 9, 27, 81, …, <1000

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.

For example, the number


12345
Should be written as
54321

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.

Hint: your program should use three variables as follows

¾ 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:

a. A series of N Fibonacci numbers. [0, 1, 1, 2, 3, 5, 8, 13, 21, …]


b. Nth Fibonacci number.
c. The sum of N Fibonacci numbers.

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.

30. A user is asked to enter a positive number N, determine

a. Whether or not N is a perfect square.


b. Calculate and print N! (Factorial).
c. Calculate the sum of numbers starting from 1 to N. (e.g. if N = 4, then return 10)
d. Calculate the square of numbers starting from 1 to N.(e.g. if N = 4, then return 30)
e. Whether or not N is prime (To get full credit give an optimized algorithm)

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.

a. Write a program to print the average of all the numbers.


b. Write a program to print the largest number entered.
c. Write a program to print the smallest number entered.
d. Write a program to print the largest as well as the smallest number entered.
e. Write a program to print the second largest number entered.

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.

Sample Output: (for d = 3) 1, 4, 7, 10, 13, 16, 19,

33. Write a program that generates geometric sequence; for this please take first term (a1), common ratio (r) &
number of terms (n) from user.

Sample Output: (for r = 2) 1, 2, 4, 8, 16, 32, 64,


Sample Output: (for r = 2/3) 1, 2/3, 4/9, 8/27, 16/81,

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.

GRADING SYSTEM for fall 2010 batch


PERCENT MARKS GRADE POINTS
85-100 4.00
80-84 3.70
75-79 3.30
70-74 3.00
65-69 2.70
61-64 2.30
58-60 2.00
55-57 1.70
50-54 1.00
Below 50 0.00
A student attains Probation Status if his/her CGPA becomes 1.70 or more but less than 2.00.

4 of 4 
    Arif Butt 
 

You might also like