How To Print "HAPPY" In A Triangle In C++?

How to print the following triangle in C.?

  • Its an equilateral triangle with numbers as follows :- 1 121 12321 1234321 123454321

  • Answer:

    include<stdio.h> void main() { int i,j; clrscr(); for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d",j); } for(j=i-1;j>=1;j--) { printf("%d",j); } printf("\n"); } getch(); }

neville at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Like this: /*     To display:           1         121       12321     1234321     ... */ #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) {     int i,n,m,k;     char *s, *spaces;     if ((argc < 2) ||             (sscanf(argv[1],"%d",&n) != 1) ||             ((n = abs(n)) > 9)) {         printf("usage : %s <n>\n",argv[0]);         puts(" where n is an integer, and |n| < 10");         return -1;     }     spaces = s = calloc(n, sizeof(char));     memset(spaces, ' ', n-1);     for (m = i = k = 1; m <= n; m++, spaces++, i = k = 1) {         printf("\n%s",spaces);         do {             printf("%d",i);             if (i == m) k = -1;         } while ((i += k) != 0);     }     free(s);     return 0; }

include <stdio.h> void main() { int i,j,k; for (i = 1; i <= 11; i=i+2) { for (j = 1,k=i; j <= i; j++,k--) { if (i == 1) printf("%d", j); else if (j <= i / 2) printf("%d", j); else printf("%d", k); } printf("\n"); } }

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.