-2

I have this code so far but it doesn't give me the right input. It needs to print prime nos from 2 till the number that the user inputs. What am I doing wrong?

import java.util.Scanner; 
public class Exhibit2 {
    public static void main(String args[]) { //forgot to add main
        System.out.println("This program takes the user input and prints the prime numbers until that number");
        System.out.println ("Enter Number:");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        for(int i=2;i<num;i++){
          for(int j=2; j<i; j++){
            if(num%j == 0){
              System.out.print(" ");
            }
            else{
              System.out.print(i);
            }
          }
        }
    }
}
azurefrog
  • 10,785
  • 7
  • 42
  • 56
  • What doesn't work? – Dan W Oct 13 '17 at 15:19
  • 4
    You are doing the math wrong. The first thing you need to do is write the code to [determine if a number is prime](https://stackoverflow.com/questions/9625663/calculating-and-printing-the-nth-prime-number) correctly. Then write a loop that prints the primes. – azurefrog Oct 13 '17 at 15:21
  • One problem is that you test `num` instead of `i` if it is divisible by `j`. – Henry Oct 13 '17 at 15:23
  • Read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Thomas Fritsch Oct 13 '17 at 15:26

2 Answers2

0

I have written a program that finds primes up to a certain number and start point. I wrote this a while ago and I'm sure there are better ways of doing it - but it allows you select a starting number and continue up to a point specified.

public static void main(String[] args) {

    System.out.println("This program takes the user input and prints the prime numbers until that number");
    System.out.println ("Enter Number:");
    Scanner sc = new Scanner(System.in);
    num = sc.nextInt();

    long startValue = 0;
    long primeNumbersToTest = num;
    for (long i = startValue; i < primeNumbersToTest; i++) {
        if (findPrime(i).equals("Prime")) {
            System.out.println(i + " is Prime");
        } else {
            System.out.println(i + " is not Prime");
        }
    }
}

public static String findPrime(long num) {

    boolean isPrime = true;
    for (long i = 2; i <= num / i; i++) {
        if ((num % i) == 0) {
            isPrime = false;
            break;
        }
    }
    if (isPrime)
        return "Prime";
    else
        return "Not Prime";
}

It outputs the first 10 like so:

0 is Prime
1 is Prime
2 is Prime
3 is Prime
4 is not Prime
5 is Prime
6 is not Prime
7 is Prime
8 is not Prime
9 is not Prime
10 is not Prime
farmlandbee
  • 345
  • 1
  • 3
  • 14
0
import java.util.Scanner; 

public class Main {
    public static void main(String args[]) { 
        System.out.println("This program takes the user input and prints the prime numbers until that number");
        System.out.println ("Enter Number:");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();  
        int i = 0;
        while(i<=num) {
            if(isPrime(i)) {
                System.out.println("Prime Number: "+i);
            }
            i++;
        }    
    }

    public static boolean isPrime(int n) {
        if(n > 2 && (n & 1) == 0)
           return false;
        for(int i = 3; i * i <= n; i += 2)
            if (n % i == 0) 
                return false;
        return true;
    }
}

This will print out your prime numbers from 0 to entered int with a decent speed.

JT 1114
  • 73
  • 1
  • 11