0

I am trying a task with arrays: add two elements and check if the sum is less than or equal to 50. If the condition is satisfied, it should break.

Example program:

public class HelloWorld {

     public static void main(String []args)
     {
         int[] nums = new int[2];

         for (int i = 0; i < 100; i++)
         {
             nums[i] = i + 1;
             System.out.println(i);
         }
         System.out.println(nums[1]);
         System.out.println(nums[2]);

         if  (nums[0]+nums[1]<=50)
         {
             System.out.printf("Sucessfully finished");
         }     
     }
}

Of course, my program is not working. I want i value to store in the two elements
nums[0] = 1 and nums[1] = 2. I also want to add these two elements and check if the sum is less than or equal to 50. I have allocated two elements in the array which means nums want to add and check the current two elements of i and clear and adds next two elements and check if its less than or equal to 50.

nums[0]=1;
nums[1]=2; check <=50 . fails clear the the array elements and store next i value           
nums[0]=3;
nums[1]=4; check <=50 . fails clear the the array elements and store next i value              
...                        
nums[0]=25;                      
nums[1]=26; check <=50 .
4
  • 1
    Important note: <= denotes less than or equal while >= denotes greater than or equal. Commented Nov 27, 2013 at 16:38
  • 1
    In your if statement you have "(nums[1]+nums[2]<=50)". When using arrays/lists the first element is always 0. So your array with two ints can be accessed using nums[0] and nums[1] (you have actually mentioned this later one). For what you are doing I'd recommend you just use two variables noOne and noTwo. It would make it easier to read. Commented Nov 27, 2013 at 16:43
  • Don't forget the two System.out.println() statements too. :) Commented Nov 27, 2013 at 16:48
  • I think you have your logic backwards. nums[0]=1;nums[1]=2;check <= 50 should be true since 1 + 2 = 3; 3 <= 50 Commented Nov 27, 2013 at 16:57

4 Answers 4

2

There are a lot of ways to do this but here is a nifty trick that solves exactly this kind of problem.

int[] nums = new int[2];

for (int i = 0; i < 100; i++) {
    nums[i % nums.length] = i + 1;

    if (nums[0] + nums[1] <= 50) {
        System.out.println("sum is less than or equal to 50");
        break;
    }
}

What the mod operator (%) does is calculate the remainder of i based on the array's length. This ensures that i goes from 0 to 99 but the array index always "resets" and stays within the range of the array. For example after i == 0 and i == 1, i will be incremented to out of bounds at i == 2 but 2 % 2 == 0. When i == 3, 3 % 2 == 1 and so on.

But as a side note, the condition you've described ("if the sum is less than or equal to 50...it should break") will be satisfied immediately (sums 1 at nums[0] and 0 at nums[1]) and the loop will not execute past the first iteration (i == 0). I'm not sure that's what you are wanting. Do you mean "not less than or equal to 50"?

int[] nums = new int[2];

for (int i = 0; i < 100; i++) {
    nums[i % nums.length] = i + 1;

    if (nums[0] + nums[1] > 50) {
        System.out.println("sum was NOT less than or equal to 50");
        break;
    }
}

As an alternate solution finding this result can be very much shortened to the following while loop:

int i = 0;

// note sum of two consecutive integers will never be even (never 50)
while (i + ++i < 50);

System.out.println("min increments with sum > 50 was " + (i - 1) + " and " + i);

The output is min increments with sum > 50 was 25 and 26.

Sign up to request clarification or add additional context in comments.

3 Comments

head shot .. code works and your explanation was good thanks for the clarity
how to check the value inside the nums[0] and nums[1] .. bcoz i am getting 1 and 0
Not sure what you mean "check the value"? They will (correctly) be 1 and 0 on the first iteration of the loop.
0

I believe, you need 1 more for loop, to go about checking each num[i] + num[i+1]. You can do it in a single for loop as well, but kept the code as simple as possible for your clarity.(As you are new to java programming ;))

public class HelloWorld{

public static void main(String[] args) {
    int[] nums = new int[100];

    for (int i = 0; i < 100; i++) {
        nums[i] = i + 1;
        System.out.println(i);
    }

    for (int i = 0; i < 99; i++) {
        System.out.println(nums[i]);
        System.out.println(nums[i+1]);

        if (nums[i] + nums[i+1] == 50) {
            System.out.printf("Successfully finished");
        }
    }
}
}

3 Comments

i tried with two loop .. as you mentioned above.. but i want to make it in a single loop ... this is what i am trying for 2 days
I think OP just wants nums to have two elements, and in the loop, assign (I think) i to one element and i+1 to the other, then check if the sum is less than 50. (Rather than populating nums with 100 elements, then checking those elements, which is what your code does.)
Further nitpick. The sum of two consecutive integers will never equal 50, so your test condition will never be met. The closest you'll get will be 24 + 25 == 49 and then 25 + 26 == 51
0

I'm not sure if this is what you meant. Have a try.

public static void main(String[]    args) {
    int[] nums = new int[100];
    nums[0] =1;
    for (int i = 1; i < 100; i++) {
        nums[i] = i + 1;
        if ((nums[i] +nums[i-1]) >= 50) {
             System.out.printf("Successfully finished");
        } 
    }
  }

Comments

0

I'm not entirely sure on what your end goal is. If you just want to add two numbers (i, and i+1) and see if they are less than 50 then you can use this.

for (int i =0; i < 100; i++) {
    int j = i+1;
    int total = i+j;
    if((i+(i+1)) < 50) {
        System.out.println("Numbers '" + i + "' and '" + j + "' equal '" + total + "'.");
    }
}

This will print out all the pairs of numbers that add to less than 50 along with what they add to.

I accept that you're probably wanting something more. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.