KnowledgeBoat Logo

Computer Applications

Write a program in Java to input two numbers (say, n and m) using the Scanner class. Display the results of n raised to the power m (nm) and mn.

Java

Input in Java

26 Likes

Answer

import java.util.*;
public class KboatExponent
{
    public static void main(String args[])
    {
        int n, m;
        double expo;
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter a number (n): ");
        n = in.nextInt();
        
        System.out.print("Enter a number (m): ");
        m = in.nextInt();
        
        expo = Math.pow(n,m);
        System.out.println(n + " raised to the power " 
                             + m + " = " + expo); 
                             
        expo = Math.pow(m,n);
        System.out.println(m + " raised to the power " 
                             + n + " = " + expo);
    }
}

Output

BlueJ output of Write a program in Java to input two numbers (say, n and m) using the Scanner class. Display the results of n raised to the power m (n m ) and m n .

Answered By

16 Likes


Related Questions