Looping statement are the statements execute one or more statement again and again several number of times.
Lopping sattements are used when check same condition number of times.
while( condition ) { statements to be executed //Body of loop; }
in "while loop" check the condition if the condition is true then the body of the while loop is executed, and again check the condition. the while loop is executd until the condition gets false.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum;
n=10;
sum=0;
while(n<=10)
{
sum=sum+n;
}
printf("Sum of frist 10 natural numbers is %d”, sum);
getch();
}
Output-
sum of frist 10 natural numbers is 55
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum;
sum=0;
printf("Enter the number\n");
scanf("%d",&n;)
while(n!=0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("Sum of digit of number is %d”, sum);
getch();
}
Output-
Enter the number 123 Sum of digit of number is 6
do { Statements //Body of loop; } while ( condition );
in "do while loop" frist time the body of loop get executed and then check the condition if condition is true the body of loop again executed. body of loop get executed until yhe condition get false. the difference between while and do while loop is in do while loop body of loop executed at least once even als condition is false.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n;
printf("Enter a number\n ");
scanf("%d",&n);
printf("The table of %d is\n"n);
do{
printf("%d \n",(n*i));
i++;
}while(i<=10);
getch();
}
Output-
Enter a number 5 The table of 5 is 5 10 15 20 25 30 35 40 45 50
#include<stdio.h>
#include<conio.h>
void main()
{
int num,r,sum=0;
clrscr();
printf("enter any number");
scanf("%d",&num);
do{
r=num%10;
sum=sum+r;
num=num/10;
}while(num!=0);
printf("sum of digits =%d",sum);
getch();
}
}
Output-
enter eny number 123 Sum of digits=6
for(initialization; condition; increment/decrement statement) { Body of for loop; }
In "for loop" The initialization statement is executed only once.Then, the condition is evaluated. If condition is false (0), for loop is terminated. But if the condition is true (nonzero), codes inside the body of for loop is executed and then increment or decrement statement is executed and again condition is checked untill the condition is false body of for loop get executed. All expressions or statement in the for loop are optional. at the end of the for loop semicolon is there then for loop not executed.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
printf("frist 10 even natural numbers are\n");
for(i=1;i<21;i++)
{
if(i%2==0)
{
printf("%i\n",i);
}
}
getch();
}
Output-
frist 10 even numbers are 2 4 6 8 10 12 14 16 18 20
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,k;
k=0;
printf("Enter the number for checking prime or not prime\n");
scanf("%d",&n;)
for(i=2;i<n/2;i++)
{
if(n%i==0)
{
k=1;
}
}
if(k==0)
{
printf("The entered number is prime");
}
else
{
printf("The entered number is not prime");
}
printf("Sum of digit of number is %d”, sum);
getch();
}
Output-
Enter the number for checking prime or not prime 37 The entered number is prime