Problem Statement: Write a C# program to generate a multiplication table for a given number n. The table should display the products of n multiplied by numbers from 1 to 10.
Example:
Input: n = 5 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
Steps to Solve the Problem:
Below are the steps that you need to follow to print a multiplication table of any given number:
Step 1: Take an integer n as input, which represents the number for which the multiplication table needs to be generated.
Step 2: Display a header message, such as "Multiplication Table for n."
Step 3: Use a for loop to iterate through numbers from 1 to 10.
Step 4: In each iteration of the loop, calculate the product of n multiplied by the current loop variable.
Step 5: Display the multiplication equation, including n, the current loop variable, and the product.
C# Code Implementation for Multiplication Table.
// C-sharp code to print multiplication table using System; namespace MultiplicationTable { class Program { static void Main(string[] args) { Console.Write("Enter a number: "); int n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine($"Multiplication Table for {n}:"); for (int i = 1; i <= 10; i++) { int product = n * i; Console.WriteLine($"{n} x {i} = {product}"); } } } }
Enter a number: 8
Multiplication Table for 8:
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80
Explanation:
The above program uses a for loop to generate the multiplication table for the given number. The loop iterates from 1 to 10, calculating the product in each iteration.
Similar articles:
No comments:
Post a Comment