Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Fundamentals of Programming

Lab Report 08

Loops Formation Using While Statement, do while


and for statements:

1st SEMESTER

Submitted to: Engr. Ali Hassan


Session: ME-14 Section: B Group:_
SUBMITTED BY
Name CMS Objective Lab Lab
Theory
(1) Work(3) Task Total
(3)
(3)
Malik Haseeb Ullah 428302

School of Mechanical and Manufacturing


Engineering
Objectives:
• Introduction to while statement

• Introduction to do while statements


• Introduction to for statement

Theory:
1. Loops:
Loops can execute a block of code as long as a specified condition is reached. Loops
are handy because they save time, reduce errors and they make code more easy.

2. While loop:
The while loop, loops through a block of code as long as the specified condition is
“true”.
Syntax:
while (condition)
{
code block
}
Do not forget to increase the variable used in the condition otherwise the loop will
never end.
3. do while loop :
It is the variant of the while loop. The loop will execute the code block once, before
checking if the condition is true then it will repeat the loop as long as the condition is
true.
Syntax:
do
{
code block
}
while (condition);

The loop will always be executed at least once, even if the condition is false ,
because the code block is executed before the condition is tested.
Do not forget to increase the variable used in the condition otherwise the loop will
never end.
4. for loop:
when you know exactly how many times you want to loop through a block of code ,
use “for loop” instead of while loop.
Syntax:
for (statement 1; statement 2; statement 3)
{
code block
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.

Lab Work:
Program No 1:
#include<iostream>
using namespace std;
int main()
{int i(5);
while(i<10)
{
cout<<"Imported hakoomat namanzoor\n";
i+=2;
}
cout<<"I love pak army\n";
char n;
cout<<"Please enter r to repeat and e to exit";
cin>>n;
while(n=='r')
{
cout<<"Repeat\n";
}
cout<<"Die";
}
Figure 1Output of program no 1

Program no 2:

#include<iostream>
using namespace std;
int main()
{
int i(5);

while(i<10)
{
cout<<"Imported hakoomat namanzoor\n";
i+=2; //update statement
}

for(int i=5;i<10;i+=2)
{
cout<<"Imported hakoomat namanzoor\n";
}
}
Figure 2 output of program no 2

Program no 3:
#include<iostream>
using namespace std;
int main()
{
for(int i=0,sum=0;i<101;i++)
{
sum=sum+i;
}
}

Figure 3 output of program no 3

Program no 4:

#include<iostream>
#include<iomanip>
#include<string.h>

using namespace std;


int main()
{

string name,father,cnic;
char ch;
do
{
cout<<"\nPlease enter your name";
cin>>name;
cout<<"Please enter your father's name";
cin>>father;
cout<<"Please enter your CNIC";
cin>>cnic;
cout<<"Do you want to enter any other record?y/n:";

cin>>ch;

while(ch=='y'||ch=='Y');
cout<<setw(30)<<"Your Bio data\n";
cout<<"Name"<<name<<endl;
cout<<"Father's Name"<<father<<endl;
cout<<"CNIC"<<cnic<<endl;

cout<<"\nThank you for using our program";

}
Figure 4 output of program no 4

Program No 5:
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
for(int i=0;i<10;i++)
{
for(int a=0;a<10;a++)
{
cout<<"* ";
}
cout<<endl;
}
}

Figure 5 output of program no 5

Program no 6:
#include<iostream>
using namespace std;

int main()
{
int a=5,b=1;

while(b<11)
{
cout<<a<<" love pak army"<<endl;
b=b*2;
}}

Figure 6 Output of program no 6


Program 7:
#include<iostream>
using namespace std;

int main()
{
// Declare and initialize objects
const double V0(0.0); //initial velocity m/s
const double A(2.537); //constant acceleration m/s*s
double time, velocity;
//print heading

cout << " Time, s\tVelocity, m/s\n" ;


time = 0;
while(time <= 10.0)
{
velocity = A*time + V0;
cout << time << "\t\t" << velocity << endl;
++time;
}

return 0;
}

Figure 7 output of Program no 7


Program no 8:

#include<iostream>
using namespace std;

int main()

int b;
for(int a=0;b<10;b=0+(a++))

cout<<b<<endl;

Figure 8 output of program no 8

Program no 9:
#include<iostream>
using namespace std;
int main()
{

int a=0;
while(a<10)
{
cout<<a<<endl;
a++;
}

Figure 9 output of program no 9

Program 10:
#include<iostream>
using namespace std;

int main()
{
char section;
int cc=0;
string name,cmsid,title;
char ch;

do
{
cout<<"Please enter your section";
cin>>section;
cout<<"Please enter name";
cin>>name;
cout<<"Please enter CMSID";
cin>>cmsid;
cout<<"Title?";
cin>>title;
cout<<"Press y to submit again n to exit";
cin>>ch;
cc++;
}while((ch=='y'||ch=='Y')&&cc<=1);
}

Figure 10 output of Program no 10

Program No 11:
#include<iostream>
using namespace std;

int main()
{
int a=0;
while(a<10)
{
cout<<a<<endl;
a++;
if(a==6)
{
break;
}
}
cout<<"tota howa saaz";
}

Figure 11 Output of program no 11

Program no 12:

#include<iostream>
using namespace std;

int main()
{

int sum=0;
for(int a=0;a<=100;a=a+2)
{sum=sum+a;
}
cout<<sum;
}

Figure 12 Output of program no 12

Program no 13:

#include<iostream>
using namespace std;

int main()
{

int sum=0;

for(int a=1;a<=100;a=a+2)
{sum=sum+a;
}
cout<<sum;
}

Figure 13 output of program no 13


Program no 14:
#include<iostream>
using namespace std;

int main()
{

int sum=0;
int a=0;

while(a<=100)
{
sum=sum+a;
a+=2;
}
cout<<sum;
}

Figure 14 output of program no 14

Program no 15:
#include<iostream>
using namespace std;
int main()
{

int sum=0;
int a=1;

while(a<=100)
{
sum=sum+a;
a+=2;
}
cout<<sum;

Figure 15 output of program no 15

Program no 16:
#include<iostream>
using namespace std;

int main()
{

int a=5;
int b=9;

while(b>a)
{
cout<<"Statement";
cout<<"statement";
system("TIMEOUT /T 1");
a=a+1;

}
char ch=' ';
while(isspace(ch))
{cin>>ch;
}
}

Figure 16 Output of Program no 16

Program no 17:
#include<iostream>
using namespace std;
int main()
{

int a=5;
int b=9;
char ch=' ';
while(!isspace(ch));
{
cin>>ch;

Figure 17output of program no 17

Program no 18:
#include<iostream>
using namespace std;
int main()
{
int a=5;
int fact=1;
while(a>0)
{
fact=fact*a;
a--;

}
cout<<fact;
}

Figure 18 output of program no 18

Lab Task:
Program No 1:
#include<iostream>
using namespace std;
int main()
{
cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID = 428302"<<endl;
int a = 2;
int b = 1;
while(a<=12)
{
b=b*a;
a+=2;
}
cout<<"The product of first 12 even numbers in while loop "<<b<<endl;
return 0; }
Figure 19 Output of Program no 1

Program no 2:
#include<iostream>
using namespace std;
int main()
{ cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID = 428302"<<endl;
int a=5;
int factorial=1;
while(a>0) {
fact=fact*a;
a--; }
cout<<"The factorial of 5 using loops is "<<factorial;
return 0; }

Figure 19 Output of program no 2


Program no 3:
#include<iostream>
using namespace std;
int main()
{
cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID = 428302"<<endl;
int a=4;
int b=6;
int c=1 ;
for(int d=1;d<=b;d++)
{
c*=a;
}
cout<<"4 raised to power 6 is "<<c;
return 0;
}

Figure 20 Output of program no 3

Program no 4:
#include <iostream>
using namespace std;
int main()

{
cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID = 428302"<<endl;
int number_1 = 0;
int number_2 = 1;
int number_3;
int i;
int number;
cout << "Enter how many numbers u want to print"<<endl;
cin >> number;
cout<<"Fabonaci series loading....."<<endl;
cout<<number_1<<" ";
cout<<number_2<<" ";
for (i = 0; i <= number; ++i)
{
number_3 = number_1 + number_2;
cout<<number_3<<" ";
number_1 = number_2;
number_2 = number_3;
}
return 0;
}

Output of Program no 4
Program No 5:
#include<iostream>
using namespace std;
int main()
{
cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID = 428302"<<endl;

for (int i=2; i<542; i++) // 0 , 1 and 2 are not prime numbers.
for (int j=2; j<i; j++)
{
if (i % j == 0)
break;
else if (i == j+1)
{
cout << i <<" ";
}

}
return 0;
}

Output of program no 5

You might also like