Q:

Java Program to Find Square and Cube of a Number (N^1, N^2, N^3)

0

Logic:- 

For this problem We need to multiply number or we can use power function for that take an example for better understood this problem take a Number 5 as input and multiply with the same number again like 5 * 5 for cube we need to again multiply with the same number like 5 * 5 * 5 or we can use power function.

Power Function Syntax:- for given example x^y.

pow(x, y)

also, define the data type of x or y and here x is a number and y is the power of x.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Method 2:- Using Power Function

import java.util.Scanner;
import java.lang.*;

public class threenum1
{
    public static void main(String args[])
    {
        int num;
  double a,b,c; 
    
  Scanner sc = new Scanner(System.in);
 
        System.out.print("Enter The Number :\n\n");
        num = sc.nextInt();

  a=Math.pow(num,1);
  b=Math.pow(num,2);
  c=Math.pow(num,3);

  System.out.println("\nOutput Is = " + a + " ,"+ b +" ,"+ c +"\n\n"); 
 }
}

 

Output:

Enter The Number :

10

Output Is = 10.0 ,100.0 ,1000.0

need an explanation for this answer? contact us directly to get an explanation for this answer
need an explanation for this answer? contact us directly to get an explanation for this answer

Method 1:- Simple Without Using Power Function

import java.util.Scanner;
import java.lang.*;

public class threenum
{
    public static void main(String args[])
    {
        int num,a,b,c; 
        Scanner sc = new Scanner(System.in);
 
        System.out.print("Enter The Number :\n\n");
        num = sc.nextInt();
 
  a=num;
  b=num*num;
  c=num*num*num;

  System.out.println("\nOutput Is = " + a + " ,"+ b +" ,"+ c +"\n\n"); 
 }
} 

 

Output:

Enter The Number :

5

Output Is = 5 ,25 ,125

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (2)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now