Question:

Hi, can anyone write a program in java for me?

by  |  earlier

0 LIKES UnLike

hi,

can anyone write a program in java for me?

my program is:-

write a program for finding the H.C.F (highest common factor) of two numbers.

 Tags:

   Report

3 ANSWERS


  1. Java Tutorials- http://javatutorial.info/


  2. This is a recursive call but it works:

    ////////// method alone //////////

            public static long hcf(long m, long n) {

                if (m == 0) return n;

                if (n == 0) return m;

                return hcf(n,m%n);

            }

    ///////////////////// In a class it would be /////////////////

            public class RecurseHCF {

                public static long hcf(long m, long n) {

                    if (m == 0) return n;

                    if (n == 0) return m;

                    return hcf(n,m%n);

                }

                public static void main(String[] argv) {

                    int n = 4567;

                    int m = 23412;

                    System.out.print("The hcf of " + m + " and "+ n);

                    System.out.println(" is " + hcf(m,n) + ".");

                }

            }

  3. package hcf;

    public class Main {

        public static void main(String[] args) {

          int a=Math.abs(-12);

          int b=Math.abs(128);

          while (a!=b) {

            if (a>b) {

              a-=b;

            } else {

              b-=a;

            }

          }

          System.out.println("The HCF is "+a);

        }

    }

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.