Thursday 12 September 2013

Even Number Pattern

Q. Write a C program to print the following even number pattern as:

 2
 2 4
 2 4 6
 2 4 6 8

Ans.

/*c program for even number pattern*/
#include<stdio.h>
int main()
{

 int r,c,p;
 for(r=1; r<=4; r++)
 {
   for(c=1,p=2; c<=r; c++,p=p+2)
     printf(" %d",p);
   printf("\n");
 }
 getch();
 return 0;
}

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

Output of even number pattern C program
Figure: Screen shot for even number pattern C program

Friday 6 September 2013

Number Pyramid Structure

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

1 2 3 4 5
2 3 4 5
3 4 5
4 5
5

Ans.

/*c program for number pyramid pattern design*/
#include<stdio.h>
int main()
{

 int num,r,c;
 printf("Enter no of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(c=r; c<=num; c++)
     printf("%d ",c);
  printf("\n");
 }
 getch();
 return 0;
}

/*************************************************************
The output of number pyramid pattern design program would be:
**************************************************************/


Output of Number Pyramid Pattern C program
Figure: Screen-shot for number pyramid pattern C program


Positive-Negative Number Triangle

Q. Write a C program to print the following positive-negative number triangle as:

9
8  6
7  5  3
4  2  0 -2
1 -1 -3 -5 -7

Ans.

/*c program for positive-negative number triangle*/
#include<stdio.h>
int main()
{

 int num,n,r,c;
 for(r=1,num=9; r<=5; r++,num--)
 {
   if(r==4)
   {
     for(c=1,n=4; c<=r; c++,n=n-2)
        printf(" %d",n);
   }
   else if(r==5)
   {
     for(c=1,n=1; c<=r; c++,n=n-2)
        printf(" %d",n);
   }
   else
   {
     for(c=1,n=num; c<=r; c++,n=n-2)
        printf(" %d",n);
   }
   printf("\n");
 }
 getch();
 return 0;
}

/***************************************************************
The output of above positive-negative number triangle C program would be:
****************************************************************/

Output of Positive-Negative Number Triangle C program
Figure: Screen shot for Positive-Negative Number
Triangle C program

Thursday 5 September 2013

Equilateral Triangle Number Design

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

     5
    454
   34543
  2345432
 123454321

Ans.

/*c program for equilateral triangle design*/
#include<stdio.h>
int main()
{

 int num,r,c,sp,n;
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(sp=num-r; sp>=1; sp--)
     printf(" ");
  for(n=num-r+1; n<=num; n++)
     printf("%d",n);
  for(c=1,n=num-1; c<r; c++,n--)
     printf("%d",n);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of Equilateral Triangle design C program
Figure: Screen shot for Equilateral Triangle design C program


Z-Shape Pyramid

Q. Write a C program to print the Z-Shape pyramid as:

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

Ans.

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

 int num,r,c,sp,n;
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
    printf("*");
 printf("\n");
 for(r=1,n=num-1; r<=num-2; r++,n--)
 {
  for(sp=1; sp<n; sp++)
     printf(" ");
  printf("*");
  printf("\n");
 }
 for(r=1; r<=num; r++)
    printf("*");
 printf("\n");
 getch();
 return 0;
}

The output of above program would be:

Output of Z-Sahpe pyramid C program
Figure : Screen shot for Z-Shape pyramid C program

Number Triangle

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

     1
    21
   321
  4321
 54321

Ans.

/*c program for number pyramid*/
#include<stdio.h>
int main()
{

 int num,r,c,sp;
 printf("Enter no of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(sp=num-r; sp>=1; sp--)
     printf(" ");
  for(c=r; c>=1; c--)
     printf("%d",c);
  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

Sunday 1 September 2013

Increased Number Triangle

Q. Write a C program to print the following Increased Number Triangle as:

    5
   45
  345
 2345
12345

Ans.

/*c program for increased number triangle*/
#include<stdio.h>
int main()
{

 int num,r,n,sp;
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(sp=num-r; sp>=1; sp--)
     printf(" ");
  for(n=num-r+1; n<=num; n++)
     printf("%d",n);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

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


Decreased Number Triangle

Q. Write a C program for decreased number triangle as:

5
54
543
5432
54321

Ans.

/*c program for decreased number triangle*/
#include<stdio.h>
int main()

{
 int num,r,c,n;
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
   for(c=1,n=num; c<=r; c++,n--)
       printf("%d",n);
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

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



Half-Square Number Triangle

Q. Write a C program to print the following half-square number triangle C program as : 

543212345
 4321234
  32123
   212
    1

Ans.

/*c program for half-square number triangle*/
#include<stdio.h>
int main()
{
 int num,r,c,n,sp,p;

 printf("Enter any number : ");
 scanf("%d", &num);
 for(r=1,n=num; r<=num; r++,n--)
 {
   for(sp=r; sp>1; sp--)
      printf(" ");
   for(c=r,p=n; c<=num; c++,p--)
      printf("%d",p);
   for(c=num-r,p=2; c>=1; c--,p++)
      printf("%d",p);
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of Half-Square Number Triangle C program
Figure :  Screen shot for Half-Square Number Triangle C program


Odd-Number Triangle

Q. Write a C program to print the following odd-number triangle C program as:

135
11111
33333
55555

Ans.

/*c program for odd number triangle*/
#include<stdio.h>
int main()
{

 int num=5,r,c,p;
 //as num=5 is given 
 for(r=1,p=1; r<num-1; r++,p=p+2)
    printf("%d",p);
 printf("\n");
 for(r=1,p=1; r<num-1; r++,p=p+2)
 {
  for(c=1; c<=num; c++)
     printf("%d",p);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of odd number triangle C program
Figure : Screen shot for odd number triangle C program


V-Shape Pyramid

Q. Write a C program to print the following V symbol star pyramid as:

*     *
 *   *
  * *
   *

Ans.

/*c pyramid program for v-symbol*/
#include<stdio.h>
int main()
{

 int num=4,r,c,sp;
 for(r=1,c=num+1; r<=num; r++,c=c-2)
 {
   for(sp=r; sp>1; sp--)
      printf(" ");
   printf("*");
   for(sp=c; sp>=1; sp--)
      printf(" ");
   if(c>=1)
      printf("*");
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of V-Shape C pyramid program
Figure : Screen shot for V-Shape pyramid C program


Tuesday 27 August 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 26 August 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 25 August 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 23 August 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

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