+ 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

20th Jul 2016, 4:30 PM
Charlton J. Dias
Charlton J. Dias - avatar
6 Answers
+ 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 ); } } }
20th Jul 2016, 4:44 PM
WPimpong
WPimpong - avatar
+ 1
sreeraj g your code does not make sense...
20th Jul 2016, 7:26 PM
The Eagle
The Eagle - avatar
+ 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
21st Jul 2016, 2:31 AM
sreeraj g
sreeraj g - avatar
+ 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
21st Jul 2016, 3:12 AM
James
James - avatar
+ 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.
21st Jul 2016, 4:06 AM
Eric Gitangu
Eric Gitangu - avatar
0
the eagle just check again, is it ok?
23rd Jul 2016, 11:28 AM
sreeraj g
sreeraj g - avatar