This C code block is a program that prints the multiplication table for a given starting number and stopping number.
#include <stdio.h>
int main()
{
int num1, num2;
printf("Enter Starting Point: \n");
scanf("%d", &num1);
while (num2 <= 0)
{
printf("Enter Stoping Point: \n");
scanf("%d", &num2);
}
printf("Multiplication rable for %d and %d \n", num1, num2);
for (int x = 1; x <= num2; x++)
{
printf("%d * %d = %d \n", num1, x, num1 * x);
}
return 0;
}
Result:
Enter Starting Point:
5
Enter Stoping Point:
15
Multiplication rable for 5 and 15
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
5 * 11 = 55
5 * 12 = 60
5 * 13 = 65
5 * 14 = 70
5 * 15 = 75
Process returned 0 (0x0) execution time : 5.813 s
Press any key to continue.
Let's go through it line by line:
#include <stdio.h>
This line includes the standard input/output header file.
int main()
{
This line defines the main function of the program.
int num1, num2;
This line declares two integer variables num1
and num2
which will be used to store the starting and stopping points of the multiplication table.
printf("Enter Starting Point: \n");
scanf("%d", &num1);
These lines prompt the user to enter the starting point of the multiplication table and store it in the variable num1
using the scanf()
function.
while (num2 <= 0)
{
printf("Enter Stoping Point: \n");
scanf("%d", &num2);
}
These lines prompt the user to enter the stopping point of the multiplication table and store it in the variable num2
using the scanf()
function. If the user enters a non-positive number, the program will continue prompting the user until a positive number is entered.
printf("Multiplication rable for %d and %d \n", num1, num2);
This line prints the starting and stopping points of the multiplication table.
for (int x = 1; x <= num2; x++)
{
printf("%d * %d = %d \n", num1, x, num1 * x);
}
These lines use a for
loop to iterate through each number in the multiplication table, from 1 to num2
. For each number, the program calculates the product of num1
and the current number and prints it in the format of num1 * x = product
.
return 0;
}
This line indicates the end of the main()
function and returns 0, indicating that the program has executed successfully.
No comments:
Post a Comment