Friday 30 August 2013

The “ do-while “ loop | C Programming Language Tutorial pdf



The “ do-while “ loop:

=> In do-while, the condition is checked at the end of the loop.
=> The do-while loop will execute at least one time even if the condition is false initially.
=> The do-while loop executes until the condition becomes false.
Syntax:
Initialization Expression;
do
{
Body of the loop
Updation Expression;
} while ( Test Condition);
--------------------------- ------------
I = 1; / * initializing * /
Sum = 0;
do
{
Sum = Sum + I;
I = I +2; / * increment * /
}
While (sum < 40 || I < 10); \ * Testing * /
printf(“% d %d \n”, I, sum);
-------------------------------------------------

Program to check whether the given number is prime or not.
# include<stdio.h>
# include<conio.h>
main( )
{
int n, x=2;
clrscr( );
printf( “Enter the number for testing (prime or not”);
scanf(“%d”, &n);
do
{
if(n%x = = 0)
{
printf(“ \n the number %d is not prime”, n);
exit(0);
}
x++;
} while ( x < n);
printf(“ \n the number %d is prime”, n);
getch( ) ;
}

Output:
Enter the number for testing ( Prime or not) : 5
The number 5 is prime.

Program to compute the factorial of given number using do-while loop.
# include<stdio.h>
# include<conio.h>
main( )
{
int a, fact = 1;
clrscr( );
printf( “ \n Enter the Number:”);
scanf( “ %d”, &a);
do
{
fact = fact * a;
a - -;
} while (a >= 1);
printf( “ \n factorial of given number is %d”, fact);
getch( );
}

Output:
Enter the number : 5
Factorial of given number is 120.

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved