Short C Program to get multiplication table of entered number


         Short and simple C program to get multiplication table of entered number using for loop. In this program a single for loop is used, Usually tables are written from 1 to 10 therefore we are giving condition as "i<=10". loop will print till the condition become false. This program can be written in two different types, In 1st type we initialize 3 variables that are a, i, m. and we do calculation (Multiplication) and then print the answer. where as in 2nd type we don't take separate variable to store value of answer, we just initialize 2 variables a and i. and do calculation in "printf" itself. therefore we don't need 3rd variable.
Short programs are always better.!


Program:

1)

#include <stdio.h>
#include <conio.h>
//C Program to get multiplication table. SlashMyCode.com
void main()
{
    int a,m,i;
    printf("Multiplication Table for number: ");
    scanf("%d",&a);

    for(i=1;i<=10;i++)
      {
      m=a*i;
      printf("%d X %d=%d \n",a,i,m);
      }
//C Program to get multiplication table. SlashMyCode.com
    getch();

}


2)

#include <stdio.h>
#include <conio.h>
//C Program to get multiplication table. SlashMyCode.com
void main()
{
    int a,i;
    printf("Multiplication Table for number: ");
    scanf("%d",&a);

    for(i=1;i<=10;i++)
      {
      printf("%d X %d=%d \n",a,i,a*i);
      }
//C Program to get multiplication table. SlashMyCode.com
    getch();
}

Output:



Algorithm

  1. Start.
  2. Declare variable: a, i and m.
  3. accept value for a.
  4. assign i=1.
  5. if i<=10 then goto step 6.
  6. multiply a and i, and assign result to m.   m=a*i;
  7. print: "a x i=m".
  8. increment; i++ and goto step 5.
  9. Stop

No comments:

Post a Comment

Feel free to ask us any question regarding this post