Computer Concept & Programming In C - Unit III - 11

Q.23. Write a program in C to read the age of 100 persons and count the number of persons in the age group 50 to 60. Use for and continue statement. (AKTU. 2011 - 12)
Related Questions -


Q.24 Illustrate the use of continue statement in C with some example.    (AKTU. 2013-14)
Ans.
# include<stdio.h>
# include<conio.h>
# define p 100
         Void main ( )
           {
float age [p];
int i, count;
clrscr ( );
for (i = 0; i < p; i++)
   {
  printf(“\n Enter age of %d person”, (i+1);
  scanf (“%f”, & age[i]);
         }
   for (i = 0; i < p; i++)
      {
        if ((age[i] >= 50 - 0)&&(age[i] <= 60.0))
  {
count ++;
   }
 else
      {
countnue;
       }
       }
  getch( );
}
The while loop: -
The while loop construct contains only the condition. You have to take care of the other elements (initializing and incrementing). The general form of the while loop is:
while (condition)
{
statement(s);
}
The associated statement (s) are executed as long as the condition is TRUE. Usually more than one statement are associated with the while keyword. The following example, displays the string “Hello-world” four times:
# include <stdio.h>
# include <conio.h>
void main ( )
{
int c = 100;
clrscr ( );
while (c < = 103)
{
printf (“\ n Hello-World”);
C++;
}
printf (“\ n Done”)
getch ( );
}
The output is:
Hello World
Hello World
Hello World
Hello World
Done.
The do-while loop: -
The only difference between the do-while and the other two loops is that in the do-while loop the condition comes after the process. It takes the following form:
do
{
statement(s);
}
while (condition);
This means that the process will be executed at least once regardless of the condition evaluation.
Following is an example of the do-while loop.
# include <stdio.h>
void main ( )
{
int c = 100;
clrscr ( );
do
{
printf (“\ n Hello World”);
c++;
}
while (c < = 103);
printf (“\n Done);
getch ( );
}
The output of the program along side is:
Hello World
Hello World
Hello World
Hello World
Done.
Now look at the factorial example using the do-while loop. The same variables are used and the same logic, except for the different construction of the do-while.
# include <stdio.h>
# include <conio.h>
void main ( )
{
int number;
clrscr ( );
long int factorial = 1;
printf (“\ n Enter the number”);
scanf (“%d”, & number);
do
{
factorial = factorial * number - -;
}
while (number > 1);
printf (“\ Factorial of %d = %1d”, number, factorial);
getch ( );
}
the output of the program:
Enter the number: 5
Factorial of 5 = 120.