lecture 7 loop

71
LOOP (REPETITION) STRUCTURES CSE- 105 Structure Programming

Upload: turjo987

Post on 10-Jun-2015

96 views

Category:

Engineering


0 download

DESCRIPTION

East West University Bangladesh , computer science teaching lectures, NB:only C lecturer in university who provides Digital lectures

TRANSCRIPT

Page 1: Lecture 7 loop

LOOP (REPETITION) STRUCTURES

CSE- 105Structure Programming

Page 2: Lecture 7 loop

Problem: Conversion tabledegrees radians

Degrees to Radians 0 0.000000 10 0.174533 20 0.349066 30 0.523599

…340 5.934120

350 6.108653 360 6.283186 radians = degrees * PI / 180;

Page 3: Lecture 7 loop

#include <stdio.h>#define PI 3.141593

int main(void){ int degrees=0; double radians;

printf("Degrees to Radians \n"); degrees = 0; radians = degrees*PI/180; printf("%d %f \n", degrees, radians);

degrees = 10; radians = degrees*PI/180; printf(“%d %f \n", degrees, radians);

degrees = 20; radians = degrees*PI/180; printf(“%d %f \n", degrees, radians);… degrees = 360; radians = degrees*PI/180; printf(“%d %f \n", degrees, radians);}

Sequential Solution

degrees = ??? radians = degrees*PI/180; printf("%d %f \n", degrees, radians);

Not a good solution

Page 4: Lecture 7 loop

Loop Solution

#include <stdio.h>#define PI 3.141593

int main(void){ int degrees=0; double radians;

printf("Degrees to Radians \n"); while (degrees <= 360) {

radians = degrees*PI/180; printf(“%d %f \n", degrees,

radians); degrees += 10; }

}

degrees = ??? radians = degrees*PI/180; printf("%d %f \n", degrees, radians);

Page 5: Lecture 7 loop

Loop Solution

#include <stdio.h>#define PI 3.141593

int main(void){ int degrees=0; double radians;

printf("Degrees to Radians \n"); while (degrees <= 360) { radians = degrees*PI/180; printf(“%d %f \n", degrees,

radians); degrees += 10; }

}

degrees = ??? radians = degrees*PI/180; printf("%d %f \n", degrees, radians);

Page 6: Lecture 7 loop

Loop Solution

#include <stdio.h>#define PI 3.141593

int main(void){ int degrees=0; double radians;

printf("Degrees to Radians \n"); while (degrees <= 360) { radians = degrees*PI/180; printf(“%d %f \n", degrees,

radians); degrees += 10; }

}

degrees = ??? radians = degrees*PI/180; printf("%d %f \n", degrees, radians);

Page 7: Lecture 7 loop

Loop (Repetition) Structures

while statementdo while statementfor statementTwo new statements used with loops

break and continue

Page 8: Lecture 7 loop

while statement

while(expression)statement;

while(expression) { statement;statement;

.}

Task is similar as was in if-else

Page 9: Lecture 7 loop

The while Control Structure

Example: x = 1; i = 1;while (i <= 9) {

x = x * i;i = i + 1;

}

i <= 9 x = x * i; i = i + 1;true

false

Initially x = 1; i = 1;

Page 10: Lecture 7 loop

The while Control Structure

Example: x = 1; i = 1;while (i <= 9) {

x = x * i;i = i + 1;

}

Initially, x = 1, i= 1

Iteration 1 :

i <= 9 is true

x = x * i = 1*1 = 1

i = i+1 = 1+1 = 2

Page 11: Lecture 7 loop

The while Control Structure

Example: x = 1; i = 1;while (i <= 9) {

x = x * i;i = i + 1;

}

Now, x = 1, i= 2

Iteration 2 :

i <= 9 is true

x = x * i = 1*2 = 2

i = i+1 = 2+1 = 3

Page 12: Lecture 7 loop

The while Control Structure

Example: x = 1; i = 1;while (i <= 9) {

x = x * i;i = i + 1;

}

Now, x = 2, i= 3

Iteration 3 :

i <= 9 is true

x = x * i = 2*3 = 6

i = i+1 = 3+1 = 4

Page 13: Lecture 7 loop

The while Control Structure

Example: x = 1; i = 1;while (i <= 9) {

x = x * i;i = i + 1;

}

Now, x = 6, i= 4

Iteration 4 :

i <= 9 is true

x = x * i = 6*4 = 24

i = i+1 = 4+1 = 5

Page 14: Lecture 7 loop

The while Control Structure

Example: x = 1; i = 1;while (i <= 9) {

x = x * i;i = i + 1;

}

Now, x = 24, i= 5

Iteration 5 :

i <= 9 is true

x = x * i = 24*5 = 120

i = i+1 = 5+1 = 6

Page 15: Lecture 7 loop

The while Control Structure

Example: x = 1; i = 1;while (i <= 9) {

x = x * i;i = i + 1;

}

Now, x = 120, i= 6

Iteration 6 :

i <= 9 is true

x = x * i = 120*6 = 720

i = i+1 = 6+1 = 7

Page 16: Lecture 7 loop

The while Control Structure

Example: x = 1; i = 1;while (i <= 9) {

x = x * i;i = i + 1;

}

Now, x = 720, i= 7

Iteration 7 :

i <= 9 is true

x = x * i = 720*7 = 5760

i = i+1 = 7+1 = 8

Page 17: Lecture 7 loop

The while Control Structure

Example: x = 1; i = 1;while (i <= 9) {

x = x * i;i = i + 1;

}

Now, x = 5760, i= 8

Iteration 8 :

i <= 9 is true

x = x * i = 5760*8 = 46080

i = i+1 = 8+1 = 9

Page 18: Lecture 7 loop

The while Control Structure

Example: x = 1; i = 1;while (i <= 9) {

x = x * i;i = i + 1;

}

Now, x = 46080, i= 9

Iteration 9 :

i <= 9 is true

x = x * i = 46080*9 = 414720

i = i+1 = 9+1 = 10

Page 19: Lecture 7 loop

The while Control Structure

Example: x = 1; i = 1;while (i <= 9) {

x = x * i;i = i + 1;

}

Now, x = 414720, i= 10

Iteration 10 :

i <= 9 is false

Out of loop

Page 20: Lecture 7 loop

Example#include <stdio.h>#define PI 3.141593

int main(void){ int degrees=0; double radians;

printf("Degrees to Radians \n"); while (degrees <= 360) { radians = degrees*PI/180; printf("%d %.6f \n", degrees, radians); degrees += 10; }return 0;}

OutputDegrees to Radians0 0.00000010 0.17453311 20 0.349066… … …… … …… … …… … …350 6.108653360 6.283186Press any key to continue

Page 21: Lecture 7 loop

Example#include <stdio.h>#define PI 3.141593

int main(void){ int degrees=0; double radians;

printf("Degrees to Radians \n"); while (degrees <= 360) { radians = degrees*PI/180; printf("%d %.6f \n", degrees, radians); degrees += 100; }return 0;}

OutputDegrees to Radians0 0.00000010 0.17453311 20 0.349066… … …… … …… … …… … …350 6.108653360 6.283186Press any key to continue

OutputDegrees to Radians0 0.000000100 1.745329200 3.490659300 5.235988Press any key to continue

Page 22: Lecture 7 loop

Example#include <stdio.h>#define PI 3.141593

int main(void){ int degrees=0; double radians;

printf("Degrees to Radians \n"); while (degrees <= 160) { radians = degrees*PI/180; printf("%d %.6f \n", degrees, radians); degrees += 100; }return 0;}

OutputDegrees to Radians0 0.00000010 0.17453311 20 0.349066… … …… … …… … …… … …350 6.108653360 6.283186Press any key to continue

OutputDegrees to Radians0 0.000000100 1.745329

Press any key to continue

Page 23: Lecture 7 loop

Example#include <stdio.h>#define PI 3.141593

int main(void){ int degrees=0; double radians;

printf("Degrees to Radians \n"); while (degrees <= 60) { radians = degrees*PI/180; printf("%d %.6f \n", degrees, radians); degrees += 10; }return 0;}

OutputDegrees to Radians0 0.00000010 0.17453311… …… … …… … …… … …350 6.108653360 6.283186Press any key to continue

Globally define

Page 24: Lecture 7 loop

Example#include <stdio.h>

int main(void){#define PI 3.141593 int degrees=0; double radians;

printf("Degrees to Radians \n"); while (degrees <= 60) { radians = degrees*PI/180; printf("%d %.6f \n", degrees, radians); degrees += 10; }return 0;}

OutputDegrees to Radians0 0.00000010 0.17453311 20 0 …… … …… … …… … …350 6.108653360 6.283186Press any key to continue

Locally define

Page 25: Lecture 7 loop

Example#include <stdio.h>

int main(void){int degrees=0; double radians;

printf("Degrees to Radians \n"); while (degrees <= 60) { radians = degrees*3.1416/180; printf("%d %.6f \n", degrees, radians); degrees += 10; }return 0;}

OutputDegrees to Radians0 0.00000010 0.17453311 20 …… … …… … …… … …350 6.108653360 6.283186Press any key to continue

Value is used right through

Page 26: Lecture 7 loop

Example#include <stdio.h>

int main(void){int degrees=100; double radians;

printf("Degrees to Radians \n"); while (degrees <= 60) { radians = degrees*3.1416/180; printf("%d %.6f \n", degrees, radians); degrees += 10; }return 0;}

OutputPress any key to continue

0.17453310 20 0.349066… … …… … …continue

No Execution

Page 27: Lecture 7 loop

do while

dostatement;while(expression);

do {

statement1;statement2;

.} while(expression);

Note - the expression is tested after the statement(s) are executed, so statements are executed at least once.

Page 28: Lecture 7 loop

Example#include <stdio.h>

int main(void){int degrees=100; double radians;

printf("Degrees to Radians \n");do { radians = degrees*3.1416/180; printf("%d %.6f \n", degrees, radians); degrees += 10; } while (degrees <= 60);return 0;}

OutputDegrees to Radians100 1.745333Press any key to continue 20 0.349066… … …… … …continue

1 ExecutionAt least

Page 29: Lecture 7 loop

4.8 The do…while Repetition Statement

Flowchart of the do…while repetition statement

true

false

action(s)

condition

Page 30: Lecture 7 loop

for statement

for(initialization; test; increment or decrement)statement;

for(initialization; test; increment or decrement){

statement;statement;

.}

Page 31: Lecture 7 loop

4.4 The for Repetition Statement

Page 32: Lecture 7 loop

Example

#include <stdio.h>#define PI 3.141593

int main(void){ int degrees; double radians;

printf("Degrees to Radians \n"); for (degrees=0; degrees<=360; degrees+=10) { radians = degrees*PI/180; printf("%6i %9.6f \n", degrees, radians); } return 0;}

OutputDegrees to Radians0 0.00000010 0.17453311 20 0.349066… … …… … …… … …… … …350 6.108653360 6.283186Press any key to continue

Page 33: Lecture 7 loop

Example

#include <stdio.h>#define PI 3.141593

int main(void){ int degrees; double radians;

printf("Degrees to Radians \n"); for (degrees=0; degrees<=360; degrees+=100) { radians = degrees*PI/180; printf("%6i %9.6f \n", degrees, radians); } return 0;}

OutputDegrees to Radians0 0.00000010 0.17453311 20 0.349066… … …… … …… … …… … …350 6.108656Press any key to continue

OutputDegrees to Radians0 0.000000100 1.745329200 3.490659300 5.235988Press any key to continue

Page 34: Lecture 7 loop

Example

#include <stdio.h>#define PI 3.141593

int main(void){ int degrees; double radians;

printf("Degrees to Radians \n"); for (degrees=0; degrees<=160; degrees+=100) { radians = degrees*PI/180; printf("%6i %9.6f \n", degrees, radians); } return 0;}

OutputDegrees to Radians0 0.00000010 0.17453311 20 0.349066… … …… … …… … …… … …350 6.108656Press any key to continue

OutputDegrees to Radians0 0.000000100 1.745329

Press any key to continue

Page 35: Lecture 7 loop

4.4 The for Repetition Statement

Format when using for loopsfor ( initialization; loopContinuationTest; increment )

statement

Example: for(counter = 1; counter <= 10; counter++ ) printf( "%d\n", counter );

Prints the integers from one to ten

 

No semicolon (;) after last expression

Page 36: Lecture 7 loop

4.4 The for Repetition Statement

Format when using for loopsfor ( initialization; loopContinuationTest; increment )

statement

Example: for(counter = 1; counter <= 10; counter++ ); printf( "%d\n", counter );

Prints the integers Eleven only

 

Ifsemicolon (;) after last expression only the final value will be printed after increment

Page 37: Lecture 7 loop

Flow Chart of the Example for Loop

counter = 1

counter <= 10true

false

counter = 1

counter++

Establish initial value of control variable

Determine if final value of control variable has been reached

Body of loop (this may be many statements)

Increment the control variable

printf( "%d", counter );

Page 38: Lecture 7 loop

The for Repetition Statement

For loops can usually be rewritten as while loops:initialization;while ( loopContinuationTest ) { statement; increment;}

Initialization and increment Can be more than one, comma-separated lists of

statements Can even add the counter variable declaration in

initialization Example:

for (int i = 0, j = 0; j + i <= 10; j++, i++)

printf( "%d\n", j + i );

Page 39: Lecture 7 loop

Examples

int sum =0;for( int i=1 ; i < 7 ; i+=2 )

sum = sum + i;?

0

i

sum

1

1

3

4

5

9

7

False so done i = 7, sum = 9

Page 40: Lecture 7 loop

Examples

int fact =1;for(int n=5;n>1;n--)

fact = fact * n;

5

1

n

fact

Find the final Value of n and fact

Page 41: Lecture 7 loop

Exercise

Determine the number of times that each of the following for loops are executed.

for (k=3; k<=10; k++) { statements;}

for (k=3; k<=10; ++k) { statements;}

for(count=-2;count<=5; count++) {

statements;}

1

increment

initialfinal

Page 42: Lecture 7 loop

Example

What will be the output of the following program, also show how values of variables change in the memory.

int sum1, sum2, k;sum1 = 0;sum2 = 0;for( k = 1; k < 5; k++) { if( k % 2 == 0) sum1 =sum1 + k; else sum2 = sum2 + k;}printf(“sum1 is %d\n”, sum1);printf(“sum2 is %d\n”, sum2);

0 2 6

0 1 4

1 2 3 4 5

sum1

sum2

k

Page 43: Lecture 7 loop

Example

What will be the output of the following program, also show how values of variables change in the memory.

int sum1, sum2, k;sum1 = 0;sum2 = 0;for( k = 1; k < 5; k++) { if( k % 2 == 0) sum1 =sum1 + k; else sum2 = sum2 + k;}printf(“sum1 is %d\n”, sum1);printf(“sum2 is %d\n”, sum2);

0 2 6

0 1 4

1 2 3 4 5

sum1

sum2

k

Page 44: Lecture 7 loop

Example

What will be the output of the following program, also show how values of variables change in the memory.

int sum1, sum2, k;sum1 = 0;sum2 = 0;for( k = 1; k < 5; k++) { if( k % 2 == 0) sum1 =sum1 + k; else sum2 = sum2 + k;}printf(“sum1 is %d\n”, sum1);printf(“sum2 is %d\n”, sum2);

0 2 6

0 1 4

1 2 3 4 5

sum1

sum2

k

Page 45: Lecture 7 loop

Example

What will be the output of the following program, also show how values of variables change in the memory.

int sum1, sum2, k;sum1 = 0;sum2 = 0;for( k = 1; k < 5; k++) { if( k % 2 == 0) sum1 =sum1 + k; else sum2 = sum2 + k;}printf(“sum1 is %d\n”, sum1);printf(“sum2 is %d\n”, sum2);

0 2 6

0 1 4

1 2 3 4 5

sum1

sum2

k

Page 46: Lecture 7 loop

Example

What will be the output of the following program, also show how values of variables change in the memory.

int sum1, sum2, k;sum1 = 0;sum2 = 0;for( k = 1; k < 5; k++) { if( k % 2 == 0) sum1 =sum1 + k; else sum2 = sum2 + k;}printf(“sum1 is %d\n”, sum1);printf(“sum2 is %d\n”, sum2);

0 2 6

0 1 4

1 2 3 4 5

sum1

sum2

k

Page 47: Lecture 7 loop

Example

What will be the output of the following program, also show how values of variables change in the memory.

int sum1, sum2, k;sum1 = 0;sum2 = 0;for( k = 1; k < 5; k++) { if( k % 2 == 0) sum1 =sum1 + k; else sum2 = sum2 + k;}printf(“sum1 is %d\n”, sum1);printf(“sum2 is %d\n”, sum2);

0 2 6

0 1 4

1 2 3 4 5

sum1

sum2

k

Page 48: Lecture 7 loop

Example

What will be the output of the following program, also show how values of variables change in the memory.

int sum1, sum2, k;sum1 = 0;sum2 = 0;for( k = 1; k < 5; k++) { if( k % 2 == 0) sum1 =sum1 + k; else sum2 = sum2 + k;}printf(“sum1 is %d\n”, sum1);printf(“sum2 is %d\n”, sum2);

0 2 6

0 1 4

1 2 3 4 5

sum1

sum2

k

Page 49: Lecture 7 loop

Example

What will be the output of the following program, also show how values of variables change in the memory.

int sum1, sum2, k;sum1 = 0;sum2 = 0;for( k = 1; k < 5; k++) { if( k % 2 == 0) sum1 =sum1 + k; else sum2 = sum2 + k;}printf(“sum1 is %d\n”, sum1);printf(“sum2 is %d\n”, sum2);

0 2 6

0 1 4

1 2 3 4 5

sum1

sum2

k

Page 50: Lecture 7 loop

Example

What will be the output of the following program, also show how values of variables change in the memory.

int sum1, sum2, k;sum1 = 0;sum2 = 0;for( k = 1; k < 5; k++) { if( k % 2 == 0) sum1 =sum1 + k; else sum2 = sum2 + k;}printf(“sum1 is %d\n”, sum1);printf(“sum2 is %d\n”, sum2);

0 2 6

0 1 4

1 2 3 4 5

sum1

sum2

k

Expression is falsesum1 is 6sum2 is 4

Page 51: Lecture 7 loop

Exercise

What is the output of the following program?for (i=1; i<=5; i++) { for (j=1; j<=4; j++){ printf(“*”);

} printf(“\n”);}

Output

********************

Page 52: Lecture 7 loop

Exercise

What is the output of the following program?

for (i=1; i<=5; i++) { for (j=1; j<=i; j++){ printf(“*”); } printf(“\n”);}

Output

***************

Page 53: Lecture 7 loop

Example: nested loops to generate the following output

i=1 * i=2 + +i=3 * * * i=4 + + + +i=5 * * * * *

int i, j;for(i=1; i <= 5; i++){ printf("i=%d ", i); for(j=1; j <= i; j++) { if (i % 2 == 0) printf("+ "); else printf("* "); } printf("\n");}

Page 54: Lecture 7 loop

Exercise: Modify the following program to produce the output.

for (i=A; i<=B; i++) {

for (j=C; j<=D; j++) {

printf(“*”);

}

printf(“\n”);

}

Output

***************

Page 55: Lecture 7 loop

Exercise

Write a program using loop statements to produce the output.

Output * ** *** *********

Page 56: Lecture 7 loop

For vs. while loop

Convert the following for loop to while loop

for( i=5; i<10; i++) { pritntf(“ i = %d \n”, i);

}

i=5;while(i<10){ pritntf(“ i = %d \n”, i); i++;

}

Page 57: Lecture 7 loop

For vs. while loop : Convert the following for loop to while loop

for( i=5; i<10; i++) {

printf(“AAA %d \n”, i);

if (i % 2==0) continue;

pritntf(“BBB %d \n”, i);

}i=5;while(i<10) { printf(“AAA %d \n”, i); if (i % 2==0) {

i++; continue;

} pritntf(“BBB %d \n”, i); i++;}

Page 58: Lecture 7 loop

Compute xy when y is integer

Suppose we don’t have pow(x,y) and y is integer, write a loop to compute xy

Enter x, y

res=1;

for(i=1; i<=y; i++){

res = res * x;

}

Page 59: Lecture 7 loop

Exercise: sum

Write a program to compute the following

nin

i

...3211

Enter n

total=0;

for(i=1; i<=n; i++)

total = total + i ;

print total

ntotal 2...642

Enter n

total=0;

for(i=1; i<=n; i++)

total = total + 2 * i ;

print total

Page 60: Lecture 7 loop

Write a program to compute the following

mm

i

i xxxxxxx

43210

0

Enter x and m

total=0;

for(i=0; i<=m; i++)

total = total + pow(x, i);

print total

Enter x and m

total=0; sofarx=1;

for(i=0; i<=m; i++) {

total = total +sofarx;

sofarx = sofarx * x;

}

print total

Exercise: sum

Page 61: Lecture 7 loop

Exercise: ln 2

Write a program to compute the following

n

1

7

1

6

1

5

1

4

1

3

1

2

1

1

12ln

Enter nln2=0;for(i=1; i<=n; i++) if ( i % 2 == 0)

ln2 = ln2 - 1.0 / i; else ln2 = ln2 + 1.0 / i; print total

Page 62: Lecture 7 loop

Exercise: ex

Write C program that reads the value of x and n from the keyboard and then approximately computes the value of using the following formula:

Then compare your approximate result to the one returned by exp(x) in C library, and print out whether your approximation is higher or lower.

!!3!2!11

32

n

xxxxe

nx

Page 63: Lecture 7 loop

Exercise: sin x

Compute sin x using

)!12()1(

!7!5!3!1sin

12753

n

xxxxxx

nn

Get ntotal=0; powx=x; factx=1;for(i=0; i <= n; i++){ k= 2*n+1; if (i%2==0) total= total - powx/factx; else total= total + powx/factx; powx= powx * x * x; factx = factx * k * (k-1);}Print total;

Page 64: Lecture 7 loop

Example

Write a program that prints in two columns n even numbers starting from 2, and a running sum of those values. For example suppose user enters 5 for n, then the program should generate the following table:

Enter n (the number of even numbers): 5Value Sum 2 2 4 6 6 12 8 2010 30

Page 65: Lecture 7 loop

break statement

break; terminates loop execution continues with the first statement following

the loop

sum = 0;for (k=1; k<=5; k++) {

scanf(“%lf”,&x); if (x > 10.0) break; sum +=x;}printf(“Sum = %f \n”,sum);

Page 66: Lecture 7 loop

continue statement

continue; forces next iteration of the loop, skipping any

remaining statements in the loop

sum = 0;for (k=1; k<=5; k++) {

scanf(“%lf”,&x); if (x > 10.0) continue; sum +=x;}printf(“Sum = %f \n”,sum);

Page 67: Lecture 7 loop

Example: A man walks

Suppose a man (say, A) stands at (0, 0) and waits for user to give him the direction and distance to go.

User may enter N E W S for north, east, west, south, and any value for distance.

When user enters 0 as direction, stop and print out the location where the man stopped

N

E

S

W

Page 68: Lecture 7 loop

float x=0, y=0; char direction; float mile; while (1) { printf("Please input the direction as N,S,E,W (0 to exit): "); scanf("%c", &direction); fflush(stdin); if (direction=='0'){ /*stop input, get out of the loop */ break; } if (direction!='N' && direction!='S' && direction!='E' && direction!='W') { printf("Invalid direction, re-enter \n"); continue; } printf("Please input the mile in %c direction: ", direction); scanf ("%f",&mile); fflush(stdin); if (direction == 'N'){ /*in north, compute the y*/ y+=mile; } else if (direction == 'E'){ /*in east, compute the x*/ x+=mile; } else if (direction == 'W'){ /*in west, compute the x*/ x-=mile; } else if (direction == 'S'){ /*in south, compute the y*/ y-=mile; } } printf("\nCurrent position of A: (%4.2f,%4.2f)\n",x,y); /* output A's location */

Page 69: Lecture 7 loop

int i, n; double x, ex;double powx, fact;

printf("Enter the value of x and n : ");scanf("%lf %d",&x, &n);

/* Write a loop to compute e^x using the above formula */ex=1.0; fact=1.0; powx=1.0;for(i=1; i<=n; i++){ powx = powx * x; fact = fact * i; ex = ex + powx / fact;}printf("Approx value of e^x is %lf when n=%d\n",ex, n);

/* Check if ex is higher/lower than exp(x) in math lib.*/if(ex < exp(x)) printf("ex est is lower than exp(x)=%lf\n",exp(x));else if (ex > exp(x)) printf("ex est is higher than exp(x)=%lf\n",exp(x));else printf("ex est is the same as exp(x)\n");

Page 70: Lecture 7 loop

Example

Write a program that prints in two columns n even numbers starting from 2, and a running sum of those values. For example suppose user enters 5 for n, then the program should generate the following table:

Enter n (the number of even numbers): 5Value Sum 2 2 4 6 6 12 8 2010 30

Page 71: Lecture 7 loop

#include <stdio.h>int main(void){ /* Declare variables. */ int n; int sum, i; printf("Enter n "); scanf("%d",&n);

printf("Value \t Sum\n"); sum = 0; for(i=1; i <=n; i++){ sum = sum + 2*i; printf("%d \t %d\n", 2*i, sum); } return 0;}