.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Greatest Common Divisor

Posted By:papsantony       Posted Date: August 13, 2010    Points: 10    Category: Visual Studio    URL: http://www.dotnetspark.com  

Greatest Common Divisor

This is an example of calculating the GCD in C#:
using System;

public class Program
static int GCD(int a, int b)
int Remainder;

while( b != 0 )
Remainder = a % b;
a = b;
b = Remainder;

return a;

static int Main(string[] args)
int x, y;

Console.WriteLine("This program allows calculating the GCD");
Console.Write("Value 1: ");
x = int.Parse(Console.ReadLine());
Console.Write("Value 2: ");
y = int.Parse(Console.ReadLine());

Console.Write("\nThe Greatest Common Divisor of ");
Console.WriteLine("{0} and {1} is {2}", x, y, GCD(x, y));

return 0;


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend