要使一个数字成为一个令人着迷的数字,它应该是三位数或三位数以上。
向您展示一些实例实例1输入数字为327
让我们用迷人数字的逻辑来检查一下 -
327 * 2 = 654327 * 3 = 981
连接“654”+“981”+“327”= 654981327
因此,327 是一个令人着迷的数字。
实例2输入数字为192
让我们用迷人数字的逻辑来检查一下 -
192 * 2 = 384192 * 3 = 576
连接“384”+“576”+“192”= 384576192
因此,327 是一个令人着迷的数字。
实例3输入数字为241
让我们用迷人数字的逻辑来检查一下 -
241 * 2 = 482241 * 3 = 723
连接“482”+“723”+“241”= 482723241
因此,241并不是一个令人着迷的数字
其他一些令人着迷的数字示例包括 192、1920、2019、327 等。
算法第 1 步 - 通过初始化或用户输入获取整数。
第 2 步 - 检查这是否是三位数。
第 3 步 - 将数字乘以 2 和 3。
第 4 步 - 将两个乘积与数字本身连接起来。
第 5 步 - 现在查找数字中是否存在从 1 到 9 的所有数字。如果确实如此,那么这将是一个令人着迷的数字
多种方法我们通过不同的方式提供了解决方案。
通过使用静态输入值
通过使用用户定义的方法
让我们一一看看该程序及其输出。
方法 1:使用静态输入值在这种方法中,在程序中初始化一个整数值,然后通过使用算法我们可以检查一个数字是否是一个迷人的数字。
示例public class main { public static void main(string args[]){ // initialized an integer value int num = 327; system.out.println(given number: +num); // store the product of the numbers in the variables int prod1 = num*2; int prod2 = num*3; // concatenate the numbers string concatnum = prod1++prod2+num; // boolean value to store the result boolean flag = true; // loops from 1 to 9 for(char c = '1'; c <= '9'; c++) { // count holds the number of times a digit occurs int count = 0; //loop counts the frequency of each digit for(int i = 0; i < concatnum.length(); i++) { char ch = concatnum.charat(i); //compares the character of concatnum with i if(ch == c) //increments the count by 1 if the specified condition returns true count++; } // checks if all the digits are present in the number if(count > 1 || count == 0) { flag = false; break; } } // prints the result if(flag) system.out.println(fascinating number); else system.out.println(not a fascinating number); }}
输出given number: 327fascinating number
方法2:使用用户定义的方法在这种方法中,在程序中初始化一个整数值,并将该数字作为参数传递到用户定义的方法中,然后在该方法中使用算法我们可以检查一个数字是否是一个迷人的数字。示例public class mai { static boolean fascinatingnum(int num){ // store the product of the numbers in the variables int prod1 = num*2; int prod2 = num*3; // concatenate the numbers string concatnum = prod1++prod2+num; // boolean value to store the result boolean flag = true; // loops from 1 to 9 for(char c = '1'; c <= '9'; c++) { // count holds the number of times a digit occurs int count = 0; //loop counts the frequency of each digit for(int i = 0; i < concatnum.length(); i++){ char ch = concatnum.charat(i); //compares the character of concatnum with i if(ch == c) //increments the count by 1 if the specified condition returns true count++; } // checks if all the digits are present in the number if(count > 1 || count == 0) { flag = false; break; } } return flag; } public static void main(string args[]){ // initialized an integer value int num = 327; system.out.println(given number: +num); // calls the user defined method and stores the result in res variable boolean res = fascinatingnum(num); // prints the result if(res) system.out.println(fascinating number); else system.out.println(not a fascinating number); }}
输出given number: 327fascinating number
在本文中,我们探讨了如何使用不同的方法在 java 中检查一个数字是否是一个有趣的数字。
以上就是如何在java中检查一个数字是否是迷人的数字?的详细内容。
