0

i have this problem with regards to java data

the problem accepts input number and then output the sum of the square from 1 to the n(1 to the no. input) thus, if the input is 4 the output should be 288 because:

1 raise to 1 + 2 raise to 2 + 3 raise to 3 + 4 raise to 4
1 + 4 + 27 + 256 = 288


i have my code but i'm stuck gotta help me outta here

import javax.swing.*;
public class RRunner {
public static void main(String[] args){

    int i; // counter
    int num; // number to be input
    int pw; // power of sum

    num = Integer.parseInt(JOptionPane.showInputDialog("Enter a number: "));
    for( i=1; i<=num; i++ ){
    
        pw = (num*num) + i;
            System.out.println("The answer is "+ pw);
   
    }

}
}

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by huyyaya
0
import javax.swing.*;
public class RRunner {
public static void main(String[] args){

int i; // counter
int num; // number to be input
int pw=0; // power of sum

num = Integer.parseInt(JOptionPane.showInputDialog("Enter a number: "));
for( i=1; i<=num; i++ ){
pw += Math.pow(i, i);

}
System.out.println("The answer is "+ pw);
}
}

small change

-1

nice solution! :)
<FAKE SIGNATURE>

Edited by peter_budo: Keep It On The Site - Do not manually post &quot;fake&quot; signatures in your posts.

Votes + Comments
Useless.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.