Write a C program to print?

Write a C program to print

1

212

32123

4321234

543212345

4321234

32123

212

1

Update:

For input 4 and for any inputs

Comments

  • The aim of this exercise may be for you to use nested 'for' loops, but you can do it in one loop; see example below. There actually is a nested loop, but it's hidden in a function call. I also added indentation, to make the '1's line up, so it produces a nice diamond shape. So, you might like to try something like this:

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

     

    #define MAX_LINE_LEN 80

     

    void nprintc(int n, char c);

    void nprintln(const char *s, size_t n);

    typedef enum { false = 0, true } bool;

    const char space = ' ';

    const char *L = "987654321";

    const char *R = "23456789";

     

    int main(int argc, char *argv[]) {

        char line[MAX_LINE_LEN];

        int n, i = 1, x = 1;

        char *p = strchr(L, '\0') - 1;

        bool inputOk;

     

        for (inputOk = false; inputOk == false; ) {

            printf("Enter an integer, 1 .. 9: ");

            fgets(line,MAX_LINE_LEN,stdin);

            inputOk = ((sscanf(line,"%d",&n) == 1) && (n > 0) && (n < 10));

        }

        for (i = 1; i > 0; i += x, p -= x) {

            /* indent */

            nprintc(n - i, space);

            /* display left */

            printf("%s", p);

            /* display right */

            nprintln(R, i - 1);

            /* if top half done, switch to bottom */

            if (i == n) x = -1;

        }

        return 0;

    }

    void nprintc(int n, char c) {

        while (n-- > 0) putchar(c);

    }

    void nprintln(const char *s, size_t n) {

        char *str = (char *)calloc(n + 1, sizeof(char));

        strncpy(str, s, n);

        printf("%s\n", str);

        free(str);

    }

  • This is an exercise in nested FOR loops. Look at each line - the first number will come from an outer loop, the remainder from inner loops - you just need to work out the details - it's actually a good learning experience.

  • You might get an idea if you see these basic programs:

    www.programming9.com/c-programming/119-c-program-for-floyd-s-triangle

    www.programming9.com/c-programming/100-c-program-to-print-triangle-of-values

  • #include<stdio.h> #include<conio.h> void main() int a,b,c; clrscr(); printf("1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"); getch();

  • Just use printf..

    printf("1\n212 \n32123\n4321234\n543212345\n543212345\n

    4321234\n32123\n212\n1")

    or use individual printf line by line!

  • here you are. . . .

    #include<stdio.h>

    #include<conio.h>

    void main() {

    int i,j,k,n=5;

    for(i=1;i<=n;i++) {

    for(j=i;j>=1;j--) {

    printf("%d",j);

    }

    for(k=j+2;k<=i;k++) {

    printf("%d",k);

    }

    printf("\n");

    }

    /* Printing the sequence in reverse */

    for(i=n-1;i>=1;i--) {

    for(j=i;j>=1;j--) {

    printf("%d",j);

    }

    for(k=j+2;k<=i;k++) {

    printf("%d",k);

    }

    printf("\n");

    }

    getch();

    }

    for more C programs <www.letuscalllessons.blogspot.in> this is my blog.

  • include<iostream.h>

    #include<conio.h>

    void main

    {

    cout<<"1

    212

    32123

    4321234

    543212345

    32123

    212

    1";

    clrscr();

    getch();

    }

    Now press ctrl+F9 and see the result..

Sign In or Register to comment.