do-while loop
C Programming मे तीसरा Loop, do-while loop है । do-while loop को exit controlled loop भी कहते है। exit controlled का मतलब इस Loop में condition, statement execute होने के बाद test होती है। इस loop की खास बात यह है कि condition, true हो या false हो do-while loop एक बार तो execute होता ही है।
Syntax :
Example
Output :
}while(reminder !=0);
getch();
return 0;
}
Output :
Armstrong Numbers
Output :
/* Program to print natural numbers from 1 to 10 */
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
int main(void)
{
int i;
clrscr();
i=1;
printf("Natural Numbers from 1 to 10");
int main(void)
{
int i;
clrscr();
i=1;
printf("Natural Numbers from 1 to 10");
do
{
printf("%d ", i );
i++;
}while(i<=10);
getch();
return 0;
}
}while(i<=10);
getch();
return 0;
}
Natural Numbers from 1 to 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
/* Program to find the H.C.F of two numbers by continued division method. */
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
int main(void)
{
int dividend, divisor, remainder;
clrscr();
printf("Enter two numbers");
int main(void)
{
int dividend, divisor, remainder;
clrscr();
printf("Enter two numbers");
scanf("%d%d", ÷nd, &divisor);
do
{
remainder = dividend % divisor;
if(remainder == 0)
{
printf("The HCF ïs =%d", divisor);
}
else
{
dividend = divisor;
divisor = remainder;
}
}while(reminder !=0);
getch();
return 0;
}
Enter two numbers 100 56
The HCF is = 4
The HCF is = 4
/* Program to check that a number is Armstrong or not */
#include<stdio.h>
#include<conio.h>
int main(void)
{
int number, sum=0;
int duplicate, digit;
clrscr();
printf("Enter the number");
int main(void)
{
int number, sum=0;
int duplicate, digit;
clrscr();
printf("Enter the number");
scanf("%d", &number);
duplicate = number;
duplicate = number;
do
{
digit = number%10;
sum =sum+digit*digit*digit;
number = number /10;
number = number /10;
}while(number > 0);
if(duplicate == sum)
{
printf("%d is Armstrong", duplicate);
}
else
{
printf("%d is not Armstrong", duplicate);
}
getch();
return 0;
}
if(duplicate == sum)
{
printf("%d is Armstrong", duplicate);
}
else
{
printf("%d is not Armstrong", duplicate);
}
getch();
return 0;
}
Enter the number 153
153 is Armstrong
153 is Armstrong
PREVIOUS : Nesting of while loop
No comments:
Post a Comment