Download presentation

Presentation is loading. Please wait.

Published byHayden Wilton Modified over 2 years ago

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, 1. body of the loop 2. control statements.

3
Structure of Loops Entry Controlled Loop Exit Controlled Loop Exit

4
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

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

8
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

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

11
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

12
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

13
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

14
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

15
Structure of While, Do While Loop

16
Structure of for loop

17
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

18
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

19
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

20
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

21
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

22
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

23
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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google