Java: Array with loop

Go To StackoverFlow.com

17

I need to create an array with 100 numbers (1-100) and then calculate how much it all will be (1+2+3+4+..+100 = sum).

I don't want to enter these numbers into the arrays manually, 100 spots would take a while and cost more code.

I'm thinking something like using variable++ till 100 and then calculate the sum of it all. Not sure how exactly it would be written. But it's in important that it's in arrays so I can also say later, "How much is array 55" and I can could easily see it.

2011-10-07 12:32
by Michael


40

Here's how:

// Create an array with room for 100 integers
int[] nums = new int[100];

// Fill it with numbers using a for-loop
for (int i = 0; i < nums.length; i++)
    nums[i] = i + 1;  // +1 since we want 1-100 and not 0-99

// Compute sum
int sum = 0;
for (int n : nums)
    sum += n;

// Print the result (5050)
System.out.println(sum);
2011-10-07 12:35
by aioobe
Exactly what I was looking for and thanks for the descriptions as well - Michael 2011-10-07 12:48


9

If all you want to do is calculate the sum of 1,2,3... n then you could use :

 int sum = (n * (n + 1)) / 2;
2011-10-07 12:35
by Bala R
Or just print 5050 as the OP mentions n=100.. - Veger 2013-03-06 14:46


6

int count = 100;
int total = 0;
int[] numbers = new int[count];
for (int i=0; count>i; i++) {
    numbers[i] = i+1;
    total += i+1;
}
// done
2011-10-07 12:35
by Charles Goodwin


4

I'm not sure what structure you want your resulting array in, but the following code will do what I think you're asking for:

int sum = 0;
int[] results = new int[100];
for (int i = 0; i < 100; i++) {
  sum += (i+1);
  results[i] = sum;
}

Gives you an array of the sum at each point in the loop [1, 3, 6, 10...]

2011-10-07 12:37
by jefflunt
This would throw an array out of bounds exception for i=100 - jornb87 2011-10-07 13:21
Ah, yes. Corrected loop index, and insertion - jefflunt 2011-10-07 13:28


2

To populate the array:

int[] numbers = new int[100];
for (int i = 0; i < 100; i++) {
    numbers[i] = i+1;
}

and then to sum it:

int ans = 0;
for (int i = 0; i < numbers.length; i++) {
    ans += numbers[i];
}

or in short, if you want the sum from 1 to n:

( n ( n +1) ) / 2

2011-10-07 12:35
by Nico Huysamen


1

If your array of numbers always is starting with 1 and ending with X then you could use the following formula: sum = x * (x+1) / 2

from 1 till 100 the sum would be 100 * 101 / 2 = 5050

2011-10-07 12:37
by TRD


1

this is actually the summation of an arithmatic progression with common difference as 1. So this is a special case of sum of natural numbers. Its easy can be done with a single line of code.

int i = 100;
// Implement the fomrulae n*(n+1)/2
int sum = (i*(i+1))/2;
System.out.println(sum);
2012-08-25 14:58
by Sriram


0

int[] nums = new int[100];

int sum = 0;

// Fill it with numbers using a for-loop for (int i = 0; i < nums.length; i++)

{ 
     nums[i] = i + 1;
    sum += n;
}

System.out.println(sum);

2011-10-07 13:23
by Sashi Kant
@normalocity:: U have an error in ur code as u r declaring an array of 100 cells and trying to access its 101th cell. So what will happen when the loop reaches when i=100. U will access results[100] which is the 101th cell starting from 0.

int sum = 0; int[] results = new int[100]; for (int i = 1; i <= 100; i++) { sum += i; results[i] = sum; - Sashi Kant 2011-10-07 13:27



-1

The Array has declared without intializing the values and if you want to insert values by itterating the loop this code will work.

Public Class Program
{

public static void main(String args[])

{
 //Array Intialization
 int my[] = new int[6];

 for(int i=0;i<=5;i++)

{

//Storing array values in array
my[i]= i;

//Printing array values

System.out.println(my[i]);

}

}

}
2017-11-05 06:41
by rajesh lakkakula
Please add a short description of your solution as well. This will help users in understanding why your solution works - Mayank Patel 2017-11-05 07:58
Added description for the solution.. Thanks Mayan - rajesh lakkakula 2017-11-05 11:54
Ads