wap to calculate the square and cube of a number using scanning class
Answers
Answered by
13
The given problem is solved using language - Java.
import java.util.Scanner;
public class NumberOperation{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
int n=sc.nextInt();
sc.close();
System.out.println("Square: "+n*n);
System.out.println("Cube: "+n*n*n);
}
}
- Line 1: Imports scanner class for taking input.
- Line 2: Start of class.
- Line 3: Start of main() method.
- Line 4: Object of Scanner class is created.
- Line 5: Requesting the user to enter a number.
- Line 6: Number is taken as input.
- Line 7: Scanner class is closed as there is no need of it.
- Line 8: Square of the number is displayed.
- Line 9: Cube of the number is displayed.
- Line 10: End of main().
- Line 11: End of class.
See attachment for output.
Attachments:
Similar questions