+ 4

Anyone make a program of this logic in java

user have to input 2 numbers... example:- 2 and 3 okay... then make a program that it will find the power . i mean... 2 to the power 3... i.e. 2×2×2 without using any math library or any library just do it with loop or with anything....

21st Sep 2017, 3:26 PM
Manjit Kumar
Manjit Kumar - avatar
4 Answers
+ 9
He did say no math library though, so maybe create your own pow method: // This is only for positive values of b static int pow(int a, int b){ int total = a; if (b == 0) total = 1; for(int i = 1; i < b; i++) total *= a; return total; } Then you can write: pow(int,int); Instead of: Math.pow(int,int);
21st Sep 2017, 5:05 PM
Rrestoring faith
Rrestoring faith - avatar
+ 10
Scanner s = new Scanner(System.in); System.out.println(Math.pow(s.nextInt(), s.nextInt()));
21st Sep 2017, 4:19 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 9
a=2,b=3; while (b !=0) { a *=a; b--;}
21st Sep 2017, 9:13 PM
Changed
Changed - avatar
+ 7
int result = 1; Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int power = sc.nextInt(); while(power>0) { result*=a; power--; }
21st Sep 2017, 9:19 PM
David Akhihiero
David Akhihiero - avatar