Lab 6 PF 2019cpe27 M - Usama Saghar
Lab 6 PF 2019cpe27 M - Usama Saghar
Lab Manual # 06
CLO: CLO-1
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
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.
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.
While Loop
Syntax:
while(condition)
{
statement(s);
}
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.
#include <iostream>
using namespace std;
int main ()
{
int a = 10;
while( a < 20 )
{
cout << "value of a: " << a << endl;
a++;
}
System("pause");
return 0;
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;
}
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;
}
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;
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++;
}
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 << base << "^" << exponent << " = ";
while (exponent != 0) {
result *= base;
--exponent;
}
return 0;
}
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.