C Interview Programs to print number patterns

by Madhulika Reddy on April 8, 2009


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

Over 1600 professionals follow Interview Mantra.


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


  • Raj

    3 4 5 * * * 3 4 5
    6 7 8 * * * 6 7 8
    9 0 1 * * * 9 0 1
    * * * 3 4 5 * * *
    * * * 6 7 8 * * *
    * * * 9 0 1 * * *
    3 4 5 * * * 3 4 5
    6 7 8 * * * 6 7 8
    9 0 1 * * * 9 0 1Code For this one please

  • Sanne_dhankuta

    I need souece code to generate following pattern

    1
    21
    3  1
    4     154321

  • Sanne_dhankuta

    I need souece code to generate following pattern 
    1213  14      154321

  • Jithu Khandelwal

    hello mam this is jithu khandelwal…… i’ve an program for the above pattern so pleasesend me ur e-mail id i’ll send the program

  • Prashant Gupta

    #include
    #include
    void main()
    {
    int i,j,k;
    clrscr();
    for(i=1;i<=4;i++)
    {
       for(k=1;k<=(4-i);k++)
       {
           printf(" ");
       }
           for(j=1;j=1;i–)
    {
         for(k=1;k<=(4-i);k++)
         {
              printf(" ");
         }
              for(j=1;j<=i;j++)
              {
                   printf("%d",i);
              }
    printf("n");
    }
    getch();
    }

  • anil

                                   * * * * * * * * * *
                                    * * * * * * * * *
                                     * * * * * * * *
                                      * * * * * * *
                                       * * * * * *
                                        * * * * * 
                                         * * * *
                                          * * *
                                           * *
                                            *

  • Raavi Swami

    plz tell me code plzzzzzzzzz

  • munna

    hi jithu please send above program source code to my mail id is muralikrishna720@gmail.com

  • Aishu_lovelygal92

    i need this pattern
    1 2 3 4
    2 3 4 1
    3 4 1 2
    4 1 2 3

  • Aishu_lovelygal92

    pls mail me the ans to this pattern right now
    1 2 3 4
    2 3 4 1
    3 4 1 2
    4 1 2 3

  • Anonymous

    i want the code of the g patterns:
    if n=4:
    1              1
    12         21
    123   321 
    1234321   

  • B Bravara charyulu

    plz send me the code … bbcmca@gmail.com

  • soumya nandy

    WAp in c++ to print the following pattern…
     & & & & & & &
      _& & & & &
      _ _& & &       
      _ _ _&

  • rupali singh

    5
    54
    543
    5432
    54321

  • rupali singh

    A
    AB
    ABC
    ABCD
    ABCDE

  • rupali singh

    E
    DD
    CCC
    BBBB
    AAAAA

  • rupali singh

    A
    AB
    ABC
    ABCD
    ABCDE

  • rupali singh

    E
    ED
    EDC
    EDCB
    EDCBA

  • rupali singh

    ****************
    *                                 *
    *                                 *
    *                                 *
    *                                 *
    *                                 *
    *                                 *
    ****************

  • rupali singh

    ***********
    ****       ****
    ***            ***
    **                 **
    *                      *

  • rupali singh

    *                     *
    **                **  
    ***           ***
    ****       ****        
    ***********

  • Nekta1293

    hi jithu, pls send me d ans 2… my id is nekta1293@gmail.com

  • Elsie_delacerna

    write program that will display the following pattern,given the value of n and m
    example: if n=5 and m=6
    output:
    ********
    *              *
    *              *
    *              *
    *              *
    ********

  • Stephannie Joy

    sir, please help me for this program using the c++
     ****   ***     **       *

  • Elsie_delacerna

    how can i print this pattern

    4321321211

  • Seth’s World

    1
    01
    101
    0101
    10101

     Prnint that Pattern and tell me  how it’s possible…

  • Uday Kumar Goshika

    #include
    #include
    main()
    {
          int i,j,n,x;
          printf(“Enter n value:”);
          scanf(“%d”,&n);
          x=n;
           for(i=0;i<=2*x;i++)
                 {
                         for(j=0;j<=2*x;j++)
                         {
                                 if(i==j||(i+j)==2*x)
                                 {
                                     printf("%d",n);
                                     //printf("*");
                                  }
                                  else
                                  { 
                                      printf(" ");
                                  }
                          }
                          printf("n");
                          if(i<x)
                          n–;
                          else
                            n++;
                 }
           getch();
          }

  • Uday Kumar Goshika

    Enter n value:5
    5         5
     4       4
      3     3
       2   2
        1 1
         0
        1 1
       2   2
      3     3
     4       4
    5         5

  • Uday Kumar Goshika

    #include
    main()
    {
          int i,j,n,x;
          printf(“Enter n value:”);
          scanf(“%d”,&n);
          x=n;
           for(i=0;i<=2*x;i++)
                 {
                         for(j=0;j<=2*x;j++)
                         {
                                 if(i==j||(i+j)==2*x)
                                 {
                                     printf("%d",n);
                                     //printf("*");
                                  }
                                  else
                                  { 
                                      printf(" ");
                                  }
                          }
                          printf("n");
                          if(i<x)
                          n–;
                          else
                            n++;
                 }
           getch();
          }

  • Uday Kumar Goshika

    click on the image you will get the exact code for your question.

  • Uday Kumar Goshika

    click on the below image you will get the exact code for you question. For more details send mail to udaykumargoshika@gmail.com

  • Uday Kumar Goshika

    Here is the output. click on that image

  • IamScholer

    http://www.scribd.com/doc/52111510/Programs-in-c
    See weather ur programme is there on this site or not

  • Sandeeptayal

    program-to-print-table-of-2-in-c-using-vi

    The following post was helpful for those looking to print table of 2 in unix

    http://myblogs.netne.net/program-to-print-table-of-2-in-c-using-vi/

  • Piyushspimple

    WAP FOR printing the patter
                   1
                22
             333
          4444
       55555

  • Piyushspimple

    WAP fro reading the string from a file and check the number of occurence of the string “the” and print the count
    e.g in string weather the count is one for the string “the”

  • Tabia_rashid

    i want a code for this output 
    *bcdef
    a*cdef
    ab*def
    abc*ef
    abcd*f
    abcde*

  • Yamnaiftikhar

       need a source code plz give me if anyone know..

                                     1                                  212
                                    32123
                                  4321234
                               543212345
                                   4321234
                                      32123  
                                         212 
                                            1                         

  • Yamnaiftikhar

    what will be  the output of this source code????

  • Yamnaiftikhar

    i need coding of this program

  • Yamnaiftikhar

                                          1                                                                                
                                       212
                                    32123
                                  4321234
                               543212345
                                   4321234
                                      32123  
                                         212 
                                            1                         

  • Pathakayushi2612

    Plz show different type of pattern

Previous post:

Next post: