Tuesday, August 27, 2013

Reverse L-Shape Pyramid

Q. Write a C program to print the following Reverse L-Shape pyramid as:

*************
*
*
*
*
*
*
*


Ans.

/*c program for reverse L-shape pyramid*/
#include<stdio.h>
int main()
{
 int c;

 for(c=1; c<=13; c++)
    printf("*");
 for(c=1; c<=7; c++)
    printf("*\n");
 getch();
 return 0;
}

/**************************************************************
The output of above program would be:
**************************************************************/


Output of reverse L-Shape Pyramid C program
Figure : Screen shot for reverse L-Shape pyramid C program



C Star Triangle

Q. Write a C program to make the following star triangle as:

*
**
**
**
******

Ans.

/*c program for star triangle design*/
#include<stdio.h>
int main()
{
 int r,c,n;
 printf("*\n");

 for(r=1; r<=4; r++)
 {
  if(r<=3)
  {
    for(c=1; c<=2; c++)
        printf("*");
  }
  else
  {
    for(c=1; c<=6; c++)
        printf("*");
  }
  printf("\n");
 }
 getch();
 return 0;
}

/*************************************************************
The output of above program would be:
**************************************************************/


Output of star triangle pyramid C program
Figure : Screen shot for star triangle C program

Monday, August 26, 2013

Nested Star-Hash Pyramid

Q. Write a C program to print the following star-hash pyramid design:

#####*#####
####*#*####
###*###*###
##*#####*##
#*#######*#
*#########*

/**************************************************************
How To Make Above nested Pyramid C program
***************************************************************
So you can see, there are four types pyramid in above design as:

 #####      *         #####
 ####      *#   *      ####
 ###      *##   #*      ###
 ##      *###   ##*      ##
 #      *####   ###*      #
       *#####   ####* 

Hence, now it is easy to make these above pyramid program. So i recommended to make first above four pyramid program, after making these pyramid design, add these all pyramid in single C program and you are done. :) )
**************************************************************/

Ans.

/*c program for nested star-hash pyramid*/
#include<stdio.h>
int main()
{

 int n=5,r,c;
 for(r=1; r<=6; r++,n--)
 {
   /*first pyramid*/
   for(c=1; c<=n; c++)
     printf(" #");
   /*second pyramid*/
   for(c=1; c<=r; c++)
   {
     if(c==1)
        printf(" *");
     else
        printf(" #");
   }
   /*third pyramid*/
   for(c=r; c>1; c--)
   {
     if(c==2)
        printf(" *");
     else
        printf(" #");
   }
   /*fourth pyramid*/
   for(c=n; c>=1; c--)
        printf(" #");
   printf("\n");
 }
 getch();
 return 0;
}

/*************************************************************
The output of above program would be:
**************************************************************/


Output of nested hash-star pyramid C program
Figure : Screen shot for nested Hash-Star
Pyramid C program

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


Friday, August 23, 2013

Number Triangle Design

Q. Write a C program to print the following number triangle or pyramid design as:

1
121
12321
1234321

Ans.

/*c program for number triangle design*/
#include<stdio.h>
int main()
{
 int num,r,c,z;
 printf("Enter any number : ");
 scanf("%d", &num);

 for(r=1; r<=num; r++)
 {
  for(c=1; c<=r; c++)
     printf("%d",c);
  for(z=r-1; z>=1; z--)
     printf("%d",z);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of Number Triangle C program
Figure : Screen shot for Number Triangle C program

Learners also read:

1. Character Triangle pattern as:

A
ABA
ABCBA
ABCDCBA
ABCBA
ABA
A

Alternate Number-Star Pyramid

Q. Write a C program to print the following alternate number-star pyramid/triangle as:

1
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
1

Ans.

/*c program for alternate number-star pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c,z=1,p,n;
 printf("Enter maximum number : ");
 scanf("%d", &num);

 for(r=1; r<=num; r++,z=z+2)
 {
  for(c=1; c<=z; c++)
  {
    if(c%2==0)
       printf("*");
    else
       printf("%d",r);
  }
  printf("\n");
 }
 n=num+3;
/*
NOTE: increase +1 above digit when you increase maximum number to 4 
For example: if you enter:
num=5 then n=num+4
if num=6 then n=num+5 and so on. 
*/
 p=num;
 for(r=num; r>=1; r--,n=n-2,p--)
 {
  for(c=1; c<=n; c++)
  {
    if(c%2==0)
       printf("*");
    else
       printf("%d",p);
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of Alternate Number-Star Pyramid C program
Figure : Screen shot for Alternate Number-Star
 Triangle C program

Number Triangle Design

Q. Write a C program to design the number triangle pyramid as:

1
121
1231
12341
123451

Ans.

/*c program for number triangle design*/
#include<stdio.h>
int main()
{
 int num,r,c,z;
 printf("Enter No. of rows : ");
 scanf("%d", &num);

 for(r=1; r<=num; r++)
 {
  for(c=1; c<=r; c++)
     printf("%d",c);
  for(z=1; z<r; z++)
  {
     printf("%d",z);
     break;
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program  would be:

Output of Number Triangle Design C program
Figure: Screen shot for Number Triangle Design C program

Diagonal Star and Zero Rectangle

Q. Write a C program to print the following diagonal star and zero rectangle structure as:

*000000
0*00000
00*0000
000*000
0000*00
00000*0
000000*

Ans.

/*c program for diagonal star and zero rectangle*/
#include<stdio.h>
int main()
{
 int rows=7,r,c;

 for(r=1; r<=rows; r++)
 {
  for(c=1; c<=rows; c++)
  {
    if( (c==1 && r==1) ||
        (c==2 && r==2) ||
        (c==3 && r==3) ||
        (c==4 && r==4) ||
        (c==5 && r==5) ||
        (c==6 && r==6) ||
        (c==7 && r==7)        
      )
         printf("*");
     else
         printf("0");
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of Diagonal Star and Zero Rectangle C program
Figure: Screen shot for diagonal star and zero
 rectangle C program


Thursday, August 22, 2013

Vertical Continues Number Pyramid

Q. Write a C program to print the following vertical continues number pyramid as:

 1

 23
 4
 56
 7
 89
 10

Ans.

/*c program for vertical continues number pyramid*/
#include<stdio.h>
int main()
{
 int r,c,x,y;

 static int i=1;
 for(r=1; r<=7; r++)
 {
   if(r%2==0)
   {
     for(x=2; x>=1; x--,i++)
        printf("%d",i);
   }
   else
   {
     for(y=1; y>=1; y--,i++)
        printf("%d",i);
   }
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of Vertical Continues Number Pyramid C program
Figure: Screen shot for vertical continues number
 pyramid C program