LAB EXPERIMENT # 09
FAMILIRIZATION WITH FOR STATEMENT
Student Name: M kashif Roll No: 36
Lab Instructor Signatures: Date:
OBJECTIVE
To be familiar with For statement.
THEORY:
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.
Syntax
The syntax of a for loop in C++ is −
for ( init; condition; increment ) {
statement(s);
}
Here is the flow of control in a for loop −
The init step is executed first, and only once. This step allows you to declare and
initialize any loop control variables. You are not required to put a statement here, as long
as a semicolon appears.
Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false,
the body of the loop does not execute and flow of control jumps to the next statement just
after the for loop.
After the body of the for loop executes, the flow of control jumps back up to
the increment statement. This statement can be left blank, as long as a semicolon appears
after the condition.
The condition is now evaluated again. If it is true, the loop executes and the process
repeats itself (body of loop, then increment step, and then again condition). After the
condition becomes false, the for loop terminates.
Flow Diagram
Example#1
#include<iostream>
using namespace std;
int main()
{
int i;
for(i=0;i<=10;i++)
cout<<i<< ;
return 0;
}
Observe the output.
OUTPUT:
Example#2:
#include<iostream>
using namespace std;
int main(0
{
int MAXCOUNT=5;
int num;
int total=0;
for(num=1;num<=MAXCOUNT;num++)
{
Cout<<enter num<<endl;
Cin>>num;
Total=total+num;
}
Cout<<total is<<total;
return 0;
}
Observe the output
OUTPUT:
Assignment:
Task#1: Write a program to find the factorial of the number as an input taken by the
user.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double number, factorial=1;
cout << "Enter a number : " ;
cin >> number;
for (int i = 1; i <= number; i++)
{
factorial *= i;
}
cout << "Factorial : " << factorial << endl;
return 0;
}
Output
Enter a number : 13
Factorial : 6.22702e+009
--------------------------------
Task#2: Write a program that finds prime numbers up to that number which is taken
from user as an input.
#include<iostream>
using namespace std;
int main()
{
int prime,i,n,j;
cout<<"Enter the number : ";
cin>>n;
cout<<"2";
for(i=1;i<=n;i++)
{
for(j=2;j<i;j++)
{
if(i%j == 0)
{
prime = 0;
break;
}
prime = 1;
}
if(prime == 1)
cout<<","<<i;
}
return 0;
}
Output
Enter the number : 33
2,3,5,7,11,13,17,19,23,29,31
--------------------------------
Task#3: Write a program that displays the squares of the numbers from 0 to 10.
#include<iostream>
using namespace std;
int main()
{
int MAXALS=10;
int i;
cout<<"Square of the numbers From 0 to 10 are:"<<endl;
for(i=0;i<=MAXALS;i++)
{
cout<<" "<<i*i;
}
return 0;
}
Output
Square of the numbers From 0 to 10 are:
0 1 4 9 16 25 36 49 64 81 100
--------------------------------
Task#4: Write a program that generates Fibonacci series up to 100.
#include<iostream>
using namespace std;
int main()
{
int MAXALS=100;
int first=0,second=1,next,i;
cout<<first<<","<<second;
for(i=3;i<=MAXALS;i++)
{
next=first+second;
first=second;
second=next;
cout<<", "<<next;
}
return 0;
}
Output
0,1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765,
10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040,
1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169,
63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170,
1836311903, -1323752223, 512559680, -811192543, -298632863, -1109825406, -
1408458269, 1776683621, 368225352, 2144908973, -1781832971, 363076002, -
1418756969, -1055680967, 1820529360, 764848393, -1709589543, -944741150,
1640636603, 695895453, -1958435240, -1262539787, 1073992269, -188547518,
885444751, 696897233, 1582341984, -2015728079, -433386095, 1845853122,
1412467027, -1036647147, 375819880, -660827267, -285007387, -945834654, -
1230842041, 2118290601, 887448560, -1289228135, -401779575, -1691007710, -
2092787285, 511172301, -1581614984, -1070442683, 1642909629, 572466946, -
2079590721, -1507123775, 708252800, -798870975, -90618175, -889489150
--------------------------------