Loading...
Loading...
Loading...
A maths quiz asks two things about the numbers from 1 to n: the total of the even numbers and the total of the odd numbers. Read n and print both totals.
Even: E and then Odd: O, where E is the total of the even numbers and O the total of the odd numbers from 1 to n.Example
Input
10
Output
Even: 30 Odd: 25
Even: 2 + 4 + 6 + 8 + 10 = 30. Odd: 1 + 3 + 5 + 7 + 9 = 25.
Try the problem first. Open one hint at a time.
Keep two totals that start at 0.
Loop over 1 to n. Use i % 2 == 0 to decide which total gets i.
The totals can pass 2 billion, so use long for them.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor