This program aims to generate a multiplication table for a given number that the user enters.
#include <stdio.h>
int main()
{
int number, x;
printf("Enter an integer: ");
scanf("%d", &number);
printf("Multiplication table of %d: \n", number);
for (x = 1; x <= 10; x++)
{
printf("%d * %d = %d \n", number, x, number * x);
}
return 0;
}
Result:
Enter an integer: 1
Multiplication table of 1:
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9
1 * 10 = 10
Process returned 0 (0x0) execution time : 4.505 s
Press any key to continue.
At first, the program declares two variables, number
and x
, both of type int
. number
is the input integer that the user enters, and x
is used as a loop variable to iterate over the multiplication table.
Next, the program prompts the user to enter an integer by printing the message "Enter an integer: " using the printf
function. Then, the scanf
function is used to read the integer entered by the user and store it in the variable number
.
After that, the program prints a message using printf
to indicate the start of the multiplication table for the entered integer. It uses the value of number
to generate the message by using a format specifier %d
which is replaced by the value of number
in the output message.
The core of the program is a for
loop, which iterates over the multiplication table from 1 to 10. Within the for
loop, the printf
function is used to print the multiplication table of the entered number. It uses format specifiers %d
and %d * %d = %d \n
to generate the output message. %d
is replaced by the value of number
and x
, and %d * %d = %d \n
is replaced by the result of the multiplication. The \n
at the end of the format specifier indicates that a new line should be added after each iteration of the loop.
Finally, the program ends by returning 0 to the operating system to indicate that the program has completed successfully.
No comments:
Post a Comment