In this program, You will learn how to display even numbers from 1 to 100 in R.

R Example: How to display even numbers from 1 to 100 in R

1 2 3 4 5 6 7 |
for (num in 1:100) { if (num %% 2 == 0) { print(paste("Even number is :", num)) } } |

The code uses a `for`

loop to iterate over a sequence of numbers from 1 to 100. For each iteration of the loop, the loop variable `num`

takes on the value of the current number in the sequence.

Inside the loop, the code uses the `if`

statement to check if `num`

is even by using the modulo operator (`%%`

) to compute the remainder of `num`

divided by 2. If the remainder is 0, it means that `num`

is evenly divisible by 2 and therefore is even. If `num`

is even, the code prints a message to the console saying that `num`

is an even number.

This code will print all even numbers between 1 and 100 to the console, but it will not print the odd numbers.

Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
[1] "Even number is : 2" [1] "Even number is : 4" [1] "Even number is : 6" [1] "Even number is : 8" [1] "Even number is : 10" [1] "Even number is : 12" [1] "Even number is : 14" [1] "Even number is : 16" [1] "Even number is : 18" [1] "Even number is : 20" [1] "Even number is : 22" [1] "Even number is : 24" [1] "Even number is : 26" [1] "Even number is : 28" [1] "Even number is : 30" [1] "Even number is : 32" [1] "Even number is : 34" [1] "Even number is : 36" [1] "Even number is : 38" [1] "Even number is : 40" [1] "Even number is : 42" [1] "Even number is : 44" [1] "Even number is : 46" [1] "Even number is : 48" [1] "Even number is : 50" [1] "Even number is : 52" [1] "Even number is : 54" [1] "Even number is : 56" [1] "Even number is : 58" [1] "Even number is : 60" [1] "Even number is : 62" [1] "Even number is : 64" [1] "Even number is : 66" [1] "Even number is : 68" [1] "Even number is : 70" [1] "Even number is : 72" [1] "Even number is : 74" [1] "Even number is : 76" [1] "Even number is : 78" [1] "Even number is : 80" [1] "Even number is : 82" [1] "Even number is : 84" [1] "Even number is : 86" [1] "Even number is : 88" [1] "Even number is : 90" [1] "Even number is : 92" [1] "Even number is : 94" [1] "Even number is : 96" [1] "Even number is : 98" [1] "Even number is : 100" |