Table of Contents
C# | Multiply two numbers using function
In this article, we will discuss the concept of C# | Multiply two numbers using function
In this post, we are going to learn how to write a program to multiply two numbers using user-defined function in C# language
Code to multiplication of two numbers in C#
Multiply two numbers in C# using function
In this code, we are going to learn how to write a program to multiply two numbers using initialized variables in C# programming language
Program 1
//Write a program calculate multiplication of two numbers using function using System; public class Mul_Two_Num_Fun { //class definition public static int mul_Num(int fNum,int lNum) { //user defined function with argument int mul; //declare int variable for multiplication mul=fNum*lNum; //Calculate product of given numbers return mul; //return value to main method } public static void Main() { //main method int num1=25; //Declare and initialize variable int num2=60; Console.WriteLine("The multiplication of {0} and {1} is: {2}: ",num1,num2,mul_Num(num1,num2)); } } //Display result on the screen
When the above code is executed, it produces the following result
The multiplication of 25 and 60 is: 1500:
Multiply two numbers in C# using function – Entered by user
In this example, the user is asked to enter two integer numbers. Then the multiplication of the these two integers is calculated and displayed on the screen in C#
Program 2
// Write a program calculate multiplication of two numbers using function using System; public class Mul_Two_Num_Fun { //class definition public static int mul_Num(int num1, int num2) { //user-defined function definition int mulRes; //declare integer variable mulRes=num1*num2; //calculate multiplication return mulRes; //return value to main method } public static void Main() { //main method Console.Write("Enter number for n1: "); int n1=Convert.ToInt32(Console.ReadLine()); //get int input from the user for n1 Console.Write("Enter number for n2: "); int n2=Convert.ToInt32(Console.ReadLine()); //get int input from the user for n2 Console.WriteLine("The multiplication of {0} and {1} is: {2}: ",n1,n2,mul_Num(n1,n2)); } } //display result on the screen
When the above code is executed, it produces the following result
Enter number for n1: 12 Enter number for n2: 23 The multiplication of 12 and 23 is: 276:
In this above code, the user asked to enter two integers and the given integers are stored in variables n1 and n2 respectively.
Then these two numbers are multiplied using the multiplication(*) operator and the result is stored in the mulRes variable.
Finally the display statement is used to print the multiplication of numbers
Similar post
Java program to multiply two numbers
C++ program to multiply two numbers
C program to multiply two numbers
Python program to multiply two numbers
Java program to multiply two floating-point numbers
C++ program to multiply two floating-point numbers
C program to multiply two floating-point numbers
Python program to multiply two floating-point numbers
Java program to print multiplication table
C program to print multiplication table
C++ program to print multiplication table
Python program to print multiplication table