Write a C Program to Generate Multiplication Table.
In the world of programming, C is often considered one of the foundational
languages. It's known for its simplicity and powerful capabilities. If you're
learning C or just want to brush up on your programming skills, creating a
program to generate a multiplication table is a great exercise. In this
article, we'll walk you through the process of writing a C program to generate
a multiplication table without using structures and functions.
Understanding the Problem
Before diving into writing code, it's essential to understand the problem
thoroughly. In this case, we want to create a program that prints out a
multiplication table for a given number. The table should display the
multiplication results from 1 to 10. For example, if the user enters the
number 5, the program should generate the following output:
Multiplication Table for 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
#include<stdio.h>
#include<conio.h>
void main()
{
int number;// Ask the user for input
printf("Enter a number: ");
scanf("%d", &number);
// Print the table header
printf("Multiplication Table for %d:\n\n", number);
// Generate and print the multiplication table
for (int i = 1; i <= 10; i++)
{
printf("%d x %d = %d\n", number, i, number * i);
}
}
Code Explanation
- We include the standard input-output library (<stdio.h>) to use functions like printf and scanf.
- We declare an integer variable number to store the user's input.
- We prompt the user to enter a number and read it using scanf.
- We print the table header, indicating for which number we are generating the multiplication table.
- Using a for loop, we iterate from 1 to 10 to generate the multiplication table. Inside the loop:
- We print the multiplication expression and result using printf.
Output: