Basic Programming C# Examples

C# Program to find LCM of two numbers using HCF

The Program in C# Program to find LCM of two numbers using HCF is given below:

using System;

namespace LCM_HCF
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, num2, hcf, lcm;
Console.WriteLine("Hello Codeauri Family,enter first and second numbers below to find lcm : ");
            Console.Write("Well,enter the first number: ");
            num1 = Convert.ToInt32(Console.ReadLine());
            Console.Write("Well, enter the second number: ");
            num2 = Convert.ToInt32(Console.ReadLine());

            hcf = HCF(num1, num2);
            lcm = num1 * num2 / hcf;

            Console.WriteLine("Okay, The LCM of " + num1 + " and " + num2 + " is " + lcm);
            Console.ReadLine();
        }

        static int HCF(int n1, int n2)
        {
            int remainder;

            while (n2 != 0)
            {
                remainder = n1 % n2;
                n1 = n2;
                n2 = remainder;
            }

            return n1;
        }
    }
}

Output:

Hello Codeauri Family,enter first and second numbers below to find lcm :
Well,enter the first number: 5
Well, enter the second number: 10
Okay, The LCM of 5 and 10 is 10

Pro-Tips💡

Here are the step by step execution of above program:

  1. using System; – This line is a using directive, which tells the compiler to use the System namespace, which includes classes and methods used in the program.
  2. namespace LCM_HCF – The namespace declaration creates a namespace named LCM_HCF to group classes and other code elements in a logical and organized manner.
  3. class Program – This is a class declaration, creating a class named Program.
  4. static void Main(string[] args) – This is the main method of the program, the entry point for the program. When the program is executed, the code inside the Main method is executed first.
  5. int num1, num2, hcf, lcm; – This line declares four integer variables: num1, num2, hcf, and lcm.
  6. Console.WriteLine("Hello Codeauri Family,enter first and second numbers below to find lcm : "); – This line writes a message to the console asking the user to enter two numbers.
  7. Console.Write("Well,enter the first number: "); – This line writes a message to the console asking the user to enter the first number.
  8. num1 = Convert.ToInt32(Console.ReadLine()); – This line reads the first number entered by the user, converts it to an integer using the Convert.ToInt32 method, and assigns it to the num1 variable.
  9. Console.Write("Well, enter the second number: "); – This line writes a message to the console asking the user to enter the second number.
  10. num2 = Convert.ToInt32(Console.ReadLine()); – This line reads the second number entered by the user, converts it to an integer using the Convert.ToInt32 method, and assigns it to the num2 variable.
  11. hcf = HCF(num1, num2); – This line calls the HCF method and assigns the result to the hcf variable.
  12. lcm = num1 * num2 / hcf; – This line calculates the LCM by multiplying num1 and num2 and dividing the result by the hcf. The result is assigned to the lcm variable.
  13. Console.WriteLine("Okay, The LCM of " + num1 + " and " + num2 + " is " + lcm); – This line writes the result to the console.
  14. Console.ReadLine(); – This line waits for the user to press enter before ending the program.
  15. static int HCF(int n1, int n2) – This method calculates the HCF of two numbers.
  16. int remainder; – This line declares an integer variable remainder.
  17. while (n2 != 0) – This line starts a while loop that will run as long as n2 is not equal to 0.
  18. remainder = n1 % n2; – This line calculates the remainder when n1 is divided

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X