C Interview Programs to print number patterns


c interview questions This post has few of basic number pattern programs in C. Question.56(multiplication table) tests the ability to convert an elementary algorithm into a program. Question.57, Question.58 are programs that test the ability of candidate to use nested while/for loops to print special number patterns. Question.59, Question.60 test ability of using simple while/for loops and basics of formatting using printf statement. See all C interview questions in this blog in one page.

Also Read:

Showing 5 out of 65 C Interview Questions.

C Language Interview Questions (56 to 60)
  1. Write a program to display the multiplication table of a given number.
  2. Write C program to print the following pattern:
             1
            2 2
           3 3 3
          4 4 4 4
         5 5 5 5 5
  3. Write C program to print the following pattern:
             1
           1 2 1
         1 2 3 2 1
       1 2 3 4 3 2 1
     1 2 3 4 5 4 3 2 1
  4. Write a C program to display the following format:
    ------
      a b
    ------
      1 5
      2 4
      3 3
      4 2
      5 1
     ------
  5. Write a C program to display the following format:
    --------
     no. sum
    --------
      1  1
      2  3
      3  6
      4  10
      5  15
     --------

  1. Write a program to display the multiplication table of a given number.
  2. Program: Multiplication table of a given number

    #include <stdio.h>
    int main() {
          int num, i = 1;
          printf("\n Enter any Number:");
          scanf("%d", &num);
          printf("Multiplication table of %d: \n", num);
          while (i <= 10) {
                printf("\n %d x %d = %d", num, i, num * i);
                i++;
          }
          return 0;
    }

    Download Code

    Output:

    Enter any Number:5
     5 x 1 = 5
     5 x 2 = 10
     5 x 3 = 15
     5 x 4 = 20
     5 x 5 = 25
     5 x 6 = 30
     5 x 7 = 35
     5 x 8 = 40
     5 x 9 = 45
     5 x 10 = 50

    Explanation: We need to multiply the given number (i.e. the number for which we want the multiplication table) with value of ‘i’ which increments from 1 to 10.

    Back to top

  3. Write C program to print the following pattern:
  4.          1
            2 2
           3 3 3
          4 4 4 4
         5 5 5 5 5

    Program:

    #include<stdio.h>
    int main() {
        int i, j, k, c = 5;
        for (i = 1; i <= 5; i++) {
                /* k is taken for spaces */
                for (k = 1; k <= c; k++) {
                      /* blank space */
                      printf(" ");
                }
                for (j = 1; j <= i; j++) {
                      /* %2d ensures that the number is printed in
    two spaces for alignment and the numbers are printed in the order. */
                      printf("%2d", i);
                }
                printf("\n");
                /*c is decremented by 1 */
                c--;
          }
          return 0;
    }

    Download Code

    Output:

             1
            2 2
           3 3 3
          4 4 4 4
         5 5 5 5 5

    Explanation: Here ‘i’ loop is used for printing the numbers in the respective rows and ‘k’ loop is used for providing spaces. ‘j’ loop prints the numbers. ‘c’ is decremented for numbers to be displayed in alternate columns.

    Back to top

  5. Write C program to print the following pattern:
  6.          1
           1 2 1
         1 2 3 2 1
       1 2 3 4 3 2 1
     1 2 3 4 5 4 3 2 1

    Program:

    #include<stdio.h>
    int main() {
          /* c taken for columns */
          int i, j, c = 9, m, k;
          for (i = 1; i <= 5; i++) {
                /* k is used for spaces */
                for (k = 1; k <= c; k++) {
                      printf(" ");
                }
                for (j = 1; j <= i; j++) {                   printf("%2d", j);             }             for (m = j - 2; m > 0; m--) {
                      /* %2d ensures that the number
                       * is printed in two spaces
                       * for alignment */
                      printf("%2d", m);
                }
                printf("\n");
                /* c is decremented by 2 */
                c = c - 2;
          }
          return 0;
    }

    Download Code

    Output:

             1
           1 2 1
         1 2 3 2 1
       1 2 3 4 3 2 1
     1 2 3 4 5 4 3 2 1

    Explanation: Here ‘i’ loop is used for printing numbers in rows and ‘k’ loop is used for providing spaces. ‘j’ loop is used for printing numbers in increasing order. ‘m’ loop is used for printing numbers in reverse order.

    Back to top

  7. Write a C program to display the following format:
  8. ------
      a b
     ------
      1 5
      2 4
      3 3
      4 2
      5 1
     ------

    Program:

    #include<stdio.h>
    int main() {
          int i = 1, j = 5;
          printf("----------\n");
          printf("a \t b \n");
          printf("----------\n");
          /* logic: while loop repeats
           * 5 times i.e. until
           * the condition i<=5 fails */
          while (i <= 5) {
                /* i and j value printed */
                printf("%d \t %d\n", i, j);
                /* i and j value incremented
                 by 1 for every iteration */
                i++;
                j--;
          }
          printf("----------");
          return 0;
    }

    Download Code

    Output:

    ------
      a b
     ------
      1 5
      2 4
      3 3
      4 2
      5 1
     ------

    Explanation: Here, ‘i’ is initialized to least value 1 and ‘j’ initialized to highest value 5. We keep incrementing the i’ value and decrementing the ‘j’ value until the condition fails. The value is displayed at each increment and at each decrement. Back to top

  9. Write a C program to display the following format:
  10. --------
     no. sum
     --------
      1  1
      2  3
      3  6
      4  10
      5  15
     --------

    Program:

    #include<stdio.h>
    int main() {
          int num = 1, sum = 0;
          printf("-----------\n");
          printf("num \t sum\n");
          printf("-----------\n");
          /* while loop repeats 5 times
           *  i.e. until the condition
           *  num <= 5 fails */
          while (num <= 5) {
                sum = sum + num;
                printf("%d \t %d\n", num, sum);
                /* num incremented by 1
                 * for every time
                 * the loop is executed */
                num++;
          }
          printf("-----------");
          return 0;
    }

    Download Code

    Output:

    --------
     no. sum
     --------
      1  1
      2  3
      3  6
      4  10
      5  15
     --------

    Explanation: In the above program we have taken two variables ‘num’ and ‘sum’. ‘num’ is used to check the condition and to display the numbers up to 5. ‘sum’ is used to add the numbers which are displayed using variable ‘num’. The ‘sum’ value is initialized to zero. sum is added to the numbers which are incremented by ‘i’ and displayed.

    Back to top


