C Programming

C Programming


Number Character Triangle

Posted: 21 Apr 2013 11:41 AM PDT

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

1
21A
321AB
4321ABC
54321ABCD

Ans.

/*c program for number character triangle*/
#include<stdio.h>
int main()
{
 int num,r,c,p,z;

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

The output of above program would be:

Output of number character pyramid C program
Figure: Screen shot for number character pyramid C program


Odd Number Triangle

Posted: 21 Apr 2013 08:20 AM PDT

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

 0
 1 0 1
 2 1 0 2 1
 3 2 1 0 1 2 3

Ans.

/*c program for odd number triangle*/
#include<stdio.h>
int main()
{
 int num,r,c,q,s;
 printf("Enter ending number : ");
 scanf("%d", &num);
 for(r=0; r<=num; r++)
 {
  for(c=0,s=r; c<=r; c++,s--)
     printf(" %d",s);
  for(q=1; q<=r; q++)
     printf(" %d",q);
  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

Continue Character-Number Pyramid

Posted: 21 Apr 2013 08:04 AM PDT

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

1
A B
2 3 4
C D E F
5 6 7 8 9

Ans.

/*c program character number pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c;
 static int i=1;
 static char ch='A';
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(c=1; c<=r; c++)
  {
    if(r%2==0)
       printf(" %c",ch++);
    else
       printf(" %d",i++);
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of character number pyramid C program
Figure: Screen shot for character-number pyramid C program

Differ Symbol Pyramid

Posted: 21 Apr 2013 07:23 AM PDT

Q. write C program to print the following symbol pyramid as:

*
$$
***
$$$$

Ans.

/*c program for symbol pyramid*/
#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=1; c<=r; c++)
   {
     if(r%2==0)
        printf(" $");
     else
        printf(" *");
   }
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of differ symbol pyramid C program
Figure: Screen shot for differ-symbol pyramid C program

Character - Symbol Pyramid

Posted: 21 Apr 2013 07:16 AM PDT

Q. Write a C program to print the following character and symbol design:

A
**
ABC
****
ABCDE
****
ABC
**
A

Ans.

/*c program for character and star pyramid*/
#include<stdio.h>
int main()
{
 char ch,r,c,q;
 printf("Enter ending character : ");
 scanf("%c", &ch);
 if(ch>='a' && ch<='z')
    ch=ch-32;
 for(r='A'; r<=ch; r++)
 {
  for(c='A'; c<=r; c++)
  {
    if(r%2==0)
       printf("*");
    else
       printf("%c",c);
  }
  printf("\n");
 }
 for(q=ch-1; q>='A'; q--)
 {
  for(c='A'; c<=q; c++)
  {
    if(q%2==0)
       printf("*");
    else
       printf("%c",c);
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:
Output of character symbol pyramid C program
Figure: screen shot for character star pyramid C program

-------------------------------------------


Q. Write a C program to print the following number and symbol design:

1
**
123
****
12345
****
123
**
1

Ans.

/*c program for number and star pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c,n;
 printf("Enter ending number : ");
 scanf("%d"&num);
 for(r=1; r<=num; r++)
 {
  for(c=1; c<=r; c++)
  {
    if(r%2==0)
       printf("*");
    else
       printf("%d",c);
  }
  printf("\n");
 }
 for(n=num-1; n>=1; n--)
 {
  for(c=1; c<=n; c++)
  {
    if(n%2==0)
       printf("*");
    else
       printf("%d",c);
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of number symbol pyramid C program
Figure: Screen shot for number symbol pyramid C program

Odd Number Triangle

Posted: 21 Apr 2013 05:29 AM PDT

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

1
1 3
1 3 5
1 3 5 7
1 3 5 7 9

Ans.

/*c program for odd number triangle*/
#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,z=1; c<=r; c++,z=z+2)
       printf("%d ",z);
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

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



Post a Comment