<< Back Challenge 1 (Addition Addiction!) Solution - Computer Science Club

without loop, with array

int[] numbers = new int[5];

Scanner sc = new Scanner(System.in);

numbers[0] = sc.nextInt();
numbers[1] = sc.nextInt();
numbers[2] = sc.nextInt();
numbers[3] = sc.nextInt();
numbers[4] = sc.nextInt();

int total = numbers[0] + numbers[1] + numbers[2] + numbers[3] + numbers[4] + numbers[5];

System.out.println(total);

with loop, with array

int[] numbers = new int[5];

Scanner sc = new Scanner(System.in);

for (int i = 0; i < 5; i++)
 numbers[i] = sc.nextInt();

int total = 0;

for (int i = 0; i < 5; i++)
 total += numbers[i];

System.out.println(total);

without loop, without array

int total = 0;

Scanner sc = new Scanner(System.in);

total += sc.nextInt();
total += sc.nextInt();
total += sc.nextInt();
total += sc.nextInt();
total += sc.nextInt();

System.out.println(total);

with loop, without array

int total = 0;

Scanner sc = new Scanner(System.in);

for (int i = 0; i < 5; i++)
 total += sc.nextInt();

System.out.println(total);