-3

Write a program that prompts the user to input a positive ineteger. It should then output a message indicating whether the number is prime number. Note: 2 is the only even number that is prime. An odd integer is prime if it is not divisible by an odd integer less than or equal to the square root of the number.

Update:

This is my program its not done really still figuring it out :(

import java.util.*;

public class prime {

    static Scanner kb = new Scanner(System.in);

    public static void main() {

        System.out.print("\fEnter positive integer:");

        int num = kb.nextInt();

        if (num < 1)

            System.out.print("Please enter number greater than 1"
                    + "Perform the program again");

        else if (num == 2)
            System.out.print("its a prime yey!");

        else if (num % 2 == 0)
            System.out.print("its not a prime ");

    }
}

Idk whats next still figuring it out T.T

mazhar islam
  • 5,561
  • 3
  • 20
  • 41
Joshee Joshy
  • 77
  • 2
  • 7
  • Need help, actually i already have program here e.e it just need a lil bit of push xD – Joshee Joshy Jul 23 '15 at 08:58
  • 4
    You first...... If you have done something then post it in the question. – mazhar islam Jul 23 '15 at 08:58
  • Please post your code and tell us what is wrong about it. – moffeltje Jul 23 '15 at 08:59
  • *i already have program* Than please add it to your question and tell us what your problem is. – Jens Jul 23 '15 at 08:59
  • 2
    Guys, wait before downvoting! No one learns with his first question. Just a comment that he needs to improve his question is enough! – inquizitive Jul 23 '15 at 09:00
  • I dont understand about the square root thingy :( – Joshee Joshy Jul 23 '15 at 09:09
  • Hello and welcome to StackOverflow. Please take some time to read the [help page](http://stackoverflow.com/help), especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". And more importantly, please read the [Stack Overflow question checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – galath Jul 23 '15 at 09:12
  • @JosheeJoshy then you might want to start by understanding the question you try to solve. What you don't understand wrt the question, you might be able to get/ask for an answer here http://math.stackexchange.com/ . Programming this problem is mainly an implementation of the formula,,, understand the formula, understand the coding – Danielson Jul 23 '15 at 09:12
  • We don't mind what ever blunders you have tried out. At least show us some work on square root thingy. – joey rohan Jul 23 '15 at 09:15

2 Answers2

0

There are few things you need to update in your code, one is you need a loop to go through the input number dividing it by many factors to check whether it is prime or not.

Here is a sample example for you. Note that, its not the best way to calculate whether any number is prime or not but it is good enough for better understanding, you can read here for primality test.

Must read Why do we check up to the square root of a prime number to determine if it is prime? first before reading the following code.

/**
 * Returns true if an integer is prime, false otherwise
 * @param n
 * @return
 */
public static boolean isPrime(int n) {
    //2 and 3 are primes
    if (n == 2 || n == 3)
        return true;
    //numbers divisible by 2 or 3 are not primes
    if (n % 2 == 0 || n % 3 == 0)
        return false;

    int i;
    // as we already checked dividing by 2 (so for all even including 4, 6),
    // 3 so lets start with 5   
    for (i = 5; i * i <= n; i += 2) {
        if (n % i == 0)
            return false;
    }
    return true;
}
Community
  • 1
  • 1
mazhar islam
  • 5,561
  • 3
  • 20
  • 41
0

Given Number is Prime or Not. Here i am adding the program to find the given number is Prime Number or Not.

Note: Prime Number is should divided by itself only.

import java.util.ArrayList;
import java.util.List;

public class CheckPrime {
static int num = 11;  //Given Number

public static void main(String[] args) {
    if (checkPrime()) {
        System.out.println("Given Number is Prime");
    } else {
        System.out.println("Given Number is Not Prime");
    }
}

private static boolean checkPrime() {
    List l = new ArrayList();
    for (int i = 1; i <= num; i++) {
        if (num % i == 0) {
            l.add(i);
        }
    }
    return  (l.size() > 2)?false:true; //if List size more than 2, it wont be Prime Number, 
    //because it should be divided by itself and One
}
}
Karthick
  • 1
  • 2
  • **From review queue:** May I request you to please add some more context around your answer. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. See also [Explaining entirely code-based answers](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers). – help-info.de Jun 08 '17 at 18:19