C Programming

C Programming


Number Triangle Design

Posted: 23 Aug 2013 06:17 AM PDT

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

Posted: 23 Aug 2013 05:24 AM PDT

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

Posted: 23 Aug 2013 04:06 AM PDT

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

Posted: 23 Aug 2013 12:21 AM PDT

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


Vertical Continues Number Pyramid

Posted: 23 Aug 2013 12:01 AM PDT

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



Post a Comment