Narcissus: The Narcissus is a three-digit number, and the cube sum of its digit numbers is equal to the three-digit number itself, for example: 370=33+73+00; 371=33+73+13, 370, 371 is a Narcissus number
Note: To determine whether a three-digit number is a daffodil number, you must first obtain the single digits, ten digits and hundreds digits of the three-digit number.
Normal for loop:
public class Daffodil { public static void main(String[] args) { int count = 0; //Define the number of daffodils for(int i=100;i<=10000;i++){ //The range of daffodils int b = i/100; //Acquiring the hundred-digit int s = (i-100*b)/10; //Acquiring the ten-digit int g = (is*10-b*100); //Acquiring the single-digit if(i==g*g*g+s*s*s+b*b*b){ //Darks number judgment System.out.print(i+" "); //Output the number that meets the conditions count++; } } System.out.println(); //Brand System.out.println("The total number of daffodils is "+count+""); //Output the total number of daffodils}}While loop:
public class Daffodil { public static void main(String args[]){ int i=100; int count=0; //Define the number of daffodils while(i<10000){ int b = i/100; //Get the hundred-digit int s = (i-100*b)/10; //Get the ten-digit int g = (is*10-b*100); //Get the single-digit if(i==g*g*g+s*s*s+b*b*b){ //Darks number judgment System.out.print(i+" "); //Output the number that meets the conditions count++; } i+=1; } System.out.println(); System.out.println("The total number of daffodils is "+count+"); //The total number of daffodils output}}do-while loop:
public class Daffodil { public static void main(String args[]){ int i=100; int count=0; //Define the number of daffodils do{ i+=1; int b = i/100; //Get the hundred-digit int s = (i-100*b)/10; //Get the ten-digit int g = (is*10-b*100); //Get the single-digit if(i==g*g*g+s*s*s+b*b*b){ //Darks number judgment System.out.print(i+" "); //Output the number that meets the conditions count++; } }while(i<10000); System.out.println(); System.out.println("The total number of daffodils is "+count+"); //The total number of daffodils output}}