solution:
import javax.swing.*;
public class Divisor {
public static void main(String args[]){
int n1=12, n2=72, n3=18, n4=24, answer;
answer=gcd(gcd(n1, n2), gcd(n3, n4));
JOptionPane.showMessageDialog(null, "The GCD is " + answer);
System.exit(0);}
public static int gcd( int x, int y ){
int greatest = 1;
int smaller = ( x <>
for ( int z = 2; z <= smaller; z++ )if ( ( x % z == 0 ) && ( y % z == 0 ) )
greatest = z;
return greatest;}}
********************************
Write an application to calculate a power of an integer without using any math library methods. You should define a method integerPower( base, exponent ) that returns the value of base exponent
solution:
import javax.swing.*;
public class Power {
public static void main(String args[]){
int a=0, b=0, product;
String i;
i = JOptionPane.showInputDialog("Enter the base: ");
a = Integer.parseInt(i);
i = JOptionPane.showInputDialog("Enter the exponent: ");
b = Integer.parseInt(i);
product = integerPower(a, b);
JOptionPane.showMessageDialog(null, "Product is " + product);
System.exit(0);}
public static int integerPower( int base, int exponent ){
int product = 1;
for ( int x = 1; x <= exponent; x++ )
product *= base;
return product;}}
沒有留言:
張貼留言