Sunday, August 25, 2013

Binary Geometric Number Pyramid

Q. Write a C program to print the following binary geometric number pyramid as:

 1
 2 3
 4 5 6 7
 8 9 10 11 12 13 14 15
 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

or

 7
 14 15
 28 29 30 31
 56 57 58 59 60 61 62 63

[How to create : in above pyramid design, left hand side vertical left align elements are as 1, 2, 3, 4, 8 as geometric sequence numbers. And each these number increase to +1, till equal the binary number as 1, 2, 4, 8, 16 and so on.
]

Ans.

/*c program for generating geometric sequence of any number*/
#include<stdio.h>
int main()
{
 int num,n,r,c,rows,x=1;

 printf("Enter any number : ");
 scanf("%d", &num);
 printf("Enter total rows : ");
 scanf("%d", &rows);
 for(r=1; r<=rows; r++,x=x*2)
 {
   n=x*num;
   for(c=1; c<=x; c++)
       printf(" %d",n++);
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of binary geometric sequence pyramid C program
Figure : Screen shot for binary geometric sequence
 pyramid C program


Output of binary geometric sequence pyramid C program
Figure : Screen shot for binary geometric sequence
 pyramid C program


No comments:

Post a Comment