Loop Control Statements 155
{
in t x,i,jj
c lr s c r O ;
printf ("How many lines stars (*) should be printed?:");
scanf ("%d",&cx);
for (i=lrf<=x;i++)
I
for (j=l;j<=i;j++)
I
printf (" * ");
I
printf ("n");
I
I
o u t p u t :
How many lines stars (*) should be printed ? : 5
* *
* * *
* * *
* * * * »
Explanation Here in the above example the two fo r loops are used for displaying as per the
format shown in the output. Variables x , i & j are used according to the requirement. On execution
of inner loop star/stars are displayed. To display them in different lines second p r in tf () statement
is used.
6.41 Write a program to generate the pattern of numbers as given under.
6 5 4 3 2
1 0
5 4 3 2 1
4 3 2 1 0
3 2 1 0
2 1 0
1 0
0
# in clud e
< std io .h >
# in clud e
<con io.h>
void m ain()
{
156 Programming and Data Structures
in t i,c = 0 ;
clr s crO ;
printf ("Enter a Number
scanf ("% d",&ci);
fo r ( /n>=0,n)
{
c=i;
printf ("n");
for(;;)
{
printf ("%3d",c);
if (c==0)
break;
c—;
I
I
1
OUTPUT;
6 5 4 3 2 1 0
5 4 3 2 1 0
4 3 2 1 0
3 2 1 0
2 1 0
1 0
0
Explanation An integer value is read for variable ' i '. The first for loop decreases the value of
' i '. The value of ' i ' is assigned to variable ' c '. In the second infinite for loop the value of ' c ' is
printed and decreased. The i f condition terminates the infinite for loop when it finds the value of
' c ' is 0.
6.42 Write a program to display the series of numbers as given below.
1 2 3
1234
4 3 2 1
3 2 1
2 1
Loop Control Statements 157
# include <stdio.h>
# include <conio.h>
void mainO
{
in t i , j , x ;
printf ("nEnter Value of x : " ) ;
scanf (m%d",£x);
c l r s c r ( );
fo r (j=l,j<=x;j++)
1
fo r (i=l,i<=j,i++)
printf ("%3d",i);
printf (n");
i
printf C'n");
fo r (j=x,j>=l;j-)
{
fo r (i=j,-i>=l,'i)
printf ("%3d",i);
printf C'n");
}
OUTPUT:
Enter Value of x : 4
1 2
1 2 3
1234
4 3 2 1
3 2 1
2 1
Explanation The first two for loops are used to display the first four lines of the output. The next
two for loops are used to display the last four lines of the output. The outputs of the first four lines
are in ascending order whereas the last four lines of the numbers are in descending order.
158 Programming and Data Structures
6.43 Write a program to display the series of numbers as given below.
1
21
3 2 1
4321
4321
321
21
1
# include <stdio.h>
# include <conio.h>
void mainO
{
in t i , j , x;
prin tf ("nEnter Value of x ;
scanf ("%d",&x);
c l r s c r ();
for (j=l;j<=x;j++)
I
fo r (i=j;i>=l;i~)
i
printf ("%3d",i);
I
printf ("n");
}
printf (" ");
for (j=x;j>=l;j~)
I
for (i=j;i>=l;i~)
I
printf ("%3d",i);
)
printf (" ");
I
OUTPUT:
Enter Value of x : 4
1
21
3 2 1
4321
4321
3 2 1
2 1
1
Loop Control Statements 159
Explanation The logic of the program is same as the previous example. The difference is only of
displaying the numbers in different fashion. This is accomplished by using the fo r loop in different
manner.
6.44 Write a program to generate the pyramid structure using numerical.
# in clud e < std io .h >
# in clu d e <conio.h >
void main()
{
int k ,i,j,x,p=34;
p r in tf ("n E n te r A number
sca n f ("% d",& x);
c l r s c r ( ) ;
fo r (j=0;j<=x;j++)
i
gotoxy(p,j+l);
I*position curosr on screen (x cordinate,y cordinate) */
fo r (i=0-/;f<=/;i++)
printf ("%3d”,abs(i));
p=p-3;
OUTPUT:
Enter A number : 3
0
101
2 10 12
3 21 0123
Explanation
Here in the above program 'p ' is equated to 34. This number decides the 'x ' co
ordinate on the screen from where the numbers are to be displayed. The 'y ' co-ordinate is decided
by j +1 where 1 j ' is varying from 0 to entered number. The value of 1 i ' is negative towards the
left of zero. Hence its absolute value is taken. The inner fo r loop is executed for displaying digits
towards the left and right of zero.
6.45 Write a program to convert binary to decimal number.
# in clu d e < s td io.h >
# in clu d e <con io.h >
void m ain()
..................Content has been hidden....................

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