0
Java nth even no
Logic for nth even no
9 ответов
+ 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?
+ 5
The nth positive even number is 2*n.
+ 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
+ 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
0
Diego pls explain in detail
0
Ace and if i want to print only 3rd even no like 6,12,18??
0
every third even number:
2 * 3 = 6 -> start value
for(int i = 6; i <= 30; i += 6){
System.out.println(i);
}