The While loop in C

It is often the case in programming that you want to do something a fixed number of times. Perhaps you want to calculate gross salaries of ten different persons, or you want to convert temperatures from centigrade to Fahrenheit for 15 different cities.

Let us look at a example which uses a while loop. The flowchart shown below would help us to understand the operation of the while loop.

while loop in c
while loop in c

/* Calculation of simple interest for 3 sets of p, n and r */
main( )
{
int p, n, count ;
float r, si ;

count = 1 ;

while ( count <= 3 )
{
printf ( “\nEnter values of p, n and r ” ) ;

scanf ( “%d %d %f”, &p, &n, &r ) ;
si = p * n * r / 100 ;

printf ( “Simple interest = Rs. %f”, si ) ;

count = count + 1;
}
}

working of while loop in c
working of while loop in c

 

Related Posts

  • 44
    In the First C Program discussed earlier (same has been also mentioned below for the reference) we assumed the values of p, n and r to be 1000, 3 and 8.5. Every time we run the program we would get the same value for simple interest. If we want to calculate…
    Tags: values, printf, scanf, interest, simple, enter, number, si, programming, learn
  • 39
    Problem Statement: If the marks obtained by a student in five different subjects are input through the keyboard, find out the aggregate marks and percentage marks obtained by the student. Assume that the maximum marks that can be obtained by a student in each subject is 100. Answer: /* Calculation…
    Tags: printf, calculation, learn, example, calculate, programming
  • 36
    Problem Statement: Write a program to calculate the salary as per the following table: Solution: /* C program to calculate salary */ main( ) { char g ; int yos, qual, sal ; printf ( "Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):" )…
    Tags: calculate, example, learn, programming
  • 34
    Problem Statement: If a four-digit number is input through the keyboard, write a program to obtain the sum of the first and last digit of this number. Solution: /* Sum of 1st and last digit of a four digit number */ #include<stdio.h> #include<conio.h> main() { int n,a,sum=0; clrscr(); printf("\Enter a…
    Tags: number, learn, example, programming
  • 34
    The if statement by itself will execute a single statement, or a group of statements, when the expression following if evaluates to true. It does nothing when the expression evaluates to false. Can we execute one group of statements if the expression evaluates to true and another group of statements…
    Tags: rs, gross, learn, example, programming

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: