You are on page 1of 3

LAB NO.

4
LOOP STATMENTS II-WHILE LOOP

OBJECTIVE

▪ While Loop

DESCRIPTION

The “while” Loop

It is used to execute a statement or a set of statements as long as the given condition remains
true. The syntax of while loop is:

while (condition)
Statement;

Example

#include <iostream.h>
main()
{
int n = 1;
while(n<=5)
{cout<< “Hello”<<endl;
n=n+1;
}
cout<< “Program Ends”;}

Analysis

A while loop is a control flow statement that allows code to be executed repeatedly based on a
given boolean condition. The while loop can be thought of as a repeating if statement.

Understanding of basic concept of while loop. The while construct allows for repetitive
execution of a list of commands, as long as the command controlling the while loop executes
successfully (exit status of zero).
LAB TASKS:

Task1

Write a program using while loop which displays alphabets from Z-A.

Task2

Write a program to calculate factorial of a number using while loop. The output of the program
should be like:

Task3

Write a program to print natural numbers 1 to 20 in descending order. The output of the program
should be like:

Task4

Write a program to find the sum of the following series.


1+1/2+1/3+…..1/45
The output of the program should be like:

Task5
Write a program to find the average of N numbers using while loop. The output of the program
should be like:

Task6

Write a program that converts a number into its equivalent binary number using while loop.

Task7

Write a program to display following output using “while” loop

Deliverables:
1. Task1.cpp
2. Task2.cpp
3. Task3.cpp
4. Task4.cpp
5. Task5.cpp
6. Task6.cpp
7. Task7.cpp

You might also like