About the Author:  This post was written by Madhulika Reddy. You can connect with Madhulika on Orkut.



All comments of post - "C Interview Programs to print number patterns":

:Haha! I'am the first! Yeh~

Thank you!

Add a Comment / Trackback url

  1. #8  swamyNo Gravatar

    i really appreciate for ur information it is very help full for freshers its very good. can u tell please in c can we do graphical representation, if we can how tell me, and one more thing u can we same pattern for data structures and c++

    10/05/19 17:30
  2. #7  NyeNo Gravatar

    Thanks to those who hated me, they made me a stronger person.

    10/04/10 07:34
  3. #6  adminNo Gravatar

    @Amrutha,
    The question you have asked is similar to this question in my blog. Check it out. I think you will be able to write the logic for producing 1,2,3 easily and get desired output. Let me know if you have any problems.

    10/03/06 22:58
  4. #5  AmruthaNo Gravatar

    i want the c code for the following output.

    1________1
    1 2_____2 1
    1 2 3__3 2 1
    1 2 3 4 3 2 1

    i use “_”to represent spaces.

    10/02/19 11:41
  5. #4  PayalNo Gravatar

    i want the following output.
    1________1
    1 2_____2 1
    1 2 3__3 2 1
    1 2 3 4 3 2 1

    10/02/19 11:39
  6. #3  swatiNo Gravatar

    hey i need a source code for the follwing format
    _______1
    _____1___2
    ___1___2___3

    and also for..

    _____________1
    __________1_____1
    ________1_________1
    ______1_____________1

    where __ is spaces

    10/02/02 14:40
  7. #2  Sridhar

    Experimentally implemented C Syntax highlighter to program in question 56(Program to print multiplication table). Sorry about the inconsistency in the code formatting. Just testing this syntax highlighter (http://alexgorbatchev.com)

    If this goes fine, will apply syntax highlighter to all the code in this blog. Dear reader, what do you think?

    Sridhar,
    - Editor of Interview Mantra

    09/05/01 12:27
  8. #1  Sridhar

    Now applied syntax highlighter to all the code in this page

    -Sridhar

    09/05/01 11:28

Show all Show 5 so far Close all

Comment begin from here or jump up!