H. colleagues, tell me, please, how to print also the message that the number is composite. Thank!

int n=9; for (int i=2; i<n; i++) { if ((n%i)==0) { System.out.println (n+"-is not prime number "); } } 
  • Obviously, System.out.println("Число является целым"); . Do not thank. - VladD 4:44 pm
  • first time here, sorry. Not all the text for some reason in the question. It is meant to display the answer in two lines - if the whole is or not - avs6337
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

3 answers 3

decided so

 public class cycles3 { public static void main (String []args) { int n=9; int m=0; for (int i=2; i<n; i++) { if (n%i==0) { m++; } } if (m==0) { System.out.println (n+"-is the prime number "); } else { System.out.println (n+"-is not prime number "); } } } 
     double toTest = 23.23d; int flooredValue = (int) toTest; double resultDouble = (double)toTest - flooredValue; if(resultDouble == 0) { System.out.println("целое"); } else { System.out.println("НЕ целое"); } 

    To check the simplicity of the number, your code can be used as follows:

    Create a boolean variable with a value of true , set false if the condition is met. After the cycle, check the total value.

    • Thank you, is it possible within the code in question? - avs6337
    • @ avs6337, I don’t understand at all how the code you give should solve the problem you set. Wherefore, I can not answer you how you can modify it. Do you know there is divided without rest the given number into all numbers from 2 to n. - Yuriy SPb
    • one
      @ YurySPb I think the author confused the whole and simple) - pavel
    • the idea was that if a given number is divisible by at least one number, it means that it is not simple. - avs6337 5:06
    • yes - sorry - confused the whole with a simple one - avs6337
     import java.util.Scanner; class TrueFals{ public static boolean isDivisible(double n, double m){ double x = n / m; return ((x % 1) == 0); } public static void main(String[] args){ Scanner in = new Scanner(System.in); double n = in.nextInt(); double m = in.nextInt(); boolean tf = isDivisible(n, m); System.out.println(tf); } }