In: Computer Science
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube of the number. The program should be called CubeNumbers. Use a value returning method with parameter, call the method myCube.
Dear Learner,
Here is the code for your solution
import java.util.Scanner;
public class Main
{
public static long myCube (int number) //defining the
function
{
long result = number * number * number; //calculating
the cube and storing it in result variable
return result; //returning the
result variable
}
public static void main (String[]args)
{
Scanner input = new Scanner (System.in); //declaring
scanner object for input
System.out.print ("Enter a number: ");
int number = input.nextInt (); //taking user
input
long result = myCube (number); //calling the function
and storing the returned value in result
System.out.print ("Cube of " + number + " is " +
result); //printing the result
}
}
SCREENSHOT OF THE CODE:
SAMPLE OUTPUT:
I hope I have explained the question the way you were expecting. If you still have any doubts or want any further explanation, feel free to ask us in the comments.
Please leave a like if this was helpful.
Thanks,
Happy Studying.