Algorithm & Flow Charts Decision Making and Looping Presented By Manesh T Course:1090 CS
Definition Loops are used to represent repetitive statements In looping, a sequence of statements are executed until some conditions for termination of the loop are satisfied. A program loop therefore consists of two segments, body of the loop control statements.
Structure of Loops Exit Controlled Loop Entry Controlled Loop Exit
Types of Loops For Loop While Loop Do-While Loop Before studying Loops, let us study how to write algorithm and flowchart of loop based programs
Algorithm examples –loops Problem1: To print first n Numbers Problem2: To print sum of first “n” Numbers Problem3: To print all even numbers up to “n” Study all the above Problems Well.
Problem: To print first n Numbers Step 1: Start Step 2: Read n,i=1 Step 3: Is i<=n then goto step 4 else goto step 6 Step 4: Print i Step 5: i=i+1, goto step 3 Step 6: Stop
Problem: To print sum of first “n” Numbers Step 1: Start Step 2: Read n,i=1,sum=0 Step 3: Is i<=n then goto step 4 else goto step 6 Step 4: sum=sum+i; Step 5: i=i+1, goto step 3 Step 6: Print sum Step 7: Stop
Problem: To print all even numbers up to “n” Step 1: Start Step 2: Read n, i=0 Step 3: Is ( i<=n) then goto step 4 else goto step 6 Step 4: Is (i%2=0) then print “i” Step 5: i=i+1, goto step 3 Step 6: Stop
Flowchart examples –loops Problem1: To print first n Numbers Problem2: To print sum of first “n” Numbers Problem3: To print all even numbers up to “n” Study all the above Problems Well.
Problem: To print first n Numbers Output: N=5 1 2 3 4 5 Start Read N, I=1 Is I< =N No Yes Print I I=I+1 Stop
Problem: To print sum of first “n” Numbers Start Read n, i=1,sum=0 Is i<=n No Yes Print sum Sum=sum+i i=i+1 Stop
Problem: To print all even numbers up to “n” Output: N=10 2 4 6 8 Start Read n, i=1,sum=0 Is i<=n No Yes i=i+1 Is i%2=0 No Yes Print i Stop
Structure of While , Do While Loop
Structure of for loop
C Programs examples –loops Problem1: To print first n Numbers Let us study how to write above program using while, Do-while and For loops
Problem: To print first n Numbers #include<stdio.h> void main() { int n, i=1; printf(“Enter n”); scanf(“%d”,&n); while(i<=n) printf(“%d”, i); i++; } Program Using while loop
Problem: To print first n Numbers #include<stdio.h> void main() { int n, i=1; printf(“Enter n”); scanf(“%d”,&n); do printf(“%d”, i); i++; }while(i<=n); } Program Using do-while loop
Problem: To print first n Numbers #include<stdio.h> void main() { int n,i; printf("Enter n:\n"); scanf("%d",&n); for(i=1;i<=n;i++) printf("%d\n",i); } Program Using for loop
Tutorial/Model Programs Print first N numbers Print sum of first N Numbers Print all even numbers upto N Print all odd numbers upto N Print sum of even numbers up to N Print sum of odd numbers up to N Print sum of even and sum of odd numbers up to N
Assignment 3 C program to print sum of even numbers upto “n” C program to print sum of odd numbers upto “n” C program to average of first n numbers