I made a simple program - conservative sequence integers with a sum, for example input: 15, the output should be 1, 2, 3, 4, 5

I made this program in c++ and it worked successfully, see below:

#include <iostream>
#include <string>
using namespace std;

int main() {
    int num, i, j, k;
    int sum = 0;

    cout << "Num? ";
    cin >> num;

    for (i = 1; i <= num/2+1; i++) {
        j=i;
        while(sum<num) {
                     sum = sum + j;
                     j++;}
        if (sum==num) {
                      for (k = i; k < j; k++)
                      if (!k) {
                      cout << "Not an exact sum!";
                      }
                      else {
                      cout << " " << k;
                      }
        }
    }
    cout << endl;

    system("pause");
    return 0;
}

However, the problem is I'm translating this into java and it didn't work. See the java code below, any suggestions?

import java.util.Scanner;
public class Week2 {
  public static void main(String[] args) 
  {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Type an integer:");
    int n = keyboard.nextInt();

    // write your code here; k is the result of the computation //
    int i, j = 0, k;
    int sum = 0;

    for (i = 1; i <= n/2+1; i++)
        j = i;
    while(sum < n)
        sum = sum + j;
        j++;
    if (sum == n)
        for (k = i; k < j; k++)

    // do not modify anything beyond this comment //

    if (k == 0)
      System.out.println("The number "+n+" is not an exact sum");
    else
      System.out.println("The number "+n+" is the sum of integers from 1 to "+k);
  }
}

Java's rules about braces and blocks are nearly identical to C++, so if you did write that C++ code, you should see why your Java version is quite broken.

Line 15, where is the curly bracket of while loop? If you do not have it, it will execute only sum=sum+j forever... Also, other if-statement/for loop in your Java code, there is NO curly bracket to define the scope...

it didn't work

Please explain. Show the console from when you execute the program including its output and explain what is wrong with it.

One possible problem I see is the code does not use curly brackets to include code in if and while statements. Its best to always use them so there is no confusion about what statements are in the scope of the if & while statements and which are not. The indentation of the posted code leaves doubts about where some statements are intended to be.

It worked! But the output is not what I expected.

import java.util.Scanner;
public class Week2 {
  public static void main(String[] args) 
  {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Type an integer:");
    int n = keyboard.nextInt();

    // write your code here; k is the result of the computation //
    int i, k;
    int sum = 0, j = 0;

    for (i = 1; i <= n/2+1; i++) {
        j = i;
    while(sum < n) {
        sum = sum + j;
        j++;
    }
    if (sum == n) {
        for (k = i; k < j; k++)

    // do not modify anything beyond this comment //

    if (k == 0)
      System.out.println("The number "+n+" is not an exact sum");
    else
      System.out.println("The number "+n+" is the sum of integers from 1 to "+k);
  }
}}}

The output should be just one line: The number n is the sum of integers from 1 to k
instead of these multiple lines below

Type an integer:
15
The number 15 is the sum of integers from 1 to 1
The number 15 is the sum of integers from 1 to 2
The number 15 is the sum of integers from 1 to 3
The number 15 is the sum of integers from 1 to 4
The number 15 is the sum of integers from 1 to 5

The formatting of the posted code needs work:

Coding multiple '}}}'s on the same line makes it hard to read and understand the logic of the code.
Each '}' should be on its own line and vertically below the start of the line with the pairing '{'

Lines within a while or if statement should be indented 3-4 spaces to show the nesting of the logic.

the output is not what I expected.

Please explain and show what you want the output to be.

Yeah I understand about the { }

Let's say I type as input: 15
The output should be like that below

The number 15 is the sum of integers from 1 to 5

This program defines as a sum of conservative numbers so 1+2+3+4+5 = 15

Yeah I understand about the "{}"

If you want anyone to work with your code, I suggest that you fix it.

Is the last line of output from the program correct? Is the problem that there are four lines printed that are wrong?

Try doing some debugging to see why the other 4 lines are being printed.

I fixed it. But I still can't figure out with the output. Is there any function that shows the last integer?

import java.util.Scanner;
public class Week2 {
  public static void main(String[] args) 
  {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Type an integer:");
    int n = keyboard.nextInt();

    // write your code here; k is the result of the computation //
    int k;
    int sum = 0, j = 1;

    while(sum < n) {
        sum += j;
        j++;
    }
    for (k = 1; k < j; k++)

    // do not modify anything beyond this comment //

    if (k == 0)
      System.out.println("The number "+n+" is not an exact sum");
    else
      System.out.println("The number "+n+" is the sum of integers from 1 to "+k);
  }
}

Is there any function that shows the last integer?

Which integer is the "last" integer?

What statements are supposed to be in the for loop on line 17?
The lines following line 17 are not indented like they are at the same logic level as line 17.

I don't think you have the correct algorithm for solving the problem. Can you write out pseudo code for solving the problem and post it so we can see what you think the logic should be.

Ok I fixed everything! Except for the

System.out.println("The number "+n+" is not an exact sum");

is not working. When I input 5, it showed "The number 5 is the sum of integers from 1 to 3" which is not right. Any suggestions?

import java.util.Scanner;
public class Week2 {
  public static void main(String[] args) 
  {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Type an integer:");
    int n = keyboard.nextInt();

    // write your code here; k is the result of the computation //
    int k = 0;
    int sum = 0;

    while(sum < n/2+1) {
        sum += k;
        k++;
        StringBuffer sb = new StringBuffer(k);
        sb.append(k);
    }

    // do not modify anything beyond this comment //

    if (k == 0)
      System.out.println("The number "+n+" is not an exact sum");
    else
      System.out.println("The number "+n+" is the sum of integers from 1 to "+k);
  }
}

I don't think you have the correct algorithm for solving the problem. Can you write out pseudo code for solving the problem and post it so we can see what you think the logic should be.

For testing, what should be the print out if 14 is entered?

Even though the thread is marked as solved, I want to point out that the algorithm in the code is wrong as NormR1 said.

1)Line 13, it will prematurely end the loop before it should because the sum value should be compared with the n value instead of half of the value.
2)Line 22, the comparison here should be between the sum and n. If the sum & n are equal, there is a set of cumulative integers that can add up to the n value; otherwise, there is no answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.