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, 1. body of the loop 2. control statements.
Structure of Loops Entry Controlled Loop Exit Controlled Loop Exit
Types of Loops 1. For Loop 2. While Loop 3. 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=1 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 Start Read N, I=1 Stop Is I< =N Yes No Output: N= Output: N= I=I+1 d=n%0 Print I
Problem: To print first n Numbers Start Read N, I=1 n=n/10 Stop Is I<=N Yes No Output: N= Output: N= sum+=d Print sum
Problem: To print first n Numbers Start Int a[5], n=n/10 Stop Is n!=0 Yes No Output: N= Output: N= sum+=d d=n%10 Print sum
Problem: To print sum of first “n” Numbers Start Read n, i=1,sum=0 i=i+1 Stop Is i<=n Yes No Print sum Sum=sum+i
Problem: To print all even numbers up to “n” Start Read n, i=1,sum=0 Stop Is i<=n Yes No Is i%2=0 Print i Yes i=i+1 No Output: N= Output: N=
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 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 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 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
Practice 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 1. C program to print sum of even numbers upto “n” 2. C program to print sum of odd numbers upto “n” 3. C program to average of first n numbers
Start Read N elements of array i<=n Yes No Read N, Int a[5],esum=0,osum=0 i=1 a[i]%2=0 esum+=a[i] osum+=a[i] No i=i+1 Yes Print even sum, Print odd sum Stop