2

nested for the loop checker

 3 years ago
source link: https://www.codesd.com/item/nested-for-the-loop-checker.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

nested for the loop checker

advertisements

here is my code Im kinda new to programming any way the goal of the program is to out put all the primes numbers between 0 and a user inputted value, each on their own line

import java.util.Scanner;
import java.math.*;

public class Sforprime{

  public static void main (String[] args){
    double box = 0;
    double boxxx = 0;
    int po = 0;
    int blub = 0;
    int no = 1;
    Scanner scan = new Scanner (System.in);
    System.out.println("input cieling number");
    box = scan.nextInt();
    boxxx = Math.sqrt(box);
    for (int batman = (int)box; batman >= 0; batman--){
      if (no == 1){
        no = 0;
      }
      else if (po == 0){
        System.out.println(blub);
        po = 0;
      }
      blub = batman;

      for (int mot = (int)boxxx; mot >= 2; mot--){
        if (po == 1 ){
        }
        else if (box%mot == 0){
          po++;
        }
      }
      if (po == 0){
        System.out.println(blub);
      }
    }
  }
}

What ends up happening is it doesn't do anything after you input the number. What am i doing wrong here?


Since you have initialised int mot = (int) boxxx box% mot will be 0 and po will always get incremented to 1 and hence never gets printed. Initialise mot=boxxx-1. But suggest you to use the above program as it is cleaner


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK