C program to print table of a given number

Write a C program to enter any number from user and print table of the given number using for loop. How to print table of any given number in C programming. Table printing program in C.

Example:
Input number: 5
Output table of 5:
5 * 1 = 5
5 * 2 = 10
...
...
...
5 * 10 = 50

Required knowledge

Basic C programming, For loop

Program to print table

/**
 * C program to print table of any number
 */

#include <stdio.h>

int main()
{
    int i, num;

    /* Reads number to print table */
    printf("Enter number to print the table: ");
    scanf("%d", &num);

    for(i=1; i<=10; i++)
    {
        printf("%d * %d = %d\n", num, i, (num*i));
    }

    return 0;
} 


Output
X
_
Enter number to print the table of: 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

6 comments:

  1. When I did this I didn't keep the i, j before i*j in the printf command in the for loop. This changed the output value. Can you tell me why ? Do they have to be specified.

    Also I wrote the code like this afterwards is this ok ?

    #include
    main()
    {
    int a, i, j ;
    printf("Enter the number that is multiplied a : ");
    scanf("%d", &a);

    printf("Enter the value of j: ");
    scanf("%d", &j);

    for( i=1 ; i<=j; i++)
    {
    printf("%d * %d = %d\n", a, i, a*i);
    }

    system("pause");
    }

    ReplyDelete
    Replies
    1. Gaurav I didn't understand the first problem what you asked. If you can clear your first problem a bit more then I could suggest something.

      Talking about the second one. The code you wrote is perfect but the only point I can suggest is that there isn't any use of taking input in ' j ' as numbers are generally multiplied upto 10 for printing any table.
      For Example: If we print the table of 5 it should be
      5 * 1 = 5
      5 * 2 = 10
      5 * 3 = 15
      5 * 4 = 20
      ..
      ..
      ..
      5 * 10 = 50

      Hence the ' j ' remains always fixed to 10 therefore there isnt any need to take input in ' j '.

      Else your code will work good.

      Delete
    2. when i initially wrote the code i didn't input a,i in the code and just wrote the code as follows in the for command and this changed the output. I was wondering why this happened.
      for( i=1 ; i<=j; i++)
      {
      printf("%d * %d = %d\n", a*i);
      }

      Delete
    3. if your first code contains only

      for( i=1 ; i<=j; i++)
      {
      printf("%d * %d = %d\n", a*i);
      }

      without taking any input in ' a ', then C will automatically store garbage value in ' a ' which can be anything representing an integer. As you haven't initialized the value of ' a '. It would produce a garbage output each time you try to run the code as C works on the concept of GIGO (Garbage In Garbage Out).

      Also its always better to initialize variable before their use.

      Delete
    4. Thanks so that's why it showed useless values.

      Delete