UNIT – II - Problem Solving Techniques Using C
S11BLH12
1
,ALGORITHIMIC STRATEGIES:
Simple Strategies for Developing Algorithms (Iteration, Recursion)
Iterations:
A sequence of statements is executed until a specified condition is true is called
iterations.
1. For loop
2. While loop
Example: Print n Natural numbers using FOR Loop
Algorithm Pseudo code
Step 1: Start BEGIN
Step 2: Get the value of n. GET n
Step 3: Initialize i=1 INITIALIZE i=1
Step 4: if (i<=n) go to step 5 else go to step 7 FOR (i<=n) DO
Step 5: Print i value and increment i value by 1 PRINT i
Step 6: Go to step 4 i=i+1
Step 7: Stop ENDFOR
END
2
, Example: Print n Natural numbers using WHILE Loop
Algorithm Pseudo code
Step 1: Start BEGIN
Step 2: Get the value of n. GET n
Step 3: Initialize i=1 INITIALIZE i=1
Step 4: if (i<=n) go to step 5 else go to step 7 WHILE(i<=n) DO
Step 5: Print i value and increment i value by 1 PRINT i
Step 6: Go to step 4 i=i+1
Step 7: Stop ENDWHILE
END
Fig 1.8 Flowchart to print n Natural numbers using WHILE Loop
The above flowchart is to print all natural numbers up to n .The user provides
the input value n. The first value, i is initialized as one. A loop is initialized. The i value
is checked with n, the user input. The numbers are printed until the value is less than
the user input value. When the condition becomes false the loop terminates.
3
S11BLH12
1
,ALGORITHIMIC STRATEGIES:
Simple Strategies for Developing Algorithms (Iteration, Recursion)
Iterations:
A sequence of statements is executed until a specified condition is true is called
iterations.
1. For loop
2. While loop
Example: Print n Natural numbers using FOR Loop
Algorithm Pseudo code
Step 1: Start BEGIN
Step 2: Get the value of n. GET n
Step 3: Initialize i=1 INITIALIZE i=1
Step 4: if (i<=n) go to step 5 else go to step 7 FOR (i<=n) DO
Step 5: Print i value and increment i value by 1 PRINT i
Step 6: Go to step 4 i=i+1
Step 7: Stop ENDFOR
END
2
, Example: Print n Natural numbers using WHILE Loop
Algorithm Pseudo code
Step 1: Start BEGIN
Step 2: Get the value of n. GET n
Step 3: Initialize i=1 INITIALIZE i=1
Step 4: if (i<=n) go to step 5 else go to step 7 WHILE(i<=n) DO
Step 5: Print i value and increment i value by 1 PRINT i
Step 6: Go to step 4 i=i+1
Step 7: Stop ENDWHILE
END
Fig 1.8 Flowchart to print n Natural numbers using WHILE Loop
The above flowchart is to print all natural numbers up to n .The user provides
the input value n. The first value, i is initialized as one. A loop is initialized. The i value
is checked with n, the user input. The numbers are printed until the value is less than
the user input value. When the condition becomes false the loop terminates.
3