answersLogoWhite

0


Best Answer

There are two basic steps to finding the greatest number. 1) convert the input from Strings to ints (or doubles or whatever number type you are expecting), 2) find the greatest number.

/*

For this example we're going to answer the question exactly,

with no regard to expandability (for dealing with more numbers).

*/

public static void main(String[] args) {

// step 1 - convert input from Strings to ints

int a = 0, b = 0, c = 0;

// we need to wrap a try-catch block around this to

// deal with any input that cannot be converted from

// a String to an int

try {

a = Integer.parseInt(args[0]);

b = Integer.parseInt(args[1]);

c = Integer.parseInt(args[2]);

}catch(NumberFormatException ex) {

// do whatever you need to do here to manage the

// exception

}

// step 2 - find the greatest number

int max = a;

if(b > max) {

max = b;

}

if(c > max) {

max = c;

}

// at this point the value in max is the greatest of the three

// numbers passed in through the command line

}

OR ELSE YOU SHOULD TRY THE ANOTHER STYLE FOR COMMAND LINE ARGUMENT , AS IT IS GIVEN BELOW.

class Max_command

{

public static void main(String args[])

{

for(String name : args)

{

System.out.println(name);

}

int i,max=0;

for(i=0;i<=args.length;i++)

{

int a=Integer.parseInt(args[i]);

if(a>max)

{

max=a;

}

if(i==args.length-1)

{

System.out.println("Max is --> "+max);

}

}

}

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Since this sounds like a homework assignment, I'm not going give you source code, but I can give you some pseudo code to help you out.

[code]

//Find the smallest of the four numbers

min = a

IF b < min THEN min = b

IF c < min THEN min = c

IF d < min THEN min = d

FOR i = 1 TO min

//A number is only a common divisor if division does not produce a remainder

//when using it to divide any of the four numbers.

//The modulo operator, %, can be used to find the remainder.

IF a % i 0 THEN gcd = i

END FOR

[/code]

When writing the above code in Java, don't forget that you want to INCLUDE "min" in your for loop's iterations.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

The following would be easy to program, but slow to run: try all the factors, starting at the smaller of the two numbers, and going down to one, until you find a common factor.

A little more tricky, but much faster for larger numbers, is the following property, which I will illustrate with an example. The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 is calculated as 14 - 10 (or, faster, to avoid repeated subtraction, 14 % 10 - the remainder of the division). Repeat until you have a remainder of zero: (14, 10), (10, 4), (4, 2), (2, 0). The last non-zero number, in this case 2, is the greatest common factor.

The following would be easy to program, but slow to run: try all the factors, starting at the smaller of the two numbers, and going down to one, until you find a common factor.

A little more tricky, but much faster for larger numbers, is the following property, which I will illustrate with an example. The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 is calculated as 14 - 10 (or, faster, to avoid repeated subtraction, 14 % 10 - the remainder of the division). Repeat until you have a remainder of zero: (14, 10), (10, 4), (4, 2), (2, 0). The last non-zero number, in this case 2, is the greatest common factor.

The following would be easy to program, but slow to run: try all the factors, starting at the smaller of the two numbers, and going down to one, until you find a common factor.

A little more tricky, but much faster for larger numbers, is the following property, which I will illustrate with an example. The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 is calculated as 14 - 10 (or, faster, to avoid repeated subtraction, 14 % 10 - the remainder of the division). Repeat until you have a remainder of zero: (14, 10), (10, 4), (4, 2), (2, 0). The last non-zero number, in this case 2, is the greatest common factor.

The following would be easy to program, but slow to run: try all the factors, starting at the smaller of the two numbers, and going down to one, until you find a common factor.

A little more tricky, but much faster for larger numbers, is the following property, which I will illustrate with an example. The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 is calculated as 14 - 10 (or, faster, to avoid repeated subtraction, 14 % 10 - the remainder of the division). Repeat until you have a remainder of zero: (14, 10), (10, 4), (4, 2), (2, 0). The last non-zero number, in this case 2, is the greatest common factor.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

You can just use the GCD of any two of your numbers and find the GCD of it with your third number. Same for LCM.

public class Lcmgcd {

private static int gcd(int a, int b) { return (b == 0) ? a: gcd(b, a%b); }

private static int lcm(int a, int b) { return a * b / gcd(a, b); }

public static void main(String[] args) {

int[] n = {12, 16, 28};

System.out.println("GCD: " + gcd(n[2], gcd(n[0], n[1])) + "\tLCM: " + lcm(n[1],lcm(n[2],n[0])));

}

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

The following would be easy to program, but slow to run: try all the factors, starting at the smaller of the two numbers, and going down to one, until you find a common factor.

A little more tricky, but much faster for larger numbers, is the following property, which I will illustrate with an example. The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 is calculated as 14 - 10 (or, faster, to avoid repeated subtraction, 14 % 10 - the remainder of the division). Repeat until you have a remainder of zero: (14, 10), (10, 4), (4, 2), (2, 0). The last non-zero number, in this case 2, is the greatest common factor.

This answer is:
User Avatar

User Avatar

Wiki User

16y ago

public class GCD { public static int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); } public static int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); } }

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program in java to find gcf of two numbers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a java program to find sum of even and odd numbers in given array?

for(int i = 1; i &lt; 100; i+=2) { System.out.println(i); }


How do you write a java program for finding multiligual languages?

You can use Java's built-in functions to write a code that will find multilingual languages.


How do you write a java program to find the ad joint of a matrix?

bgfygfrhjyuyhh


Can we add website link when we write java program?

yes ,i can add the website link in java program when we write.


How do you write a java program to find the square root of a number?

You can use the Math.sqrt() method.


Jntu 2-2 oops through java answers?

write a java program to find factorial using recursive and non recursive


Write a java script program to print first ten odd natural numbers in C?

Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.


Java program to find sum on even numbers from 12-45?

sum = 0; for (int i = 12; i


Write a c program Fibonacci series using for loop in java?

Exactly what do you mean by 'C program in Java'


What is javadoc?

write a java program to display "Welcome Java" and list its execution steps.


How write new line program in java?

\n


How do you write a program in java to read ten numbers and print sum of ten integers?

Add the numbers into one variable as you read them in. But if you prefer, you can read the numbers into an array and then use a loop to add the numbers together.