Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithm & Flow Charts Decision Making and Looping

Similar presentations


Presentation on theme: "Algorithm & Flow Charts Decision Making and Looping"— Presentation transcript:

1 Algorithm & Flow Charts Decision Making and Looping
Presented By Manesh T Course:1090 CS

2 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.

3 Structure of Loops Exit Controlled Loop Entry Controlled Loop Exit

4 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

5 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.

6 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

7 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

8 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

9 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.

10 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

11 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

12 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

13 Structure of While , Do While Loop

14 Structure of for loop

15 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

16 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

17 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

18 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

19 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

20 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


Download ppt "Algorithm & Flow Charts Decision Making and Looping"

Similar presentations


Ads by Google