You are on page 1of 13

Lab # 6– Programming Fundamentals (CPE-121)

Lab Manual # 06

Title: Introduction to loops and implementation of While Loop IN C ++

CLO: CLO-1

Student Name: M.USAMA saghar Class Roll: 2019-CPE-27

Subject Teacher : Engr. Muhammad Baqer


Lab Engineer : Engr. Abdul Rehman
Lab Performed In : Networking Lab (CPED)

LAB POINTS SCORE


Lab Performance [10] 0 1 2 3 4 5
Lab Participation
Lab Activity Completion on the Same Day

Lab Submission[10] 0 1 2 3 4 5
Completeness & Correctness
Required Conclusion & Results

No of Checks
SUB TOTAL
TOTAL SCORE

______________________
Course Instructor / Lab Engineer

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 6– Programming Fundamentals (CPE-121)
C++ Loop Types

There may be a situation, when you need to execute a block of code several number of
times. In general statements are executed sequentially: The first statement in a function
is executed first, followed by the second, and so on. Programming languages provide
various control structures that allow for more complicated execution paths. A loop statement
allows us to execute a statement or group of statements multiple times and following is the
general from of a loop statement in most of the programming languages:

C++ programming language provides the following types of loop to handle looping
requirements.

Loop Type Description

While loop Repeats a statement or group of statements while a


given condition is true. It tests the condition before
executing the loop body.

For loop Execute a sequence of statements multiple times and


abbreviates the code that manages the loop variable.

DO....While loop Like a while statement, except that it tests the condition
at the end of the loop body

Nested loops You can use one or more loop inside any another while,
for or do..while loop.

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 6– Programming Fundamentals (CPE-121)

While Loop

A while loop statement repeatedly executes a target statement as long as a given condition is


true.

Syntax:

The syntax of a while loop in C++ is:

while(condition)
{
statement(s);
}

Here, statement(s) may be a single statement or a block of statements. The condition may be


any expression, and true is any non-zero value. The loop iterates while the condition is true.
When the condition becomes false, program control passes to the line immediately following
the loop.

Flow Diagram:

Here, key point of the while loop is that the loop might not ever run. When the condition is
tested and the result is false, the loop body will be skipped and the first statement after the
while loop will be executed.

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 6– Programming Fundamentals (CPE-121)
Example:

#include <iostream>
using namespace std;
int main ()
{
int a = 10;
while( a < 20 )
{
cout << "value of a: " << a << endl;
a++;
}
System("pause");
return 0;

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 6– Programming Fundamentals (CPE-121)

P-1 Write a Program in C++ to find factorial of a number entered by uses (Factorial of n =
1*2*3...*n)
Your Code:

#include <iostream>
using namespace std;
int main()
{
int i,fact=1,number;
cout<<"Enter any Number: ";
cin>>number;
for(i=1;i<=number;i++){
fact=fact*i;
}
cout<<"Factorial of " <<number<<" is: "<<fact<<endl;
return 0;
}

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 6– Programming Fundamentals (CPE-121)

Paste your output here

P-2 Write a program that will print table of a number entered by user
Your Code:

#include<iostream>
using namespace std;
int main()
{
int a,b,c,i;
cout<<"Enter table number ";
cin>>a;
cout<<"Enter starting number ";
cin>>b;
cout<<"Enter span number ";
cin>>c;

for(i=b;i<=c;++i)
{
cout<<a<<" * "<<i<<" = "<<a*i<<endl;
}
return 0;
}

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 6– Programming Fundamentals (CPE-121)

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 6– Programming Fundamentals (CPE-121)

P-3 Write a program that will calculate sum of first fifty natural numbers
Your Code:

#include <iostream>
using namespace std;

int main()
{
int sum = 0;

for (int i = 1; i <= 50; ++i) {


sum += i;
}

cout << "Sum of first 50 natural numbers is = " << sum;


return 0;

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 6– Programming Fundamentals (CPE-121)
}

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 6– Programming Fundamentals (CPE-121)

P-4 Write a program that will execute a while loop number of times that is entered by
user.
Your Code:

#include <iostream>
using namespace std;

int main()
{
int a,sum = 0,i;
cout<<"how many time do you want to excute while loop: ";
cin>>a;
while (i<=a)
{
sum=sum+1;
i++;
}

cout << "Loop Counter: " << sum;


return 0;
}

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 6– Programming Fundamentals (CPE-121)

Paste your output here

P-5 Write a program that will calculate power of two numbers entered by user first as base and
2 nd as power
Your Code:

#include <iostream>
using namespace std;

int main()
{
int exponent;
float base, result = 1;

cout << "Enter base and Power respectively: ";


cin >> base >> exponent;

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 6– Programming Fundamentals (CPE-121)

cout << base << "^" << exponent << " = ";

while (exponent != 0) {
result *= base;
--exponent;
}

cout << result;

return 0;
}

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 6– Programming Fundamentals (CPE-121)

Comments:
In this manual I have learnt that how to use loops i.e. for loop, while loop.
Moreover, I learnt some programs using loops.

Version 1.0 Department of Computer Engineering UCE&T BZU Multan

You might also like