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

입력 번호가 네온 번호인지 확인하는 Java 프로그램

<시간/>

이번 글에서는 입력된 숫자가 네온 숫자인지 확인하는 방법을 알아보겠습니다. 네온 숫자는 그 숫자의 제곱의 자릿수의 합이 숫자와 같은 숫자입니다.

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

입력

입력이 -

라고 가정합니다.
9

출력

92 =81, 즉 8 + 1 =9

이기 때문에 원하는 출력은 다음과 같습니다.
The given input is a neon number

알고리즘

Step1- Start
Step 2- Declare an integers my_input
Step 3- Prompt the user to enter an integer value/ Hardcode the integer
Step 4- Read the values
Step 5- Compute the square of the input and store it in a variable input_square
Step 6- Compute the sum of the of the digits of input_square and compare the result with my_input
Step 6- If the input matches, then the input number is a neon number
Step 7- If not, the input is not a neon number.
Step 8- Display the result
Step 9- Stop

예시 1

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

import java.util.Scanner;
public class NeonNumbers{
   public static void main(String[] args){
      int my_input, input_square, sum=0;
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("Required packages have been imported");
      System.out.println("A scanner object has been defined ");
      System.out.println("Enter the number: ");
      my_input=my_scanner.nextInt();
      input_square=my_input*my_input;
      while(input_square>0){
         sum=sum+input_square%10;
         input_square=input_square/10;
      }
      if(sum!=my_input)
         System.out.println("The given input is not a Neon number.");
      else
         System.out.println("The given input is Neon number.");
   }
}

출력

Required packages have been imported
A scanner object has been defined
Enter the number:
9
The given input is Neon number.

예시 2

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

public class NeonNumbers{
   public static void main(String[] args){
      int my_input, input_square, sum=0;
      my_input= 9;
      System.out.printf("The number is %d ", my_input);
      input_square=my_input*my_input;
      while(input_square<0){
         sum=sum+input_square%10;
         input_square=input_square/10;
      }
      if(sum!=my_input)
         System.out.println("\nThe given input is not a Neon number.");
      else
         System.out.println("\nThe given input is Neon number.");
   }
}

출력

The number is 9
The given input is Neon number.