import java.io.*;
import java.util.*;
public class SquareOfNumber{
Scanner scan=new Scanner(System.in);
double num=0;
double square;
public void input()throws IOException{
System.out.println("Enter any number to find its square: ");
num=scan.nextDouble();
}
public void compute(){
square=num*num;
}
public void display(){
System.out.println("Square of "+num+" is: "+square);
}
public static void main(String[] args)throws IOException{
SquareOfNumber obj=new SquareOfNumber();
obj.input();
obj.compute();
obj.display();
}
}
Above program is to calculate the square of any digit entered by user. If you want to find the cube of any number, then just change the code line "square=num*num;" to "<variable_name>=num*num*num;"
import java.util.*;
public class SquareOfNumber{
Scanner scan=new Scanner(System.in);
double num=0;
double square;
public void input()throws IOException{
System.out.println("Enter any number to find its square: ");
num=scan.nextDouble();
}
public void compute(){
square=num*num;
}
public void display(){
System.out.println("Square of "+num+" is: "+square);
}
public static void main(String[] args)throws IOException{
SquareOfNumber obj=new SquareOfNumber();
obj.input();
obj.compute();
obj.display();
}
}
Above program is to calculate the square of any digit entered by user. If you want to find the cube of any number, then just change the code line "square=num*num;" to "<variable_name>=num*num*num;"
Comments