0

Java nth even no

Logic for nth even no

10th Jul 2019, 7:32 PM
Pawan Kumar
Pawan Kumar - avatar
9 Answers
+ 2
Pawan Shroff : Don't think from program perspective. First think of mathematical logic. To find whether number is even or odd, what we can do is, divide number by 2 if it returns 0 it is even else it is odd. To get nth even no, think of some even number say 12, now divide it by 2, remainder is 0 and quotient is 6 so it is 6th even number. Now if mathematical logic is clear you can put in program easily, isn't it?
11th Jul 2019, 12:18 PM
Popat
Popat - avatar
+ 5
The nth positive even number is 2*n.
10th Jul 2019, 7:34 PM
Diego
Diego - avatar
+ 1
if you count the even numbers up: (in programming you start counting from 0!) 0. 0 1. 2 2. 4 3. 6 4. 8 5. 10 ... the n-th even number is actually 2 * n, n being n-th even number you want
10th Jul 2019, 8:13 PM
Anton Böhler
Anton Böhler - avatar
+ 1
Pawan Shroff n = 7 The first even number is 2. + 2 = 4 (2.) + 2 = 6 (3.) + 2 = 8 (4.) + 2 = 10 (5.) + 2 = 12 (6.) + 2 = 14 (7.) -> 2 + 2 + 2 + 2 + 2 + 2 + 2 = 7 * 2 n = 3 2 + 2 + 2 = 3 * 2 = 6 If you are interested: The formular for nth odd number is n * 2 - 1
10th Jul 2019, 8:21 PM
Denise Roßberg
Denise Roßberg - avatar
0
Diego pls explain in detail
10th Jul 2019, 7:45 PM
Pawan Kumar
Pawan Kumar - avatar
0
Ace and if i want to print only 3rd even no like 6,12,18??
10th Jul 2019, 8:18 PM
Pawan Kumar
Pawan Kumar - avatar
0
every third even number: 2 * 3 = 6 -> start value for(int i = 6; i <= 30; i += 6){ System.out.println(i); }
10th Jul 2019, 8:30 PM
Denise Roßberg
Denise Roßberg - avatar