Skip to content

Instantly share code, notes, and snippets.

@Peters8090
Created November 11, 2019 16:29
Show Gist options
  • Save Peters8090/4edbfbeb557a8a0ed48809089f29e343 to your computer and use it in GitHub Desktop.
Save Peters8090/4edbfbeb557a8a0ed48809089f29e343 to your computer and use it in GitHub Desktop.
[Computer programming homework] Write a program, in which we create an array of integers of length 10, which is initialized with any numbers from 1 to 100. The program has to print that array and the sum of all its elements.
import java.util.Random;
public class ArraysAndLoops {
public static void main(String[] args) {
int[] array = new int[10];
int arraySum = 0;
System.out.println("Array: ");
for(int i = 0; i < array.length; i++)
{
array[i] = new Random().nextInt(100);
arraySum += array[i];
System.out.println(array[i] + ",");
}
System.out.println("\nArray Sum: " + arraySum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment