Users Online

· Guests Online: 63

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C# Program to Find GCD of Two Numbers

C# Program to Find GCD of Two Numbers

 

 

 

This is a C# Program to perform gcd.

Problem Description

This C# Program Performs GCD.

Problem Solution

Greatest Common Divisor of two numbers is the largest positive numbers which can divide both numbers without any remainder

Program/Source Code

Here is source code of the C# Program that calculate GCD between two numbers. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Perform GCD
 */
using System;
public class Program
{
    static int GCD(int num1, int num2)
    {
        int Remainder;
 
        while (num2 != 0)
        {
            Remainder = num1 % num2;
            num1 = num2;
            num2 = Remainder;
        }
 
        return num1;
    }
 
    static int Main(string[] args)
    {
        int x, y;
        Console.Write("Enter the First Number : ");
        x = int.Parse(Console.ReadLine());
        Console.Write("Enter the Second Number : ");
        y = int.Parse(Console.ReadLine());
        Console.Write("\nThe Greatest Common Divisor of ");
        Console.WriteLine("{0} and {1} is {2}", x, y, GCD(x, y));
        Console.ReadLine();
        return 0;
    }
}
Program Explanation

This C# program is used to perform GCD. We are entering the first and second numbers using an x and y variables. Using while loop we are computing the GCD, is a Greatest Common Divisor of two numbers is the largest positive numbers which can divide both numbers without any remainder.

 

We are checking that ‘num2’ variable value is not equal to 0. If the condition is true, then we are executing the statement and computing the modulus of ‘num1’ variable value by ‘num2’ variable values and assigns to ‘remainder’ variable. Then we are assigning the ‘num2’ variable value to ‘num1’ variable value and ‘remainder’ variable value to ‘num2’ variable. Hence we are displaying the output of the program.

Runtime Test Cases
 
Enter the First Number : 12
Enter the Second Number : 24
The Greatest Common Divisor of 12 and 24 is : 12

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.85 seconds
10,844,561 unique visits