AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Miracle Cubes-30 min

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

corejava x 353
programming x 168
Posted On : 2016-10-25 11:45:20.0
profile Akash Prasad - anyforum.in Akash Prasad
2550
up-rate
-1
down-rate

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;
}
}

Posted On : 2016-10-27 22:35:03
Satisfied : 3 Yes  4 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188222642
Reply This Thread
up-rate
2
down-rate
Comments
Your Code is not cleared my test cases because there is no n value and no command
profile Akash Prasad - anyforum.in Akash Prasad
25  5  0
Posted On :2016-10-28 11:41:22.0
Leave a Comment
can you please provide the code that is asked in question situation
profile Akash Prasad - anyforum.in Akash Prasad
25  5  0
Posted On :2016-10-28 11:42:21.0
Leave a Comment
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;
}
}
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1882  22642
Posted On :2016-10-29 22:55:18.0
Leave a Comment

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");
}
}
}

Posted On : 2016-10-31 13:01:57
Satisfied : 0 Yes  2 No
profile Dinesh - anyforum.in Dinesh
030
Reply This Thread
up-rate
3
down-rate
Comments
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
profile Akash Prasad - anyforum.in Akash Prasad
25  5  0
Posted On :2016-10-31 14:12:11.0
Leave a Comment
It´s not about we have to take N as an array. Please comment the test case which is failing.
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1882  22642
Posted On :2016-11-02 19:49:37.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in