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()