You are on page 1of 9

LOOPING STATEMENTS

2 LOOPING STATEMENTS
Problem statement: to find the factorial of a given number Find the statement
within the box

#include<stdio.h>
int main()
{
    int a,f,i;
    printf("Enter a number: ");
    scanf("%d",&a);
    f=1;//initialize output variable
    i=1;
    while(i<=a)
    {
         f=f*I;
        i++;
    }
    printf("Factorial: %d",f);
    return 0;
}
3 LOOPING STATEMENTS
Problem statement: To find the sum of even numbers between 1 and n. Find the
statement within the box

#include <stdio.h>

int main()
{
int n, sum=0;
int i=0;
printf("Enter upper limit: ");
scanf("%d", &n);
while(i<=n)
{
sum =sum+i;
; i=i+2;
}

printf("Sum of odd numbers = %d", sum);

return 0;
4 LOOPING STATEMENTS
Problem statement: To find the factors of a number between 1 and n. Find the
statement within the box
int main()
{
int num;
int i=1;
printf("Enter any number to find its factor: ");
scanf("%d", &num);
while(i<=num)
{
if (num%i==0)
{
printf("%d, ",i);
}
}

return 0;
}
5 LOOPING STATEMENTS-do while

SYNTAX:
do
{
//Statements
}while(condition test);
6
LOOPING STATEMENTS-
programming logics

 Write a program to count the


number of spaces between numbers.
(consider that their will only one
space between two numbers)
 Input: 23 45 34 56 72
 Output: 4
7
LOOPING STATEMENTS-
programming logics
8
LOOPING STATEMENTS-
programming logics

 Write a program to count the odd


numbers ,given a set of numbers
separated by space in a line .(consider
that their will only one space between
two numbers)
 Input: 23 45 34 56 72
 Output:2
9
LOOPING STATEMENTS-
programming logics

You might also like