+ 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....
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);
+ 10
Scanner s = new Scanner(System.in);
System.out.println(Math.pow(s.nextInt(), s.nextInt()));
+ 9
a=2,b=3;
while (b !=0)
{ a *=a; b--;}
+ 7
int result = 1;
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int power = sc.nextInt();
while(power>0)
{
result*=a;
power--;
}