This is a simple program to print a square or cube in java .
First take the input from the user using Scanner class object then check the user input is valid or not using if condition if input is valid than the code which is written inside the if condition will be work otherwise it will print the statement (invalid input) from else.
equalsIgnoreCase() is present in string class it ignore the uppercase or lowercase input it will treat same (a or A) .
After getting a valid input from the user it will again check either input is s or q if input is s than first if condition will work and if input is c than second .
Than it will ask to type a no. when user will type the no. it will go to the method and return the square or cube than using System.out.println("square of "+n+" is = "+square(n)); or System.out.println("cube of "+n+" is = "+cube(n)); line it will print the output .
public class SquareAndCube
{
public static void main(String[] args)
{
Scanner scn =new Scanner(System.in);
System.out.println("Press s for square OR q for cube");
String s=scn.next();
if(s.equalsIgnoreCase("s") ||s.equalsIgnoreCase("q"))
{
if(s.equalsIgnoreCase("s"))
{
System.out.println(" Type no. to find square");
int n=scn.nextInt();
System.out.println("square of "+n+" is = "+square(n));
}
else if(s.equalsIgnoreCase("q"))
{
System.out.println("Type no. to find cube");
int n=scn.nextInt();
System.out.println("cube of "+n+" is = "+cube(n));
}
}
else
{
System.out.println("invalid input .. Please enter either s or q");
}
}
public static int square(int n)
{
return n*n;
}
public static int cube(int n)
{
return n*n*n;
}
}
First take the input from the user using Scanner class object then check the user input is valid or not using if condition if input is valid than the code which is written inside the if condition will be work otherwise it will print the statement (invalid input) from else.
equalsIgnoreCase() is present in string class it ignore the uppercase or lowercase input it will treat same (a or A) .
After getting a valid input from the user it will again check either input is s or q if input is s than first if condition will work and if input is c than second .
Than it will ask to type a no. when user will type the no. it will go to the method and return the square or cube than using System.out.println("square of "+n+" is = "+square(n)); or System.out.println("cube of "+n+" is = "+cube(n)); line it will print the output .
public class SquareAndCube
{
public static void main(String[] args)
{
Scanner scn =new Scanner(System.in);
System.out.println("Press s for square OR q for cube");
String s=scn.next();
if(s.equalsIgnoreCase("s") ||s.equalsIgnoreCase("q"))
{
if(s.equalsIgnoreCase("s"))
{
System.out.println(" Type no. to find square");
int n=scn.nextInt();
System.out.println("square of "+n+" is = "+square(n));
}
else if(s.equalsIgnoreCase("q"))
{
System.out.println("Type no. to find cube");
int n=scn.nextInt();
System.out.println("cube of "+n+" is = "+cube(n));
}
}
else
{
System.out.println("invalid input .. Please enter either s or q");
}
}
public static int square(int n)
{
return n*n;
}
public static int cube(int n)
{
return n*n*n;
}
}