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

Presentation on theme: "Algorithm & Flow Charts Decision Making and Looping Presented By Manesh T Course:1090 CS."— Presentation transcript:

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3818405/slides/slide_7.jpg", "name": "Problem: To print sum of first n Numbers Step 1: Start Step 2: Read n,i=1,sum=0 Step 3: Is i

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=5 1 2 3 4 5 Output: N=5 1 2 3 4 5 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=5 1 2 3 4 5 Output: N=5 1 2 3 4 5 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=5 1 2 3 4 5 Output: N=5 1 2 3 4 5 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=10 2 4 6 8 Output: N=10 2 4 6 8

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

Download ppt "Algorithm & Flow Charts Decision Making and Looping Presented By Manesh T Course:1090 CS."

Similar presentations