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