Professional Documents
Culture Documents
Week 4 - Looping - Do While Statement
Week 4 - Looping - Do While Statement
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;
}
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