Loop Control Statements 169
else
printf Cn(***Fail****)");
I
OUTPUT:
Enter number of Students : 1
Enter Roll Number : 1
Enter Marks of 6 Subjects for Roll no 1 :
42 52 62 72 82 92
TOTAL MARKS = 402
(First Division)
Explanation In the above program number of students whose result is to be calculated is entered.
After this marks of six subjects with roll numbers are entered. The sum of six subjects is calculated.
The sum is compared with DISTINCTION, FIRST & SECOND macros using i f - else ladder and
appropriate result is displayed.
In this program macros are used. During preprocessing, the preprocessors replace every occurrence
of DISTINCTION with 420, similarly, FIRST and SECOND are replaced with 360 & 240 respectively.
DISTINCTION, FIRST & SECOND in the above program are "macros templates", where as 420,360
& 240 are called their corresponding macro expansions. For details of Macro refer the Preprocessor
chapter.
6.4 THE while LOOP
Another kind of loop structure in C is the while loop. It's format is
Syntax:
while (test condition)
{
body of the loop
}
The test condition may be any expression. The loop statements will be executed till the condition is
true i.e. the test condition is evaluated and if the condition is true, then the body of the loop is
executed. When the condition becomes false the execution will be out of the loop.
170 Programming and Data Structures
The execution of the loop can be followed by the following flow chart.
Entry
Steps of while loops are as follows
1) The test condition is evaluated and if it is
true, the body of the loop is executed.
2) On execution of the body, test condition is
repetitively checked and if it is rue the body
is executed.
3) The process of execution of the body will be
continue till the test condition becomes false.
The control is transferred out of the loop.
4) The control is transferred out of the loop
The block of the loop may contain a single statement or a number of statements. The same block can be
repeated.
The braces are needed only if the body of the loop contains more than one statement. However, it is
a good practice to use braces even if the body of the loop contains only one statement.
6.53 Write a program to print the string "You have learnt C program" 9 times using while
loop.
mainQ
{
int x*l;
while (x<10)
{
printf ("tt You have learnt Cprogram");
x++;
I
OUTPUT:
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
Explanation The parenthesis after 'w hile' contains a condition. As long as condition remains true
all the statements within the body of the loop gets executed repeatedly. The variable ' x is initialized
Loop Control Statements 171
to 1. The compiler checks the condition and after satisfying it the body of the loop is executed. The
control then goes to the while loop. Next time the value of ' x' is increased by 1. Now ' x' is 2 again it
satisfies the condition and the body of the loop gets executed. This process is continued till the value
of ' x ' reaches to 9. When the condition becomes false (after 9) the control passes to the first statement
that follows the body of the while loop and the program is now terminated. The output of the program
is shown above.
6.54 Write a program to add 10 consecutive numbers starting from 1. Use the while loop,
main ()
{
int a=l,sum*:0;
clrscr();
while (a<=10)
I
printf ("%3d",a);
sum=sum+a;
a++;
I
printf (" Sunt of 10 numbers :%d",sum);
I
OUTPUT :12345678910
Sum of 10 numbers: 55
Explanation In the above program integer variable ' a ' is initialized to 1 and variable ' sum' to 0.
The while loop checks the condition for a<=10. The variable ' a ' is added to variable ' sum' and each
time ' a' is increased by 1. In each while loop 'a' is increased and added to ' sum'. When the value
of ' a ' reaches to 10, the condition given in while loop is false. At last the loop is terminated. The sum
of the number is displayed.
6.55 / 6.56 Write a program to calculate factorial of a given number. Use while loop,
main ()
{
int a,£act»l;
clrscrO;
printf (" Enter The Number
scanf ("%d", &a);
while (a>=l)
I
printf ("%d*",a);
fact=fact*a;
a-;
}
printf (" = %d",fact);
printf (" Factorial of Given number is %d",fact);
OUTPUT:
Enter The Number: 5
5* 4 » 3 * 2 * i * _ 120
Factorial of Given number is 120
172 Programming and Data Structures
Explanation In the above program working of while loop is same as the previous one. Only difference
is that the variable 'a' is decreased. Factorial of a number means product from 1 to that number. Here,
variable ' f a c t' is initialized to 1. For each iteration of while loop the entered number is multiplied
with previous value of variable 'f a c t' and 'a ' is decreased. When the entered number 'a ' reaches to
1, the while loop terminates and 1 f ac t ' variable contains the product of 1 till the entered number.
Here, the entered number is 5 and its factorial value is 120 and the same is displayed.
OR
mainO{ int a ,b * l,f a c t* l;
clrscrO ;
printf ("tt Enter The Number:");
scanf ("%d", &ca);
while (b<=a)
{
printf ("%d*",b);
fact=fact*b;
fr++;
/
printf (" = %d",fact);
printf ("n Factorial of%d is %d",b-l,fact);
}
Qm r UT;
Enter the Number 4
1*2*3*4* = 24
Factorial of 4 is 24.
Explanation The working of the above program is same as the last one. Here, we declare and initialize
one more variable ' b ' to 1. Here, we are keeping the variable ' a ' unchanged. We require an extra
variable to count the number of loops completed by the whi 1 e loop. Once the value of variable ' b '
matches to the entered number ' a ', the while loop terminates. The factorial of a number in variable
' f a c t ' is displayed.
6.57 Write a program to convert decimal number to binary number.
xoainO
{
int x,y*40;
clrscrO ;
printf ("Enter a Number :");
scanf("%d",&x);
printf ("n Binary Number:");
while (x!=0)
{
gotoxy(y~J);
printf (%d",x%2);
x=xJ2;
I
Loop Control Statements 173
Q u m r r
Enter a Number : 25
Binary Number: 11001
Explanation In the above program, the number, which is to be converted into binary, is entered
through the keyboard. The while loop is executed till the value of 'x' becomes non-zero. The body of
the whi 1 e loop contains themod (%) & divide operations. With these operations remainders are
obtained and value of ' x ' is reduced to half. Binary bits corresponding to the decimal number which
are obtained are taken in the reverse order by using go toxy () function. In the statement go toxy (y-
-t * / 4), the first argument (y- -) provides the column position and the second provides the row
number where the output is to be displayed.
Similarly, for conversion of decimal to octal instead of 2 one can use 8 for mod as well as division
operations.
6.58 Write a program to convert decimal number to user defined number system. The base of
the number system may be taken up to 9.
# include <stdio.h>
# include <conio.h>
void main ()
{
Int m,b,y*35;
clrsc rO ;
printf (“Enter the Decimal Number :");
scanf ("%d",San);
printf (“Enter base of number System:");
scanf ("%d",&b);
printf ("The Number Obtained
while (m!=0)
{
gotoxy(y-J);
printf ("%d",m%b);
m=mfb;
I
getcheO;
OUTPUT:
Enter the Decimal Number :50
Enter base of number System :5
The Number Obtained : 200
Explanation The logic of the above program is same as explained in the previous program. Only
difference is that the user has a choice to enter the base of any number system. This program is more
flexible than the previous one.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.101.109