Professional Documents
Culture Documents
Assignment No 8
Assignment No 8
Lab Report 08
1st SEMESTER
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;
}
}
Program no 4:
#include<iostream>
#include<iomanip>
#include<string.h>
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;
}
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;
}
}
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;
}}
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
return 0;
}
#include<iostream>
using namespace std;
int main()
int b;
for(int a=0;b<10;b=0+(a++))
cout<<b<<endl;
Program no 9:
#include<iostream>
using namespace std;
int main()
{
int a=0;
while(a<10)
{
cout<<a<<endl;
a++;
}
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);
}
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";
}
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;
}
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;
}
int main()
{
int sum=0;
int a=0;
while(a<=100)
{
sum=sum+a;
a+=2;
}
cout<<sum;
}
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;
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;
}
}
Program no 17:
#include<iostream>
using namespace std;
int main()
{
int a=5;
int b=9;
char ch=' ';
while(!isspace(ch));
{
cin>>ch;
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;
}
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; }
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