Documente Academic
Documente Profesional
Documente Cultură
Example
Int sum=0;
For (int i=1; i<=n; i++)
{
Sum=sum+I;
Count<<I;
}
(n + 2) – 1 for testing condition i.e. i<=n // n +1
3*(n +1) –1 for loop body // 3*(n )
Continue …
Example
Int sum=0;
For (int i=2; i<n; i++)
{
Sum=sum+i;
Count<<i;
}
(n + 1) – 2 for testing condition i.e. i<=n // n -1
3*(n ) –2 for loop body // 3n - 2
While statement
Example
int i=1;
While (i<=n)
{
X++;
I++;
}
(n + 2) – (1 ) for testing condition //n + 1
2 *(n + 1) – ( 1 ) for loop body // 2n + 1
Continue …
Example
int i=1;
While (i<n)
{
X++;
I++;
}
(n + 1) – (1 ) for testing condition //n
2 *(n ) – ( 1 ) for loop body // 2n – 1
Do …. While statement
General formula :
(n + 1) – ( initial value of i ) for testing condition
No. Operation *(n + 1 ) – ( initial value of i ) for loop body
Restriction :– initial value of i must be less than n
If initial value of i is greater than n there will be only (one
operation)*(No. operation in the loop ) for loop body and also one
operation for testing condition
Continue ….
Example
int i=0;
do
{
X++;
I++;
} While (i<=n);
(n + 1) – (0 ) for testing condition //n + 1
2 *(n + 1) – ( 0) for loop body // 2n +2
Continue …
Example
int i=1;
do
{
X++;
I++;
} While (i<n);
(n ) – (1 ) for testing condition //n -1
2 *(n ) – ( 1) for loop body // 2n -1
Exercises
2.
Void myfun()
{
Int x=0;
Int i=8;
Int j=5;
Cout<<” Enter an integer ”;
Cin>>n;
While (i<n)
{
X++;
I++;
}
While(j<n)
{
J++;
}
}
Continue ….
3.
Int sum( int n)
{
Int psum=0;
For(int i=1; i<=n; i++)
{
Psum+=(i*i*i);
If(psum%2)
{
Psum++;
i--;
}
Else
Psum--;
}
Return psum;
}