Header Ads

Diamond shape Pyramid using integers

This program is used to make the diamond pattern using asterisk symbol. So first of all, you have to include the stdio header file using the "include" preceding # which tells that the header file needs to be process before compilation, hence named preprocessor directive.
Then you have to define the main() function and it has been declared as an integer so by default it returns an integer. Inside the main() function you have to declare four integer type variable name - 'n', 'c', 'k', 'space' and initialize the value of space as '1'. Then a printf() function is used which will print the message - "Enter number of rows". The scanf() function ta=hen takes the value from the user and puts in variable 'n'.

************************Program*************************


#include < stdio.h >
#include < conio.h >

  main() {
    int r, c, k, m, i;
    printf("Enter the number of lines : ");
    scanf("%d", & m);
    for (r = 1; r <= m - 1; r++) {
      i = 0;
      for (c = 1; c <= m - r; c++) {
        printf(" ");
      }
      for (k = 1; k <= r; k++) {
        printf("%d ", k);
        i++;
      }
      printf("\n");
    }
    for (r = 0; r <= m - 1; r++) {
      i = 0;
      for (k = 1; k <= r; k++) {
        printf(" ");
      }
      for (c = 1; c <= m - r; c++) {
        printf("%d ", c);
        i++;
      }
      printf("\n");
    }
    getch();
    return 0;
  }


************************Output***************************


Output of diamond shape integer's pyramid

No comments