산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> java

소수를 확인하는 자바 프로그램

소수란 무엇입니까?

소수는 1 또는 자기 자신으로만 나누어 떨어지는 수입니다. 예를 들어, 11은 1 또는 그 자체로만 나눌 수 있습니다. 기타 소수 2, 3, 5, 7, 11, 13, 17….

참고: 0과 1은 소수가 아닙니다. 2는 유일한 짝수 소수입니다.

숫자가 소수인지 여부를 확인하는 Java 프로그램

프로그램 논리:

public class PrimenumberToCheckCheck {
 
 public static void main(String[] args) {
  int remainder;
  boolean isPrime=true;
  int numberToCheck=17; // Enter the numberToCheckber you want to check for prime
        
  //Loop to check whether the numberToCheckber is divisible any numberToCheckber other than 1 and itself
  for(int i=2;i<=numberToCheck/2;i++)
  {
   //numberToCheckber is divided by itself
            remainder=numberToCheck%i;
            System.out.println(numberToCheck+" Divided by "+ i + " gives a remainder "+remainder);
            
       //if remainder is 0 than numberToCheckber is not prime and break loop. Else continue the loop
     if(remainder==0)
     {
        isPrime=false;
        break;
     }
  }
  // Check value true or false, if isprime is true then numberToCheckber is prime otherwise not prime
  if(isPrime)
     System.out.println(numberToCheck + " is a Prime numberToCheckber");
  else
     System.out.println(numberToCheck + " is not a Prime numberToCheckber");
    }
  }

출력:

17 Divided by 2 gives a remainder 1
17 Divided by 3 gives a remainder 2
17 Divided by 4 gives a remainder 1
17 Divided by 5 gives a remainder 2
17 Divided by 6 gives a remainder 5
17 Divided by 7 gives a remainder 3
17 Divided by 8 gives a remainder 1
17 is a Prime Number

프로그램을 확인하여 1에서 100까지의 소수 찾기


java

  1. 자바 Hello World 프로그램
  2. 자바 연산자
  3. 자바 주석
  4. 자바 for-each 루프
  5. 자바 문자열
  6. 자바 인터페이스
  7. 자바 리소스 사용
  8. 자바 주석
  9. Java 문자열 contains() 메소드 | 예제로 하위 문자열 확인
  10. Java의 생성자 오버로딩:프로그램의 정의 및 예제