Week 09 (Repetition) (1)
Week 09 (Repetition) (1)
Week 09 (Repetition) (1)
scanf(%d %d %d %d %d,
&num1,&num2,&num3,&num4,&num5);
sum = num1+num2+num3+num4+num5;
average = sum/5;
SUPPOSE WE WANT TO ADD FIVE
NUMBERS (SAY TO FIND THEIR AVERAGE).
WITHOUT LOOP
SOLUTION (WITH LOOP):
Suppose we wanted to add and
average 100, or 1000, or more
numbers.
We would have to declare that many
5 3 7 9 4
THERE ARE MAINLY TWO TYPES OF
LOOPS IN C PROGRAMMING:
entry condition.
while ( a>b ) A while loop checks the
{
expression before entering
}
the loop body
while ( a>b && c<=78 ) The expression is evaluated
while(count <= 5)
False
count <= 5? {
count = count + 1;
True
printf(“%d ”,count);
count = count + 1 }
}
output count
11 22 33 44 55 66
end
WHILE LOOPS: EXAMPLES
start (2)
int main()
input A
{
input B
int count, A, B;
scanf(“%d”,&A);
count = A scanf(“%d”,&B);
count = A;
False
while(count <= B)
count <= B? {
True count = count + 1;
count = count + 1 printf(“%d ”,count);
}
output count }
33 88
44 55 66 77 88 99
end
What is the output when:
A = 12 and B = 21
THE DO…WHILE LOOPING
(REPETITION) STRUCTURE
The general form of a do...while statement is:
do
do
statement
statement
while(expression);
while(expression);
do
{
count = count + 1;
count = count + 1 printf(“%d ”,count);
} while(count <= 5);
output count }
True
count <= 5? 11 22 33 44 55 66
False
end
start
DO WHILE EXAMPLE(4)
int main()
{
input A
int count, A, B;
scanf(“%d”,&A);
input B
scanf(“%d”,&B);
count = A;
count = A
do
{
output count count = count + 1;
printf(“%d ”,count);
count = count + 1 } while(count <= B)
}
True
count <= B?
33 88
False
end 44 55 66 77 88 99
What is the output when:
A = 11 and B = 33
THE FOR LOOPING (REPETITION)
STRUCTURE
The general form of the for statement is
for(initial
for(initial statement;
statement; loop
loop condition;
condition; update
update statement)
statement)
statement
statement
11 22 33 44 55 66 77 88 99 10
10
int counter;
Output
Output aa line
line of
of stars.
stars. Output
Output aa line
line of
of stars.
stars.
Output
Output aa line
line of
of stars.
stars. **
Output a line of stars. Output
Output a line of stars. Output aa line
line of
of stars.
stars.
Output
Output aa line
line of
of stars.
stars. **
Output a line of stars. Output
Output a line of stars. Output aa line
line of
of stars.
stars.
** **
Output
Output aa line
line of
of stars.
stars.
for(i = 1; i <= 5; i++); **
printf("*“); Output
Output aa line
line of
of stars.
stars.
**
**
FOR LOOPING STRUCTURE
All three statements can be omit—initial statement,
loop condition, and update statement.
HelloHello Infinite output;
for(;;) Hello
Hello press break to
printf("Hello\n“); stop the program
..
running
..
Count backward
for(i = 10; i >= 1; i--)
printf(“%d “,i); 10
10 99 88 77 66 55 44 33 22 11
int main()
{
int i, newNum, sum, average;
55 44 33 22 11
sum = 0; The
The sum
sum isis 15
15
for(i = 1; i <= 5; i++)
The
The average
average is is 33
{
scanf(“%d”,&newNum);
sum = sum + newNum;
}
average = sum / 5;
printf("The sum is %d\n”,sum);
printf(“The average is %d”,average);
return 0;
}
Scenario 1: Printing Multiples of a
Number
Problem: You are given a number N. Write a C program that uses a
for loop
to print the first 10 multiples of the number.
planation:
Explanation:
The program first takes the input N, which defines how many even numbers to sum.
The for loop runs from i = 1 to N, and in each iteration, it adds the i-th even number
(calculated as 2 * i) to the sum. Finally, it prints the total sum.
Scenario 3: Calculating Factorial of a
Number
Problem: Write a C program to calculate the factorial of a given number N using
a for loop. The factorial of a number N is the product of all positive integers less
than or equal to N.
Explanation:
Explanation:
Outer Loop (for i): Controls the number of rows and runs n times.
Inner Loop (for j): Controls the number of columns and also runs n times
in each row.
Each iteration of the inner loop prints a # symbol. After printing n
symbols, the program moves to the next line (\n).
EXAMPLE-03
OUTPUT
NESTED WHILE (EXAMPLE 7)
#include<stdio.h>
int main()
while (expr1) {
int r,c,s;
r=1;
{ while(r<=5) /*outer loop*/
: {
while (expr2) c=1;
while(c<=2) /*inner loop*/
{ {
: s=r+c;
Update expr2; printf("r=%d c=%d sum=%d\n",r,c,s);
c++;
}
}
: printf("\n");
update expr1; r++;
} }
}
NESTED DO WHILE (EXAMPLE 8)
#include<stdio.h>
do int main()
{
int i=1,j=0,sum;
{ do {
: sum=0;
do do{
sum=sum+j;
{ printf("%d",j);
: j++;
Update expr; if(j<=i)
{
} printf("+");
while(expr); }
: }while(j<=i);
update expr; printf("=%d\n",sum);
j=1;
} i++;
while(expr); }while(i<=10);
}
EXERCISES
1. Find the persistence of a number. It is the
number, if times you can multiple the digits
(iteratively) before you get a single digit
number.
2. Given two numbers multiply them using the
following method. Successively divide the
smaller number by 2 (ignore any remainder)
until the quotient is 1 and multiple the
larger number by 2. Add to a total only
those multiples of the larger which
correspond to an odd quotient of the
smaller.
3. Find the digital root of a number (repeatedly
find the sum of the digits until you get a
number 1 to 9.
285:
2+8+5=15
1+5=6
4. A ball is dropped from a height of x. It
decreases 2/3 on height x at each bounce.
How many bounces the ball will have until
height is less than y? (Use while loop and if
condition to break, when height x becomes
less than y).
5. Write a C program that receives the total
number of integers (N). Then, the program
will ask for N real numbers. By applying for
loop, your program should find and display
the largest and the lowest of the numbers.
Give a proper message for invalid user input
(Don’t use arrays).