Miracle Cube are numbers whose cube root is equal to the sum of its own digits. Write a program which will print the number of distinct non miracle cubes in the series of n numbers(where n is a user specified integer such that 0 lessthan n and n less than equals 50)
Input Specification: -----------------------------------
First input n will be an integer number which the user is expected to provide(where 0<n<=50),
followed by n integer inputs such that 0<n(i)<10^5.
Output Specification: -----------------------------------
If value of n is invalid, print ´Invalid Input´. Else print count of distinct non miracle cubes in the series
Sample Input ------------------------------------
4 12 512 1 250
Sample Output ------------------------------------
2
-1
Answers
import java.util.Scanner;
public class MiracleCubes {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int total=input.nextInt();
int count=0;
for(int i=0;i<total;i++){
if(isMiracleCube(input.nextInt())){
count++;
}
}
System.out.println(count);
}
public static boolean isMiracleCube(Integer num){
double cbrt=Math.cbrt(num);
double total=0;
for(int i=0;i<num.toString().length();i++){
total=total+Double.parseDouble(Character.toString(num.toString().charAt(i)));
}
if(cbrt==total)
return true;
else
return false;
}
}
2
Your Code is not cleared my test cases because there is no n value and no command
can you please provide the code that is asked in question situation
Hi Akash,
Please try below code, If still if you face any problem, give me the test case, I´ll check and revert you back.
import java.util.Scanner;
public class MiracleCubes {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int total=input.nextInt();
int count=0;
if(total>0 && total<=50){
for(int i=0;i<total;i++){
if(isMiracleCube(input.nextInt())){
count++;
}
}
System.out.println(count);
}else{
System.out.println("Invalid Input");
}
}
public static boolean isMiracleCube(Integer num){
double cbrt=Math.cbrt(num);
double total=0;
for(int i=0;i<num.toString().length();i++){
total=total+Double.parseDouble(Character.toString(num.toString().charAt(i)));
}
if(cbrt==total)
return true;
else
return false;
}
}
import java.util.Scanner;
import java.lang.*;
class miracle
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int c=0,m=0;
if(n>0 && n<=50)
{
int []a=new int[n];
for(int i=0;i<n;i++)
{
int sum=0,sum1=0;
a[i]=in.nextInt();
sum1=a[i];
if(a[i]>0 && Math.pow(a[i],3)<Math.pow(10,5))
{
sum=sum+a[i]%10;
a[i]=a[i]/10;
}
else{
c++;
}
}
if(c>0)
{
System.out.println(c);
}
}
else{
System.out.print("Invalid input");
}
}
}
3
two cases are passed but 3rd case is not passed still getting a problem...can you please create n as an array and again write the new code by using n as an array
It´s not about we have to take N as an array. Please comment the test case which is failing.