무료 소프트웨어 개발 과정 시작
웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등수학적으로, 우리는 숫자의 개별 숫자의 계승의 합이 원래 숫자와 어떻게 일치하는지 보여 주어야합니다. 그러한 숫자의 예 중 하나는 145입니다.
145= 1! +4! +5!이 기사에서는 또한 2 자리 또는 3 자리 숫자 일 수있는 다른 특수 번호가 작동하는 것을 볼 것입니다. Java에는이 프로그램을 실행할 수있는 많은 플랫폼이 있습니다. 이번 글에서는 BlueJ 플랫폼에서 프로그램이 어떻게 작동하는지 확인해보겠습니다. 우리에게 알려진 4개의 특별한 숫자가 있습니다. 1,2, 145 및 40585.
Java의 특수 번호 예
이번 프로그램에서는 숫자를 입력하고 그 숫자가 Special인지 아닌지 확인하는 프로그램입니다. 해당 숫자의 계승합이 원래 숫자와 같은지 간접적으로 확인합니다.
이제 번호는 145, 기타 번호는 40585를 확인해 보겠습니다. 이 코드 조각을 사용하여 숫자 145, 1, 2를 확인하겠습니다. 다른 프로그램에서는 숫자 40585를 확인하고 프로그램에 설치할 수 있는 루프에 대해 다른 방법론을 사용합니다. 이제 우리는 위에 표시된 프로그램의 다양한 출력을 살펴보겠습니다. 출력은 숫자 1, 2, 25 및 145에 대해 생성됩니다.
//Java program to check if a number // is a special number import java.util.*; import java.io.*; class Special { // function to calculate the factorial // of any number using while loop static int factorial(int n) { int fact = 1; while (n != 0) { fact = fact * n; n--; } return fact; } // function to Check if number is Special static boolean isSpecial(int n) { int sum = 0; int t = n; while (t != 0) { // calculate factorial of last digit // of temp and add it to sum sum += factorial(t % 10); // replace value of t by t/10 t = t / 10; } // Check if number is Special return (sum == n); } // Driver code public static void main(String[] args)throws IOException { BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the number to check if it is Special"); int n = Integer.parseInt(br.readLine()); if (isSpecial(n)) System.out.println("YES- The number is a special number"); else System.out.println("NO- The number is not a special number"); } }출력:
이 코딩 예제에서는 for 루프를 사용하여 프로그램을 수행하는 방법을 살펴보겠습니다. 이전 프로그램에서는 while 루프를 사용하여 숫자의 계승을 계산했습니다. for a 루프를 사용하여 계승을 계산하는 방법에 대한 아래 코딩 예제를 살펴보겠습니다.
이 145번과 40585번은 모두 Special Number가 되기 위한 전제 조건을 충족합니다. 따라서 숫자는 모두 출력에 표시된 특수 숫자임을 알 수 있습니다.
//Java program to check if a number // is a special number import java.util.*; import java.io.*; class Special { // function to calculate the factorial // of any number using for loop static int factorial(int n) { int fact = 1; for (int i=1;i<=n;i++) { fact = fact * i; ; } return fact; } // function to Check if number is Special static boolean isSpecial(int n) { int sum = 0; int t = n; while (t != 0) { // calculate factorial of last digit // of temp and add it to sum sum += factorial(t % 10); // replace value of t by t/10 t = t / 10; } // Check if number is Special return (sum == n); } // Driver code public static void main(String[] args)throws IOException { BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the number to check if it is Special"); int n = Integer.parseInt(br.readLine()); if (isSpecial(n)) System.out.println("YES- The number is a special number"); else System.out.println("NO- The number is not a special number"); } }출력:
결론
위 내용은 Java의 특수 번호의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!