0% found this document useful (0 votes)
7 views

C Programming Iteration in C

The document discusses iteration in C programming, highlighting the necessity of executing statements repeatedly for computations. It provides examples of calculating sums using both closed-form formulas and iterative methods with while loops. The document also explains the structure of while statements and demonstrates their use in various coding examples.

Uploaded by

ABHISHEK GOUTAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

C Programming Iteration in C

The document discusses iteration in C programming, highlighting the necessity of executing statements repeatedly for computations. It provides examples of calculating sums using both closed-form formulas and iterative methods with while loops. The document also explains the structure of while statements and demonstrates their use in various coding examples.

Uploaded by

ABHISHEK GOUTAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

' $

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1

Iteration in C

It is often necessary to execute a sequence of


statements repeatedly to compute certain value.
Every imperative programming languagea
provide different constructs (statements) to
perform this iterative computation.
aA language that uses commands to change the content of a location.

& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2

Example III

Write a program to compute the following sum:

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

But there are alternate ways of doing this


computation. They need not be better for this
particular problem, but can be generalized to
use for similar problems.

& %
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

sum = n + sum; −−n;

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 ;
 

is executed repeatedly with decremented values


of n as long as n is not equal to zero.

& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 10

while Statement

while statement in C is one of the constructs


used for iterative computation. The structure
or syntax of while is
 

 
while (expression) statement

& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 11

while Loop

Initialization

true expression false

statement

& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 12

Note

• The while-loop will not be entered if the


loop-control expression evaluates to false
(zero) even before the first iteration.
• break statement can be used to come out of
the while loop.

& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 13

More Succinct Code

 
 
while(n>0) sum += n-- ;

& %
Lect 8 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 14

Note

• Our while program destroys the input data.


That can be avoided by introducing a third
variable where the value of n can be copied.
• This program structure can be used to
compute any sum of the following form
where c is a known positive integer.
n
X
Sn = 1c + 2c + · · · + nc = kc.
k=1

& %
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

Write a program to compute the following sum:

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

You might also like