+ 2
how to generate increasing numbers with increasing powers?
how to generate increasing numbers with increasing powers? for example 1^1, 2^2, 3^3, ...... n^n
6 Respuestas
+ 3
You can change n to take value from user input instead.
import java.lang.Math;
public class Program
{
public static void main(String[] args) {
int n = 3;
for (int i=1; i<=n; i++){
double result = Math.pow(i,i);
System .out .println (result );
}
}
}
+ 1
sreeraj g your code does not make sense...
+ 1
You can change n to take value from user input instead.
public class Program
{
public static void main(String[] args) {
int n = 3;
for (int i=1; i<=n; i++){
int result=i;
for(int j=2;j<=i;j++){
result*=i;
}
System .out .println (result );
}
}
}
this avoids using math
+ 1
Imma support Wachirakorn. Math functions were designed to be used, use em. One thing I'd like to add though, you don't need to import the math functions. They're already included in the default packages
+ 1
Just a mention, in low level assembly instructions, instructions like multiplication use successive additions, if you'd have to write the Math.pow function in assembly, you'd probably result to using successive additions like 2^3= 2 * 2 * 2= (2+2) * 2= (4+4) == 8, recursion could achieve this. This approach would only be a fancy and long winded way of achieving what you're looking for but if you were a systems engineer and you had to work close to the metal, say, in assembly you would have to do something of the sort, sorry for going off on a tangent.
0
the eagle just check again, is it ok?