-4

I am a little bit confused. I want to print all the prime starting from 2 to the entered number in java using while loop and don't want to use n/2 logic. I don't know what is wrong with my code. Please help me.

HERE IS THE CODE :-

import java.util.Scanner;
public class print_primes {
  public static void main(String[] args) {
  // TODO Auto-generated method stub
    Scanner scn = new Scanner(System.in);

    int n = scn.nextInt();
    int rem = 0;
    int i = 2;
    int flag = 0;
    int start = 3;
    System.out.println("2");
    while (start <= n) {
      i = 2;
      while (i <= start - 1) {
        rem = start % i;
        if (rem == 0) {
          flag = 1;
        }
        i = i + 1;
     }
     if (flag == 0) {
       System.out.println(start);
     }
     start++;
   }
 }
}
Ivan
  • 8,508
  • 2
  • 19
  • 30
  • 2
    Instead of n/2, you can reduce the iteration up to square root of n only! Search on the web about same. Further, there is `Sieve of Eratosthenes`... – Am_I_Helpful Aug 29 '18 at 18:00
  • Can you please tell me something to change in my code so that it will work. Because I don't know much about the things you mentioned. So please tell me how can I make it work in my style? – Suryansh Manav Aug 29 '18 at 18:01
  • 4
    Please first describe what is wrong. What is expected result and what you actually get – Ivan Aug 29 '18 at 18:03
  • Also you are using an `int` as a flag. A `boolean` might be better suited for this – GBlodgett Aug 29 '18 at 18:03
  • Expected result is that if you enter 12 as input then it should give you "2,3,5,7,11" as output but no matter what you enter it is just showing 2 and 3. – Suryansh Manav Aug 29 '18 at 18:04
  • 1
    What happens when you run your code? Do you get any errors? If so, what are they? If not, what is the output? How does it differ from what you want? I suggest that you read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ for some tips about how to debug your code. – Code-Apprentice Aug 29 '18 at 18:06
  • @Code-Apprentice There is no error in my code. as I mentioned above I want to get all the prime nos. upto the entered no. but I am only getting 2 and 3 as output. – Suryansh Manav Aug 29 '18 at 18:09
  • @SuryanshManav learning to debug is an essential skill as a programmer. I suggest you read the link I gave earlier for tips to help you start. – Code-Apprentice Aug 29 '18 at 18:53
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Your question can be answered very quickly and easily with your step-debugger. You should always try and solve your problems with a step debugger before coming to StackOverflow. –  Aug 30 '18 at 06:11

1 Answers1

0

A much more simpler approach :

public class Test {

   // method to check if each number is prime
   public static boolean isPrime(int x) {
      for (int i = 2; i < x; i++)
         if (x % i == 0) {
            return false;
         }
      return true;
   }

   public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);

      System.out.println("Enter a number");
      int num = scanner.nextInt();

      // print out 2 as we wont be checking even numbers.
      System.out.println(2);

      // iterate till user input
      for (int i = 3; i < num; i = i + 2) {
        if (isPrime(i)) {
          System.out.println(i);
        }
      }
   }
}
Nicholas K
  • 15,148
  • 7
  • 31
  • 57