Computer Science, asked by aman3258kg, 7 hours ago

wap to calculate the square and cube of a number using scanning class​

Answers

Answered by anindyaadhikari13
13

\textsf{\large{\underline{Solution}:}}

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

   }

}

\textsf{\large{\underline{Explanation}:}}

  1. Line 1: Imports scanner class for taking input.
  2. Line 2: Start of class.
  3. Line 3: Start of main() method.
  4. Line 4: Object of Scanner class is created.
  5. Line 5: Requesting the user to enter a number.
  6. Line 6: Number is taken as input.
  7. Line 7: Scanner class is closed as there is no need of it.
  8. Line 8: Square of the number is displayed.
  9. Line 9: Cube of the number is displayed.
  10. Line 10: End of main().
  11. Line 11: End of class.

See attachment for output.

Attachments:
Similar questions