0% found this document useful (0 votes)
84 views

Repetition and Loop Statements

Here is a C program to print the pattern: #include <stdio.h> int main() { int n, i, j; printf("Enter the value of n: "); scanf("%d", &n); for(i=1; i<=2*n-1; i++) { for(j=1; j<=n; j++) { if(i+j <= n+1 || i-j < n-1) printf("%d ", j); else printf(" "); } printf("\n"); } return 0; } This program takes input n from the user. Then there are two nested loops. The

Uploaded by

srutii
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Repetition and Loop Statements

Here is a C program to print the pattern: #include <stdio.h> int main() { int n, i, j; printf("Enter the value of n: "); scanf("%d", &n); for(i=1; i<=2*n-1; i++) { for(j=1; j<=n; j++) { if(i+j <= n+1 || i-j < n-1) printf("%d ", j); else printf(" "); } printf("\n"); } return 0; } This program takes input n from the user. Then there are two nested loops. The

Uploaded by

srutii
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Repetition and Loop Statements

GCD of Two Numbers


The greatest common divisor (GCD) of two
integers is the product of the integers’
common factors. Write a program that inputs
two numbers and find their GCD by repeated
division. For example, consider the numbers
252 and 735. find the remainder of one
divided by the other.
GCD of Two Numbers
Now we calculate the remainder of the old
divisor divided by the remainder found

Repeat the process until remainder is zero


The Divisor when remainder is zero is the GCD

21 is the GCD
GCD problem

Input Output Logic Involved


Two numbers GCD of the Euclidean algorithm,
numbers binary GCD algorithm,
repeated division
method
Algorithm to Find GCD
Step 1: Read the numbers from the user
Step 2: Let dividend = number1 and divisor =
number2
Step 3: Repeat step 4 to step 6 while remainder not
equal to zero
Step 4: remainder = number1 modulus number2
Step 5: dividend = divisor
Step 6: divisor = remainder
Step 7: GCD = divisor
Step 8: print GCD
Weather Report
• Weather station in Chennai has recorded
rainfall of first ten days in December 2016 using
a rain gauge. Develop an algorithm and write a
program to count and print the number of days
in which rainfall was low, medium and high. The
value given is in cm. If the value is less than 12
then it is low, 12 to 20 is medium and more
than 20 is high.
Weather problem

Input Output Logic Involved


Rainfall in cm Number of Count the number of
recorded for ten days low, days with rainfall as
days, ten medium low, medium and high
numbers and high
rainfall
Algorithm for Weather Problem
Step 1: Let counter =1, num_Of_Low = 0, num_Of_Med =
0, num_Of_High = 0
Step 2: If counter == 10 then goto Step 9
Step 3: Read rainfall recorded in cm
Step 4: increment counter
Step 5: if rainfall < 12 then increment num_Of_Low
Step 6: if rainfall is between 12 and 20 then increment
num_Of_Med
Step 7:Step 5: if rainfall > 20 then increment num_Of_High
Step 8: Goto Step 2
Step 9: Print num_Of_Low, num_Of_Med, num_Of_High
Implementation
• We have to learn how to repeat statements
• In some cases the number of times to repeat a
statement is known, in weather report example it
is ten times we have to repeat some statements
• In some other cases the conditions are not direct
as a number but as a terminating condition that
may be based on I/O. In our GCD problem, the
statements are to be repeated till reminder
becomes zero
Counter-controlled loop
Used when we can determine prior to loop
execution exactly how many loop repetitions will be
needed to solve the problem

A counter-controlled loop follows this general


format:

Set loop control variable to an initial value of 0


while (loop control variable < final value) . . .
Increase loop control variable by 1
Syntax of a For loop
for (loop control variable initialization; loop
terminating condition; loop control variable update)

-All three components are optional


-But semicolons are mandatory
GCD Program Using While
GCD Program Using For
Weather Program Using While
Weather Program Using For
Do while Loop
• Similar to a while loop, except the fact that it
is guaranteed to execute at least one time
• Syntax :
do
{
statement(s);
} while( condition );
Condition is checked at the end of execution
GCD Program Using Do While
Sentinel-Controlled Loops
• Consider program that calculates the sum of a
collection of exam scores is a candidate for
using a sentinel value. If the class is large, the
instructor may not know the exact number of
students who took the exam being graded.
The program should work regardless of class
size. The loop below uses sum as an
accumulator variable and score as an input
variable.
Steps in Sentinel-Controlled Loops
1. Initialize sum to zero
2. Get first score
3. while score is not the sentinel
4. Add score to sum
5. Get next score
Break and Continue Statements
• Interrupt iterative flow of control in loops
• Break causes a loop to end
• Continue stops the current iteration and begin
the next iteration
Iterative C code of Isogram Problem
Nested Loops
• Loops may be nested just like other control
structures.
• Nested loops consist of an outer loop with one
or more inner loops.
• Each time the outer loop is repeated, the
inner loops are reentered, their loop control
expressions are reevaluated
Print Pattern
Analysis
• Let ‘n’ is the largest number in the pattern
• The pattern consist of 2*n-1 rows
• Element in each row increases till ‘n’ is printed
and then decreases
• Two loops are required
• One to print each row
• Other one should be used in each row to print
elements of the row
Print Pattern

Input Output Logic Involved

Value of ‘n’ Print a Nested loop single or


pattern multiple
Write a Program to Print the Pattern
Write a Program to Find the Value of Pi
upto ‘n’ terms
Write a Program to Print the Pattern
Write a Program to Print the Pattern
Write a Program to Print the Pattern
Write a Program to Print the Pattern

You might also like