-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPower.java
More file actions
35 lines (30 loc) · 769 Bytes
/
Power.java
File metadata and controls
35 lines (30 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.Scanner;
public class Power {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter a base Number");
int base=sc.nextInt();
System.out.println("Enter a raise/power Number");
int pow=sc.nextInt();
if (pow>=0) {
System.out.println(base+"^"+pow+" ="+PowerP(base,pow));
}
else if (pow<0) {
System.out.println(base+" is base and "+pow+" is exponential or power and the answer is "+PowerN(base,pow));
}
}
public static int PowerP(int base,int pow){
int power=1;
while(pow>0){
power*=base;
pow--;
}
return power;
}
public static double PowerN(int base,int pow){
double num=1;
double din=PowerP(base,-pow);
double expo=num/din;
return expo;
}
}