HELP ME TO SOLVE THIS JAVA FOR LOOP PROGRAM

• December 22nd, 2012, 12:09 PM
HELP ME TO SOLVE THIS JAVA FOR LOOP PROGRAM
WRITE JAVA FOR WHILE SYNATX CODE GIVE OUT

1^1 + 2^2 + 3^3 + 4^4 + 5^5

Result=3413
• December 22nd, 2012, 12:17 PM
Norm
Re: HELP ME TO SOLVE THIS JAVA FOR LOOP PROGRAM
You have a problem with your keyboard's cap lock key. All the letters in your post are uppercase.
Can you fix your post to be in normal case?

Post the code you are working on so we can see what your problem is.
• December 22nd, 2012, 01:09 PM
matinm90
Re: HELP ME TO SOLVE THIS JAVA FOR LOOP PROGRAM
I am not sure what your code is but one way of writing this could be:
Code :

```int i = 0; while(true) { take the power of i to itselft (ie. i^i) then add it to some instance variable.   after that increment i. keep doing this forever and ever, unless you need to have a limit for how long it needs to go. in which case the while loop parameter could be "while(i < 40)" for example. }```
• December 22nd, 2012, 01:55 PM
aesguitar
Re: HELP ME TO SOLVE THIS JAVA FOR LOOP PROGRAM
Code java:

```  long val = /*start value for val*/ ;   for(int i = 1; i <= /*upper limit*/; /*increment*/) { val += //mathematical expression for the series }```
• December 22nd, 2012, 02:08 PM
Re: HELP ME TO SOLVE THIS JAVA FOR LOOP PROGRAM
//this program give output 1^3 + 2 ^3 + 3^3 + 4^4....upto n input numbers.......

//but now i wana to get out put in this form 1^1 + 2^2 + 3^3 + 4^4 .....upto n input numbers......
//pls help me to make this program..........

import java.util.*;

class cubeseries{
public static void main(String arg[]){
int s=0 , num ,i;
Scanner input = new Scanner(System.in);
System.out.print("ENTER NUMBER : ");
num = input.nextInt();

for(i = 1 ; i <= num ; i++)

s = s+i*i*i;
System.out.print(s);

}
}
• December 22nd, 2012, 02:17 PM
Norm
Re: HELP ME TO SOLVE THIS JAVA FOR LOOP PROGRAM
The code does only this:
i*i*i is i^3

Try writing a loop to compute the power instead of using a single expression on one line.
if x = i
then x*i is i*i
then x*i is i*i*i
and again x*i is i*i*i*i