140 Programming and Data Structures
6.25 Write a program to enter mantissa and exponent. Calculate the value of m Where
'm' is a mantissa and 'x' is an exponent.
mainO
{
int i,number,power;
long ans;
c l r s c r ();
printf ("n Enter Number
scanf ("%d",&:number);
printf ("n Enter Power :");
scanf ("%d",&power);
ans=l;
fo r (i=l,n<=power;i++)
ans*-number;
printf (" The Power of%d raised to %d is %ldn", number,powermans);
getcheO;
I
OUTPUT:
Enter Number : 5
Enter Power : 3
The Power of 5 raised to 3 is 125.
Explanation In the above program mantissa and exponent are entered. The first number is mantissa
and the second is its power. The variable ' ans' stores repetitive multiplication of mantissa till the
loop is active. Finally it displays the power of the given number.
6.3 NESTED for LOOPS
We can also use loop within loops. In nested for loops one or more for statements are included in
the body of the loop. In other words C allows multiple for loops in the nested forms. The numbers
of iterations in this type of structure will be equal to the number of iterations in the outer loop
multiplied by the number of iterations in the inner loop. Given below examples are based onNes ted
for loops.
6.26 Write a program to perform subtraction of two loop variables. Use nested for loops.
# include <stdio.h>
# include <conio.h>
void mainO
{
int a,b,sub ;
c l r s c r ();
fo r (a=3;a>=l;a~)
Loop Control Statements 141
I
for (b=l;b<=2;b++)
{
sub=a-b;
printf ("a=%d b=%d a-b = %d ",a,b,sub);
I
OUTPUT:
a=3 b=l a-b = 2
a=3 b=2 a-b = 1
a=2 b=l a-b = 1
a=2 b=2 a-b = 0
a=l b=l a-b = 0
a=l b=2 a-b =-1
Explanation In the above program the outer loop variable and inner loop variables are 'a ' and
' b ' respectively. For each value o f' a ' the inner loop is executed two times. The inner loop terminates
when the value o f ' b ' exceeds 2 and the outer loop terminates when the value of 1 a ' is 0. Thus, the
outer loop executes 3 times and for each outer loop the inner loop executes two times. Thus, the total
iterations are 3 x 2 = 6 and 6 output lines are shown in the output.
6.27 Write a program to illustrate an example based on nested for loops.
# in clu d e <std io .h >
# in clu d e < con io.h >
void mainO
{
in t i,j;
c l r s c s ( ) ;
fo r (i=l,'i<=3,'i++) /* outer loop */
I
fo r (j-l;j<=2;j++) /* inner loop */
printf ("n i* j: %d",i*j);
)
OUTPUT:
i*j = l
i*j= 2
i*j = 2
142 Programming and Data Structures
i*j = 4
i*j = 3
i*j = 6
Explanation Here, in the above example for each value of ' i ' the inner loop's j value varies
from J to 2/Outer loop executes three times whereas the inner loop executes six times. The inner
loops terminates when the value of j = 2 and the outer loop terminates when the value of i =3.
The total number of iterations=outer loop iterations (3) * inner loop iterations (2) = 6.
6*28 Write a program using nested for loops. Print values and messages when any loop
ends.
# include <stdio.h>
# include <conio.h>
void mainO
{
in t a ,b , c ;
c l r s c r ( ) ;
for (a=l;a<=2;a++) I* outer loop *1
I
fo r (b=l;b<=2;b++) I* middle loop *1
/
fo r (c=l;c<=2;c++) /* inner loop */
printf ("n a=%d + b=%d + c=%d : %d",a,b,c,a+b+c);
printf ("n Inner Loop Over.");
I
printf (n Middle Loop Over.");
I
printf ("n Outer Loop Over.'');
)
Explanation The execution of the above program will be done by C compiler in sequence as per in
the Table given below. The total number of iterations are equal to 2 * 2 *2 = 8. The final output
provides 8 results.
Loop Control Statements 143
Table 6.4 Output of program 6.27
Values of loop variables Output
Outer Middle
Inner
1) a= l
b= l
C = 1
a+b+c=3
2) a=l
b= l
c=2
a+b+c=4
Inner Ldop Over
3) a=l
b=2
C = 1
a+b+c=4
4) a=l b=2
c=2
a+b+c=5
Inner
Loop Over
Middle Loop Over
5) a=2 b=l
C = 1
a+b+c=4
6) a=2 b=l
c=1
a+b+c=5
Inner
Loop Over
7) a=2
b=2
C = 1
a+b+c=5
7) a=2
b=2 c=2 a+b+c=6
Inner
Loop Over
Middle Loop Over
Outer Loop Over.
6.29 Write a program to find perfect cubes up to given number.
/* 1,8,27,64 are perfect cubes of 1,2,3 and 4 *1
# include<math.h>
mainO
{
int i , j , k;
c l r s c r ();
printf ("Enter a Number
scanf ("%d",&k);
fo r (i=l;i<k,~i++)
I
for (j=l;j<=i;j++)
I
if (i==pow(j,3))
printf (" Number: %d & it's Cube :%d",j,i);
I
I
144 Programming and Data Structures
OUTPUT :
Enter a Number : 100
Number : 1 & it's Cube : 1
Number : 2 & it's Cube : 8
Number : 3 & it's Cube : 27
Number : 4 & it's Cube : 64
Explanation Here in the above program the programmer has to enter the number up to which the
perfect cubes are to be obtained. For example if the user enters 100 the perfect cubes up to 100 are
1,8,27 & 64. Here the two f o r loops are used. The outer f o r loop varies up to the entered number.
The inner f o r loop varies up to value of ' i '. For each value of 1 j ' its cube is calculated and
checked with the value of % i '. If the I f condition is true the perfect cubes is printed otherwise the
loop continues. Thus, all the perfect cubes up to a given number are displayed.
6.30 Write a program to display numbers 1 to 100 using A SC II values from 48 to 57. Use
nested loops.
# include <stdio.h>
main()
{
in t i f j= 0 ,k = -9 ;
c l r s c r ();
printf ("t Table o f l to 100 Numbers Using ASCII Values n");
printf ("f==== == = == === ======= ===== ===== ======«");
for (i=48;i<=57;i++,k++)
fo r (j=49;j<=57;j++)
printf ("%5c%c",i,j);
if (k!=l)
printf (“ %c%c",i+l,48);
if (k==0) printf (“b%d%d%d",k+l,k,k);
printf (“ ");
I
OUTPUT:
Table of 1 to 100 Numbers Using ASCII Values
01 02 03 04 05 06 07 08 09 10
11 12 13 14 15 16 17 18 19 20
..................Content has been hidden....................

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