Sepiraroth
Sepiraroth

Reputation: 13

Checking to see if a numbe is prime using methods

The answer is probably staring me in the face but I have been looking at this so long the words are blurring together. The assignment is to have the user input 3 numbers, add the numbers together using a method, then to determine if the sum is a prime number using a different method.

package chpt6_Project;
import java.util.Scanner;


public class Chpt6_Project {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int num1;
        int num2;
        int num3;   

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the first number: ");
        num1 = scan.nextInt();

        System.out.println("Enter the second number: ");
        num2 = scan.nextInt();

        System.out.println("Enter the third number: ");
        num3 = scan.nextInt();

        Chpt6_Project.sum(num1, num2, num3);

        if(isPrime()) {
            System.out.println("The number is prime");
        } else {
            System.out.println("The number is not prime.");
        }
    }

    public static void sum(int num1, int num2, int num3) {
        int total = num1 + num2 + num3;
        System.out.println(total);
    }

    public static boolean isPrime(int total) {
        if((total > 2 && total % 2 == 0) || total == 1) {
            return false;
        }

        for (int i = 3; i <= (int)Math.sqrt(total); i += 2) {
            if (total % i == 0) {
                return false;
            }
        }
        return true;
    }
}

Upvotes: 0

Views: 362

Answers (1)

LLL
LLL

Reputation: 658

Edit the code as follow and you should do the trick.

The sum function now returns the sum calculated, this value is passed by main to the isPrime function which will return the right value

package chpt6_Project;
import java.util.Scanner;


public class Chpt6_Project {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int num1;
    int num2;
    int num3;


    Scanner scan = new Scanner(System.in);

    System.out.println("Enter the first number: ");
    num1 = scan.nextInt();

    System.out.println("Enter the second number: ");
    num2 = scan.nextInt();

    System.out.println("Enter the third number: ");
    num3 = scan.nextInt();



    if(isPrime(Chpt6_Project.sum(num1, num2, num3))) {
        System.out.println("The number is prime");
    } else {
        System.out.println("The number is not prime.");
    }

}

public static int sum(int num1, int num2, int num3) {
    int total = num1 + num2 + num3;
    System.out.println(total);
    return total;
}


public static boolean isPrime(int total) {

    if((total > 2 && total % 2 == 0) || total == 1) {
        return false;
    }

    for (int i = 3; i <= (int)Math.sqrt(total); i += 2) {

        if (total % i == 0) {
            return false;
        }
    }
    return true;
}

Morover i guess this is an homework but there are better way of doing this. For example, there is no need for a sum function.

Upvotes: 3

Related Questions