-4

I need help to write a java program that adds up 1, 3, 7, 13.... and so on

What I need is a program that does 1 + 3 + 7 + 13 + 21.... + n

n can be whatever the user wants. The sequence is starts from 1 and adds each even number, so from 1 it adds 2 and then from that number it adds 4 and from that number it adds 6 and it keeps on going until you get to n

Is this right in any way? I had a complete guess

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Odd4 extends JFrame
    implements ActionListener {

    private JButton button;
    private JPanel panel;

    public static void main(String [] args) {
        Odd4 frame = new Odd4();
        frame.setSize(100, 100);
        frame.createLine();
        frame.show();
    }

    private void createLine() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window=getContentPane();
        window.setLayout (new FlowLayout());

        button = new JButton("OK");
        window.add(button);
        button.addActionListener(this);

    }

    public void actionPerformed(ActionEvent event) {
        int n;
        int sum = 0; 
        int i;
        int j = 1;
        String nString;
        nString = JOptionPane.showInputDialog("n:");
        n = Integer.parseInt(nString);
        for (i = 1; i <= n; i ++){
            if (i%2 == 0)

            do {
                j=j+i;
                sum = sum + j;
            }
            while (j <= n);
        }
        JOptionPane.showMessageDialog(null, "Total is: " + sum);
    }

}
14
  • 1
    Take a shot at it yourself. When you get stuck, some back with a specific question.
    – Mike B
    Apr 14, 2014 at 20:24
  • FYI, we aren't going to write your code for you. You must make an attempt and we will help you where you get stuck.
    – takendarkk
    Apr 14, 2014 at 20:26
  • Although I totally agree with @MikeB I would look into the mod operator to kinda guide you. Heres some google keywords I would use: mod, even, loop, java. Apr 14, 2014 at 20:26
  • 2
    @AnthonyJClink How, in heavens name, would the mod operator be helpful here? It is simple addition!
    – Ingo
    Apr 14, 2014 at 20:27
  • 1
    This question appears to be off-topic because it is a code writing request. Apr 14, 2014 at 20:30

2 Answers 2

1

I'll give the algorithm itself, but as pseudocode. Implementing it in Java is left as an exercise to the reader.

  1. Initialize variable n at 0;
  2. Initialize variable s at 1;
  3. Begin loop;
    1. Print out s;
    2. Increment n;
    3. Add 2n to s;
  4. End loop;
3
  • Can the downvoter or another person explain the issue?
    – nanofarad
    Apr 14, 2014 at 20:35
  • this is wrong, I think Apr 14, 2014 at 20:36
  • it is wrong... it would start at 3...
    – T McKeown
    Apr 14, 2014 at 20:42
0
int x= 1;
int c = 0;
while(true){
   x = x + (c *2);
   System.out.println(x);
   c++;

}

Will output:

1
3
7
....
1
  • this isn't what I want, I need a program that does 1 + 3 + 7 + 13 + 16.... + n, n can be input by the user Apr 14, 2014 at 21:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.