Monday, February 17, 2014

Square Number Triangle

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

1
1 4 9
1 4 9 16 25
1 4 9 16 25 36 49
1 4 9 16 25 36 49 64 81

Ans.

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

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

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

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

Related Program:


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

Number Pyramid Pattern

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

1  2  3  4  5
15 14 13 12
6  7  8
11 10
9

Ans.

/*c program for square number pyramid pattern*/
#include<stdio.h>
int main()
{
 int r,c,s1=1,s2=15;
 for(r=5; r>=1; r--)
 {
  if(r%2==0)
  {
    for(c=1; c<=r; c++,s2--)
       printf(" %d",s2);
  }
  else
  {
     for(c=1; c<=r; c++,s1++)
       printf(" %d",s1); 
  }
  printf("\n");
 }
 getch();
 return 0;
}

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


Output of square number pyramid pattern C program
Figure: Screen shot for square number pyramid C program

Number Triangle Pattern

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

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

Ans.

/*c program for triangle number pattern*/
#include<stdio.h>
int main()
{
 int num,r,c,q=1;
 for(r=1; r<=5; r++,q=q+2)
 {
  for(c=1; c<=q; c++)
     printf(" %d",c);
  printf("\n");
 }
 getch();
 return 0;
}


/**********************************************************
The output of above program would be:
***********************************************************/
Output of number triangle pattern C program
Figure: Screen shot for number triangle pattern C program

Related Programs:


1
1 4 9
1 4 9 16 25
1 4 9 16 25 36 49
1 4 9 16 25 36 49 64 81

Thursday, February 6, 2014

Equal Character Triangle

Q. Write a C program to print the following equal character triangle pattern as:

A
BAB
CBABC
DCBABCD
EDCBABCDE

Ans.

/*c program for Equal Character Triangle pattern*/
#include<stdio.h>
int main()
{

 char ch,r,c;
 printf("Enter Any Character : ");
 scanf("%c", &ch);
 if(ch>='a' && ch<='z')
    ch=ch-32;
 for(r='A'; r<=ch; r++)
 {
   for(c=r; c>='A'; c--)
      printf("%c",c);
   for(c='B'; c<=r; c++)
      printf("%c",c);
   printf("\n");
 }
 getch();
 return 0;
}

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


Output of equal character triangle C program
Figure: Screen shot for equal character triangle C program


Related Program:

Equal Number Triangle C program:

1
212
32123
4321234
543212345

Equal Number Triangle

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

1
212
32123
4321234
543212345

Ans.

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

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

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


Output of equal number triangle pattern C program
Figure: Screen shot for equal number triangle C program

Related Programs:

Equal Character Triangle as:

A
BAB
CBABC
DCBABCD

Saturday, February 1, 2014

Character Pyramid

Q. Write a C program to takes the last character by user and print the following character pyramid as:
(if user input : j), then output would be like as:

A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
ABCDEFGFEDCBA
ABCDEFGHGFEDCBA
ABCDEFGHJHGFEDCBA
ABCDEFGHGFEDCBA
ABCDEFGFEDCBA
ABCDEFEDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A

Ans.

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

 char ch,r,c,p;
 printf("Enter any character : ");
 scanf("%c", &ch);
 if(ch>='a' && ch<='z')
   ch=ch-32;
 for(r='A'; r<=ch; r++)
 {
   for(c='A'; c<=r; c++)
      printf("%c",c);
   for(c=r-1; c>='A'; c--)
      printf("%c",c);
   printf("\n");
 }
 for(p=ch-1,r='A'; r<ch; r++,p--)
 {
   for(c='A'; c<=p; c++)
      printf("%c",c);
   for(c=p-1; c>='A'; c--)
      printf("%c",c);
   printf("\n");
 }
 getch();
 return 0;
}

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


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