Computer >> 컴퓨터 >  >> 프로그램 작성 >> Java

숫자의 인수를 표시하는 Java 프로그램

<시간/>

이 기사에서는 숫자의 인수를 표시하는 방법을 이해할 것입니다. 인수는 다른 수나 식을 균등하게 나누는 수입니다.

요인은 다른 숫자를 얻기 위해 곱하는 숫자입니다. 예를 들어, 3과 5를 곱하면 15가 됩니다. 3과 5는 15의 인수라고 합니다. 또는 숫자의 인수는 나머지를 남기지 않고 해당 숫자를 나누는 숫자입니다. 예를 들어, 1, 2, 3, 4, 6, 12는 모두 균등하게 나누므로 12의 약수입니다.

숫자의 가장 큰 요소와 가장 작은 요소. 숫자의 가장 큰 인수는 숫자 자체이고 가장 작은 인수는 1입니다.

  • 1은 모든 숫자의 인수입니다.
  • 예를 들어 12의 가장 큰 인수와 최소 인수는 12와 1입니다.

아래는 동일한 데모입니다 -

입력

입력이 -

라고 가정합니다.
Input : 45

출력

The factors of 45 are: 1 3 5 9 15 45

알고리즘

Step 1 - START
Step 2 - Declare two integer values namely my_input and i
Step 3 - Read the required values from the user/ define the values
Step 4 - Using a for loop, iterate from 1 to my_input and check if modulus my_input value and ‘i’ value leaves a reminder. If no reminder is shown, then it’s a factor. Store the value.
Step 5 - Display the result
Step 6 - Stop

예시 1

여기에서 입력은 프롬프트에 따라 사용자가 입력하고 있습니다. 우리코딩 그라운드 도구에서 이 예제를 라이브로 사용해 볼 수 있습니다. 숫자의 인수를 표시하는 Java 프로그램 .

import java.util.Scanner;
public class Factors {
   public static void main(String[] args) {
      int my_input, i;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the number : ");
      my_input = my_scanner.nextInt();
      System.out.print("The factors of " + my_input + " are: ");
      for (i = 1; i <= my_input; ++i) {
         if (my_input % i == 0) {
            System.out.print(i + " ");
         }
      }
   }
}

출력

Required packages have been imported
A reader object has been defined
Enter the number : 45
The factors of 45 are: 1 3 5 9 15 45

예시 2

여기서 정수는 이전에 정의되었으며 그 값은 콘솔에 액세스되어 표시됩니다.

import java.util.Scanner;
public class Factors {
   public static void main(String[] args) {
      int my_input, i;
      my_input = 45;
      System.out.println("The number is defined as " +my_input);
      System.out.print("The factors of " + my_input + " are: ");
      for (i = 1; i <= my_input; ++i) {
         if (my_input % i == 0) {
            System.out.print(i + " ");
         }
      }
   }
}

출력

The number is defined as 45
The factors of 45 are: 1 3 5 9 15 45