Saturday, January 21, 2023

Takes a number as input and print its multiplication table.

 Write a C# Sharp program that takes a number as input and print its multiplication table.

 

In mathematics, a multiplication table is a mathematical table used to define a multiplication operation for an algebraic system.

 Method:

Here are two methods of writing code for the table of given number. One is for the beginners and very simple. The other is for experts and use complex method I.e. for loop.

Method 1:

In this method two variables are declared as int. One is to take input from user and other to put result after calculation. After declaration of variables, the input is taken from user and then that input is used to process. Result is printed on the screen line by line and so is calculated.

Method 2:

In this method, just like method 1, two variables are declared as int. One is to take input from user and other to put result after calculation. After declaration of variables, the input is taken from user and then that input is used to process. A for loop is used to calculate and print the output.

Psuedocode:

Here is the psuedocode for table

define table(x)

    For (I=0; I <= 10; I++){

Int result = x*I;

Print (" : {0} x {1} = {2}", x,  I, result);)

Sample Solution:

Method 1:

C# Sharp Code:

using System; public class Exercise1

{ 

public static void Main() 

{

int x, result; 

Console.WriteLine("Enter a number:"); 

= Convert.ToInt32(Console.ReadLine()); 

result = x * 1; 

Console.WriteLine("The table is : {0} x {1} = {2}", x, 1,  result); 

result = x * 2; 

Console.WriteLine(" : {0} x {1} = {2}", x,  2, result); 

result = x * 3; 

Console.WriteLine(" : {0} x {1} = {2}", x,  3, result); 

result = x * 4; 

Console.WriteLine(" : {0} x {1} = {2}", x,  4, result); 

result = x * 5; 

Console.WriteLine(" : {0} x {1} = {2}", x,  5, result); 

result = x * 6; 

Console.WriteLine(" : {0} x {1} = {2}", x,  6, result); 

result = x * 7; 

Console.WriteLine(" : {0} x {1} = {2}", x,  7, result); 

result = x * 8; 

Console.WriteLine(" : {0} x {1} = {2}", x,  8, result); 

result = x * 9; 

Console.WriteLine(" : {0} x {1} = {2}", x,  9, result); 

result = x * 10; 

Console.WriteLine(" : {0} x {1} = {2}", x, 10result); 

} 

}

 Method 2:

using System; public class Exercise1

{ 

public static void Main() 

{

int x, result; 

Console.WriteLine("Enter a number:"); 

= Convert.ToInt32(Console.ReadLine()); 

For(int I=1; I<=10; I++;)

result = x * I;  

Console.WriteLine(" {0} x {1} = {2}", x, Iresult); 

} 

}


Sample Output: 

Enter a number:

5

The table is: 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

 Flowchart:

Here is the flowchart of the table.





No comments:

Post a Comment

Popular Posts