Gcd of two numbers program

Gcd of two numbers program in c

#include<stdio.h>
#include<conio.h>
void main()
{
	int num1,num2;
	printf("Enter your first number \n");
	scanf("%d",&num1);
	printf("Enter your second number \n");
	scanf("%d",&num2);
	while(num1!=num2)
	{
		if(num1>num2)
			num1=num1-num2;
		else
			num2=num2-num1;
	}
	printf("GCD = %d",num1);
	getch();
}

Output

Enter your first number
63
Enter your second number
81
GCD = 9

Gcd of two numbers program in c++

#include<iostream>
#include<conio.h>
using namespace std;
class GCD
{
public:
	void FindGCD(int num1,int num2)
	{
		while(num1!=num2)
	    {
		if(num1>num2)
			num1=num1-num2;
		else
			num2=num2-num1;
	    }
		cout<<"GCD = "<<num1;
	}
};
void main()
{
	int num1,num2;
	cout<<"Enter the first number \n";
	cin>>num1;
	cout<<"Enter the second number \n";
	cin>>num2;
	GCD gcd;
	gcd.FindGCD(num1,num2);
	getch();
}

Output

Enter the first number
200
Enter the second number
10
GCD = 10

Gcd of two numbers program in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GCDProject
{
    class GCD
    {
        public void FindGCD(int num1, int num2)
        {
            while (num1 != num2)
            {
                if (num1 > num2)
                    num1 = num1 - num2;
                else
                    num2 = num2 - num1;
            }
            Console.WriteLine("GCD = " + num1);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the first number");
            int num1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the second number");
            int num2 = Convert.ToInt32(Console.ReadLine());
            GCD gcd = new GCD();
            gcd.FindGCD(num1, num2);
            Console.ReadLine();
        }
    }
}

Output

Enter the first number
64
Enter the second number
56
GCD = 8

Gcd of two numbers program in java

import java.util.*;
class GCD
{
	public void FindGCD(int num1, int num2)
    {
        while (num1 != num2)
        {
            if (num1 > num2)
                num1 = num1 - num2;
            else
                num2 = num2 - num1;
        }
        System.out.println("GCD = " + num1);
    }
}
class examples
{
public static void main(String[] args)
	{
	 GCD gcd=new GCD();
	 Scanner sc=new Scanner(System.in);
	 System.out.println("Enter the first number");
	 int num1=sc.nextInt();
	 System.out.println("Enter the second number");
	 int num2=sc.nextInt();
	 gcd.FindGCD(num1, num2);
	}
}

Output

Enter the first number
49
Enter the second number
35
GCD = 7 

Write a comment


Name Email Message