0% found this document useful (0 votes)
2 views

While Loop

Uploaded by

kotaa6731
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

While Loop

Uploaded by

kotaa6731
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

while Loop:

while loop is entry level control / pre-test loop.


The format of while loop is given below:

while (condition)
{
statements;
}

The condition (test-expression) is evaluated at the entry of the loop. If the condition returns true, the
statements block will be executed. Otherwise, the loop is terminated.
The control will be returned back to verifying the loop condition state after each iteration of
statements execution.

The statements block may contain any valid c statements.


The while loop is generally used to execute the statements block for indefinite number of times.
That is, the number of repetitions will be decided at run time rather than coding time like in for
loop.

Eg. The following example reads the product quantity and price values, then calculates the bill
value of the product. It reads indefinite number of products data. The loop continuity will be
decided at run time based on user choice.

/* while loop example */


# include <stdio.h>

void main()
{
float q, p, bill, netbill=0.0;
char choice='y';
while (choice == 'y')
{
printf("\nEnter quantity :" );
scanf("%f", &q);
printf("Enter price : ");
scanf("%f", &p);
bill = q*p;
printf("Current product bill %.2f", bill);
netbill += bill;
printf("\nAny more products (y/n) ? ");
choice = getche();
}
printf("\n\nNet bill : %.2f", netbill);
}

/* Program to calculate sum of digits of given number */


#include <stdio.h>

void main()
{
int num, digit, sum;

printf("Enter number :");


scanf("%d", &num);

sum = 0; /* initialize the sum used on rhs&lhs of + operation with 0


to avoid garbage value at first time adding */

while (num > 0) /* if n has any digits */


{
digit = num % 10; /* retrieve last digit */
sum += digit; /* add the digit to sum */
num /= 10; /* remove last digit */
}
printf("Sum of digits :%d\n", sum);
}

/* Program to reverse given number */

#include <stdio.h>

void main()
{
int num, digit, rev;

printf("Enter number :");


scanf("%d", &num);

rev = 0;
while (num > 0)
{
digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}
printf("Reverse of number is :%d\n", rev);
}

/* Program to verify if given number is perfect */


/* A number is called perfect if sum of its divisors (excluding
the number) is equal to the number itself.
For eg., divisors of 6 are 1, 2, 3 (excluding 6 itself).
Sum of divisors = 1+2+3 = 6. Hence, 6 is perfect number.
*/

#include <stdio.h>
void main()
{
int n, i, sum;
printf("Enter number : ");
scanf("%d", &n);

sum = 0;
for (i=1; i<n; i++)
if ( n % i == 0 )
sum += i;
if (sum == n )
printf("%d is perfect \n", n);
else
printf("%d is not perfect \n", n);
}

/* program to print all perfect numbers 1 to given number */


#include <stdio.h>
void main()
{
int x, n, i, sum;

printf("\nEnter number :");


scanf("%d", &x );

for(n=1; n<=x; n++)


{
sum = 0;
for(i=1; i<n; i++)
{
if (n%i == 0)
sum += i;
}
if (sum == n)
printf("%5d", n);
}

/* Program to check if given number is strong number */’


/* A number is said to be strong number if the sum of each digit
factorial of the number is equal to the number itself.
For eg., 145 -> 1! + 4! + 5! = 145. Hence, 145 is called strong
number */

#include <stdio.h>

void main()
{
int num, temp, digit, i;
long int f, sum;

printf("Enter number :");


scanf("%d", &num);

temp = num;
sum=0;
while (num > 0)
{
digit = num % 10;

f=1;
for(i=1; i<=digit; i++)
{
f *= i;
}

sum += f;

num /= 10;
}

if (temp == sum)
printf("%d is a strong number", temp);
else
printf("%d is not a strong number", temp);

/* Program to print all strong numbers between 1 and given number


*/
#include <stdio.h>

void main()
{
int x, num, temp, digit, i;
long int f, sum;

printf("Enter number :");


scanf("%d", &x);

for (num=1; num<=x; num++)


{
temp = num;
sum = 0;

while (num > 0)


{
digit = num % 10;

f = 1;
for(i=1; i<=digit; i++)
{
f *= i;
}
sum += f;
num /= 10;
}

if (temp == sum)
printf("%5d", temp);

num = temp;
}
}

You might also like