user13267091
user13267091

Reputation: 59

how to find the power of that number to that digit

Ram is in the process of learning powers of a number. He is given a number and a digit. He has to find the power of that number to that digit.

Example if the number is 10 and the digit is 5 the output should be 105 = 100000. If either of the input is negative, the output should be “Invalid Input”.

Help him do this by writing a program in java. Create a class "Power.java" and write the main method in it. Don't use in-built method to find the power.

Sample Input 1 : Enter the number 5 Enter the digit 3 Sample Output 1 : 125

Sample Input 2 : Enter the number 18 Enter the digit 4 Sample Output 2 : 104976

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long n, p, result = 1;
    System.out.println("Enter the number");
    n = sc.nextLong();
    System.out.println("Enter the digit");
    p = nextLong();

    if (n < 0 || p < 0) {
        System.out.println("Invalid Input");
        return;
    }
    if (n >= 0 && p == 0) {
        result = 0;
    } else if (n == 0 && p >= 1) {
        result = 0;
    } else {
        for (int i = 1; i <= p; i++) {
            result = result * n;
        }
    }
    System.out.println(result);
}

During the execution of this program a test case is failed showing "check logic for power of a number -5"
i don't know what kind of mistake i have made in the program.

Upvotes: 0

Views: 4922

Answers (2)

Shreya Chandhok
Shreya Chandhok

Reputation: 1

 import java.util.*;
    public class Power {
         public static void main(String args[]){
             Scanner scan = new Scanner (System.in);
             long n, p;
             System.out.println("Enter the number");
             n = scan.nextLong();
             System.out.println("Enter the digit");
             p = scan.nextLong();
             if(n < 0 || p < 0) {
                System.out.println("Invalid input.");
                return;
             }
             if(n == 0 && p == 0) {
                 System.out.println("Invalid input");
                 return;
             }
             if (n > 0 && p == 0) {
                 System.out.println("1");
                 return;
             }
             if(n == 0 && p > 0) {
                 System.out.println("0");
                 return;
             }
             else 
                 System.out.println(power(n, p));
         }
         private static long power(long n, long p) {
             long result = n;
             for(int i=1; i<p; i++)
                 result *= n;
             return result;
         } 
   }

Upvotes: 0

Nitin Bisht
Nitin Bisht

Reputation: 5331

Try this:

public class Main {

    public static void main(String args[]){
        Scanner scan = new Scanner (System.in);
        long n, p;
        n = scan.nextLong();
        p = scan.nextLong();
        if(n < 0 || p < 0) {
            System.out.println("Invalid input.");
            return;
        }
        if(n == 0 && p == 0) {
            // Math.pow(0, 0) = 1
            System.out.println("1");
            return;
        }
        if (n > 0 && p == 0) {
            System.out.println("Invalid input.");
            return;
        }
        if(n == 0 && p > 0) {
            System.out.println("Invalid input.");
            return;
        }
        else 
            System.out.println("Result:  " + power(n, p));
    }

    private static long power(long n, long p) {
        long result = n;
        for(int i=1; i<p; i++)
            result *= n;
        return result;
    }
}

Output:

enter image description here

Upvotes: 1

Related Questions