# How to find numbers that are divisible by a certain number for a range of values in R?

R ProgrammingServer Side ProgrammingProgramming

#### Artificial Intelligence : The Future Of Programming

15 Lectures 54 mins

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

In R, the divisibility of a number by a certain number can be found by using the modulus operator %%. If we want to check the divisibility of a set of numbers by a certain number then for loop will be used.

Check out the below given examples to understand how it can be done.

## Example 1

To check which numbers between 1 and 100 are divisible by 10, use the following code −

for(x in 1:100){
+ if(x%%10==0){
+ print(x)
+ }
+ }

If you execute the above given code, it generates the following output −

  10
  20
  30
  40
  50
  60
  70
  80
  90
 100

## Example 2

To check which numbers between 1 and 1000 are divisible by 50, use the code given below −

for(x in 1:1000){
+ if(x%%50==0){
+ print(x)
+ }
+ }

If you execute the above given code, it generates the following output −

   50
  100
  150
  200
  250
  300
  350
  400
  450
  500
  550
  600
  650
  700
  750
  800
  850
  900
  950
 1000

## Example 3

To check which numbers between 1 and 100 are divisible by 4, use the code given below −

for(x in 1:100){
+ if(x%%4==0){
+ print(x)
+ }
+ }

If you execute the above given code, it generates the following output −

   4
   8
  12
  16
  20
  24
  28
  32
  36
  40
  44
  48
  52
  56
  60
  64
  68
  72
  76
  80
  84
  88
  92
  96
 100