Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


k.jahangir kumar
javaprograms
1 Answer(s)      3 years and 2 months ago
Posted in : Java Beginners


write GCD program in java


Advertisement
View Answers

September 27, 2011 at 3:19 PM


import java.util.*;

public class GCDOFNumbers {
        public static int gcd(int a, int b) {
                if (b == 0)
                        return a;
                else
                        return gcd(b, a % b);
        }
        public static void main(String[] args){
                Scanner input = new Scanner(System.in);
                System.out.print("Enter Number 1: ");
                int num1 = input.nextInt();
                System.out.print("Enter Number 2: ");
                int num2 = input.nextInt();
                int gcdOfNum = GCDOFNumbers.gcd(num1, num2);
                System.out.println("HCF of two numbers " + num1 + "," + num2+ " is: " + gcdOfNum);
        }
}


Related Tutorials/Questions & Answers:
Advertisements
Advertisements
 

 

 

DMCA.com