C Programming Iteration in C
C Programming Iteration in C
Iteration in C
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2
Example III
Sn = 1 + 2 + 3 + · · · + n,
where n is an input.
The best way to do it is to use the closed form
or the formula
n(n + 1)
Sn = .
2
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 3
#include <stdio.h>
int main() // temp26a.c
{
int n;
printf("Enter a +ve integer: ");
scanf("%d", &n);
printf("1+ ...+%d = %d\n",
n, n*(n+1)/2);
return 0;
}
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 4
$ cc -Wall temp26a.c
$ ./a.out
Enter a +ve integer: 5
1+ ...+5 = 15
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5
An Alternate Way
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 6
read n; sum = 0;
5 0
n sum
sum = n + sum; −−n;
4 5
n sum
sum = n + sum; −−n;
3 9
n sum
0 15
n sum
if (n == 0) print sum
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 7
#include <stdio.h>
int main() // temp26.c
{
int n, sum = 0;
printf("Enter a +ve integer: ");
scanf("%d", &n);
while(n > 0) {
sum = n + sum ;
--n ;
}
printf("sum: %d\n", sum);
return 0;
}
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 8
$ cc -Wall temp26.c
$ ./a.out
Enter a +ve integer: 5
sum: 15
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 9
Note
The code
sum = n + sum ;
--n ;
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 10
while Statement
while (expression) statement
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 11
while Loop
Initialization
statement
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 12
Note
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 13
while(n>0) sum += n-- ;
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 14
Note
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 15
#include <stdio.h>
int main() // temp27.c
{
int n, sum = 0, m;
printf("Enter a +ve integer: ");
scanf("%d", &n);
m = n ; // save the value
while(n > 0) {
sum = n*n*n*n + sum ;
--n ;
}
printf("1^4 + ... + %d^4 = %d\n", m, sum);
return 0;
}
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 16
Example IV
Sn = 1c + 2c + 3c + · · · + nc ,
where n and c are input.
We use nested while-loops to solve the
problem.
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 17
#include <stdio.h>
int main() // temp28.c
{
int n, c, sum = 0, m;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Enter the power: ");
scanf("%d", &c);
m = n ; // save the value
while(n > 0) { // outer while
int i=0, p=1; // local to the block
while(i++ < c) p *= n; // inner while
sum += p;
--n ;
& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 18
}
printf("1^%d + ... + %d^%d = %d\n",
c, m, c, sum);
return 0;
}
& %
Lect 8 Goutam Biswas