Looping Statements
Looping Statements
What is a loop?
repeating
A loop is way of a statement
or a set of statements a given number of times
Example:
while loop
flowchart
2. for loop: It is a pre test loop, in which the condition is
checked first and if the condition evaluates to true then body of
loop gets executed repeatedly, until the condition remains True.
Once the condition is False, the control comes outside the loop.
Syntax of for loop
Flowchart Representation
for (initialization ; Condition; Updation)
{
statement 1; False
statement 2; Initialization
Updation
: Condition
: True
statement n;
} Statements
Example
3. do while loop: It is a post test loop, in which the body of loop
gets executed first and then the condition is checked. If the
condition evaluates to true then again the body of loop
executes repeatedly, until the condition remains True. Once the
condition is False, the control comes outside the loop.
Syntax of while loop
Example:
do while
loop
flowchart
While and do while working
Difference between while and do while
while do while
1. It is also known as pre 1. It is also known as post
test/entry controlled loop test/exit controlled loop
2. In this loop, loop condition is 2. In this loop, the body of loop
checked first, and then the executes first and then the
body of loop executes if the loop condition is checked.
condition is TRUE. 3. In this loop, condition is
3. In this loop, condition is checked n times.
checked n+1 times. 4. In this loop, minimum
4. In this loop, minimum number of iterations are 1
number of iterations are i.e. even if the condition is
zero. False, the executes at least
once.
All three loops do the same task of, repetition
Output of all
three
programs is
same
1. Program to read an integer number and find the sum of all the digits of a given number
INPUT OUTPUT
Steps to do so,
1. Dividing any number by 10 gives last number as
4 + 3 + 2 + 1 = 15 remainder. i.e. 1234%10 = 4
2. Store the remainder from step 1 in a temporary
variable to keep sum. sum = sum + rem
3. Divide the number by 10 to get rid of last digit.
i.e. 1234/10 = 123
Repeat step 1 to 3 until the number becomes zero.
1. Write a C Program to read an integer number and find the sum of all the digits of
a given number
2. Write a C Program to read an integer number and reverse it and check if the given
number is palindrome number or not.
Palindrome is the number which is same as the original number even after reversing.
Example 1221 reverse is 1221 so it is palindrome
INPUT OUTPUT
Logic???
2. Program to read an integer number and reverse it and check if the given number is
palindrome number or not.
3. Write a C Program to read a number and check if the given number is Armstrong
number or not.
A number is said to be Armstrong number, If the sum of cubes of individual
digit is same as the original number then we call it as Armstrong number.
Example → 153 = 1*1*1 + 5*5*5 + 3*3*3 Note :For only Three digit
number.
=1+125+27
= 153
INPUT OUTPUT
INPUT OUTPUT
Consider,
Number = 5
Initially keep,
Sum= 0
Sum = Sum + 1 Sum = Sum + 2
=0+1 =1+2
Sum =1 Sum =3
INPUT OUTPUT
46 PROGRAM Factorial of 4 ! is 24
Consider,
Number = 4
Initially keep,
Factorial = 1
Factorial = Factorial* 1 Factorial = Factorial* 2
=1*1 =1*2
Factorial =1 Factorial =2
In general,
repeat Factorial = Factorial * i, where i will take value from 1 to n
5. Write a C Program to find the factorial of given number.
6. Write a C Program to print Factors of given numbers.
The factors of a number are the numbers that divide into it exactly.
Example → 12 has six factors: 1, 2, 3, 4, 6 and 12.
If 12 is divided by any of the six factors then the answer will be a whole number.
OUTPUT
INPUT
Factors of 6 are:
1
66 PROGRAM 2
3
6
6%1 Is 1 factor of 6?? → Yes
6%2 Is 2 factor of 6?? → Yes
6%3 Is 3 factor of 6?? → Yes
6%4 Is 4 factor of 6?? → No
6%5 Is 5 factor of 6?? → No
6%6 Is 4 factor of 6?? → Yes
In general,
repeat n % i == 0, and print i if i is a factor of n, where i will take value from 1 to n
6. Write a C Program to print Factors of given number.
7. Write a C Program to read a number and check if the given number is Prime
number or not.
A number is said to be prime if it is only divisible by one and itself. If any other
number other than one and itself divides and gives remainder as zero then it is not prime.
Example → 17 it is divisible by only 1 and 17, so it is prime number.
→ 9 other than 1 and 9, it is also divisible by 3, so it is not prime number
INPUT OUTPUT
17 It is Prime number
A Fibonacci series is series in which the first number is 0 and second number
is 1, next numbers in the series are sum of previous two numbers.
9. Write a C Program to print Fibonacci series.
10. Write a C Program to find the nth Fibonacci number.
// Program to add numbers until the user enters zero
#include <stdio.h>
int main() {
double number, sum = 0;
printf("Sum = %.2lf",sum);
return 0;
}
• Enter a number: 1.5
• Enter a number: 2.4
• Enter a number: -3.4
• Enter a number: 4.2
• Enter a number: 0
• Sum = 4.70
• Print i as long as i is less than 6:
• int i = 1;
• (i < 6) {
• printf("%d\n", i);
•
• ;
• }
• int i = 0;
• do {
• printf("%d\n", i);
• i++;
• }
• while (i < 5);
Write the sum of nos from m to n
sum=0;
M=?
N=?
{
printf("\n");
for(j=1;j<=i;j++)
printf("*");
}
return 0;
}