LAB 5 while loop
LAB 5 while loop
Lab 5
Teacher: Dr Muhammad Tufail
5.1. Objectives
The objectives of this lab are:
• Learn about different applications to use while loop.
5.2. Pre-Lab
While Loop
Loops are used to run a block of code or statements repeatedly. “While” Loops (is a
type of loop used in c++) can execute a block of code as long as a specified
condition is reached.
Syntax
while (condition) {
// code to be executed until condition remains is true
}
You can use also if-else statements within while loop
5.3. Tasks
Task 01:
Use a loop to keep asking the use to enter positive numbers until the user enters a
negative number. In the body of the loop, the program should calculate the total
number of positive numbers, their average and the maximum number.
Sample output:
Task-02
Write a program to print the following series up to N term, the value of N should
be acquired from the user. (understand the series by estimating the next term)
1 3 7 15 31. . .
Sample Output:
Task-03
Write a program that lets user enter an integer value between 1 and 10, the program
validates the input, if the value entered is between 1 and 10 the program prints the
value entered otherwise the program takes the input again , the program ends on
legal values otherwise keep taking input.
Sample Output
Enter a number between 1 and 10 --> 56
Number is outside range 1-10. Please re-enter
Enter a number between 1 and 10 --> 6
The number is 6
Task-04
Write a program in C to display the first n terms of Fibonacci series. In mathematics,
the Fibonacci sequence is a sequence in which each number is the sum of the two
preceding ones. (Hint: 1st two terms are 0 and 1 and next term is obtained by adding
previous two terms)
Your program may look like this
Number of terms to display: 10
Fibonacci series up to 10 terms: 0 1 1 2 3 5 8 13 21 34 .
Sample Output:
After entering the number of terms program will calculate the Fabonacci series upto
the given terms
Task-05
By using % operator and for loop perform the following activities
a) Write a program that gets a number N from user and prints all positive divisors
b) Write a program that gets a number P from user, the program will check
whether the number is prime or not
Task-06
Write a program to print even numbers between two given numbers A and B such
that A can be greater or smaller than B.