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

Looping Statements

Uploaded by

Techrishu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Looping Statements

Uploaded by

Techrishu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Loops in C

What is a loop?

repeating
A loop is way of a statement
or a set of statements a given number of times

There are three Loops in C


1. while
2. for
3. do while
1. while 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 while loop

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

Sum of all digits in the


1234 PROGRAM number 1234 is 10
Logic???

Assume the given number is,


1234

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

1234 Reverse of 1234 is 4321


It is not Palindrome number
PROGRAM
1221 Reverse of 1221 is 1221
It is Palindrome number

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

153 It is Armstrong number

122 PROGRAM It is not Armstrong number


3. Write a C Program to read a number and check if the given number is Armstrong
number or not. Note :Three digit number
4. Write a C Program to find the sum of first n natural numbers.

INPUT OUTPUT

56 PROGRAM Sum of first 5 numbers =15

Consider,
Number = 5
Initially keep,
Sum= 0
Sum = Sum + 1 Sum = Sum + 2
=0+1 =1+2
Sum =1 Sum =3

Sum = Sum + 3 Sum = Sum + 4


=3+3 =6+4 Sum = Sum + 5
Sum =6 Sum =10 =10+5
Sum =15
In general,
repeat Sum = Sum + i, where i will take value from 1 to n
4. Write a C Program to find the sum of first n natural numbers.
5. Write a C Program to find the factorial of given number.
Factorial is the product of all positive integers less than or equal to a given
positive integer

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

Factorial = Factorial* 3 Factorial = Factorial* 4


=2*3 =6*4
Factorial =6 Factorial =24

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

9 PROGRAM It is not Prime number

17%2 Is remainder equals zero?? → No


17%3 Is remainder equals zero?? → No
17%4 Is remainder equals zero?? → No
: : :
17%16 Is remainder equals zero?? → No
Therefore, 17 is prime
In general, repeat n % i == 0, where i will take value from 2 to n-1 or n/2
7. Write a C Program to read a number and check if the given number is Prime number or not.
8. Write a C Program to print Prime numbers in the given range.
9. Write a C Program to print Fibonacci series.

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;

// the body of the loop is executed at least once


do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.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=?

1. Calculate the sum from m to n


Int Sum=0;
int I;
while (m<=n)
{
Sum=sum+I;
I++;
}
Printf(“%d”,Sum);
Write a program to print
**********
• i=o;
• While(i<=10)
• {
• Printf(“*”);
• }
Write a c program to accept 10 nos
and add +,- nos
• Int n,ps=0,ns=0;
• Scanf(“%d”,&i);
• while (i<=n)
• {
• if(i>0)
• ps+=I
• i++;
• else if(i<0)
• {
• ns+=i;
• i++;
• }
#include <stdio.h>
int main()
{
int i,j,m,n;
printf("enter the value of m \n"); *
scanf("%d",&m); **
for(i=1;i<=m;i++)

{
printf("\n");
for(j=1;j<=i;j++)
printf("*");
}
return 0;
}

You might also like