184 Programming and Data Structures
i++;
}
printf ("tt Outer Loop (i) Completed ");
OUTPUT:
i=lj= l k=l
Inner Loop (k) Completed.
i=l j=2 k=l
Inner Loop (k) Completed.
Middle Loop (j) Completed.
i=2 j=l k=l
Inner Loop (k) Completed.
i=2 j=2 k=l
Inner Loop (k) Completed.
Middle Loop (j) Completed.
i=3 j=l k-1
Inner Loop (k) Completed.
i--3 j~2 k=l
Inner Loop (k) Completed.
Middle Loop (j) Completed.
O uter Loop (i) Completed.
Explanation In the above program variable i , j # & k are declared and initialized to 1. The inner
most loop is ' k ', the middle is ' j 7 and the outer most is ' i ' . The execution of loop starts from outer
to inner and the completion will be from the inner most to the outer most. Here, for example the values
of i , j & k are printed and messages are printed to understand the termination of loops.
6.5 THE do-while
The format of do-while loop in C is as follows.
do
(
statement/s;
}
while (condition);
The difference between thewhile and do-while loop is in the place where the condition is to be tested.
In the while loops the condition is tested following the w hile statement and then the body gets
executed. Where as in the d o-w h ile, 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 whi le loop executes
until the condition becomes false.
Loop Control Statements 185
6.71 Use the do while loop and display a message "This is a program of do while loop." for
5 times.
mainO '
{
Int i*l;
clrscrO;
do
{
printf ("N this is a program of do while loop/');
i++;
}
while (i<=5);
.QUTrUT;
This is a program of do while loop.
This is a program of do while loop.
This is a program of do while loop.
This is a program of do while loop.
This is a program of do while loop.
Explanation The body of the loop is executed and the value o f ' i ' is increased. The increased value
is tested with the condition specified at outside of the loop. If the condition is true the statement within
the loop gets executed. In the above program the statement within the loop gets executed 5 times and
the result is as shown above.
6.72 Write a program to print the entered number in reverse order. Use do-while loop. Also
perform Sum and Multiplication with their digits.
xnainO
{
int n,d,x=l,11111=1 , sum=0;
int i;
clrscrO ;
printf("Enter the number of digits:-");
scanf (“% d"M );
printfi'^nEnter the number which is to be reversed
scanf("%d",&:n);
printf ("n Reversed Number ");
do
I
i=n%10;
printf(“%d",i);
sum=sum+i;
mul=mul*i;
n=n/10;
*++;
186 Programming and Data Structures
) while (x<=d);
printf ("n Addition of digits %4d",sum);
printf ("n Multiplication of digits% 4d", mul);
getcheO;
)
QiXDLUIj
Enter the number of digits: 4
Enter the number which is to be reversed:- 4321
Reversed Number 1234
Addition of digits 10
Multiplication of digits:- 24
Explanation In the above program length of the number and a number are entered. Using repetitive
mod operations digits are separated and displayed. The separated digits are repeatedly added and
multiplied with the variables 'sum7 and 'mul' respectively. Initially 'sum' is 0 and 'mul' is 1. After
termination of loop 'sum' and 'mul' displays addition and multiplication of individual digits of the
entered number.
6.73 Attempt the above question by considering the false condition i.e. the value of i should
be initially larger than the value in the while loop.
maln()
{ine 1*7;
clrscrO;
do
I
printf ("n This is a program of do while loop.");
*++;
I
while (i<=5);
}
OUTPUT:
This is a program of do while loop.
Explanation On execution of the above program one can observe that the statement within the loop
executes for the first time though the condition is false.
6.74 Write a program to find the cubes of 1 to 10 numbers using do while loop.
/* This is a program of cube of a given number * /
#include "math.h"
main()
{
int y,x=*l;
Loop Control Statements 187
clrscr();
printf ("tt Print the numbers and their cubes ");
printf C'n");
dot
y=pow(x,3);
printf ("%4d %27d ",x,y);
x++;
I
while (x<=10);
I
OUTPUT;
Print the numbers and their cubes
1 1
2 8
3 27
4 64
5 125
6 216
7 343
8 512
9 729
10 1000
Explanation Here, the mathematical functionpow (x, 3) is used. Its meaning is to calculate the
third power ofx. With this function we get the value ofysx3 For use of the pow 0 function we have to
includemath. h header file.
6.75 Write a program to check whether the given number is prime or not?.
mainO
{
int n,x=2;
clrscrO ;
printf ("Enter The number for testing (prime or not):);
scanf (%d"An);
do
{
if(n% x~0)
I
printf ("n The number %d is not prime/'fn);
getchO;
exit(0);
}
x++;
I while (x<n);
printf ("n The number %d is prime/',n);
getcheO;
OUTPUT:
Enter The number for testing (prime or not): 5
The number 5 is prime.
188 Programming and Data Structures
Explanation The number is said to be a prime number if it is not divisible by any number starting
from 2 onwards up to n-1, where 'n ' is the entered number by the user. Here in this example the
entered number is 5. Themod operation is performed using diviser from 2 to n-1 (5-1).
If the remainder is 0 then it is not a prime number & controls exits from the program. Otherwise if the
remainder is non-zero then the number is prime.
6.76 Write a program to count the number of students having age less than 25 and weight
less than 50 Kg out of five.
# include <stdio.h>
# include <conio.h>
void mainO
{
int age, count=0,x=l;
float wt;
clrscrO;
printf (" Ertter data of 5 boysn");
printf (" Age Weightin'');
do
/
scanf ("%d % f, &cage, &civt);
if( age<25 && wt<50)
{
coutit++;
I
*++;
I while (x<=5);
printf ("n Number of boys with age <25 ");
printf ("and weight <50 Kgs =%d ”,count);
getchO;
}
OUTPUT: Enter data of 5 boys
Age Weight
24 51
20 45
25 51
20 35
24 54
Number of boys with age <25 and weight <50 Kgs = 2
Explanation In the above-cited program age and weight of five boys are entered. The i f condition
checks the age and weight and after satisfying the condition counter variable count is increased by
one. Thus loop executes five times. The program displays number of students having age less than 25
years and weight less than 50 kg.
6.77 Compute the factorial of given number using do while loop.
# include <stdio.h>
# include <conio.h>
mainO
..................Content has been hidden....................

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