+ Start a Discussion
sudheer kakusudheer kaku 

how to write apex code for displaying numbers from 1 to 100 numbers which are divisible by 2

Suraj Tripathi 47Suraj Tripathi 47

Hi Sudheer,

Please try the following code : 

for(Integer i=1;i<=100;i++){
    Integer result = math.mod(i,2);
    if(result == 0){
        System.debug(i);
    }
}


Please mark it as the best answer if it resolves your issue.

Thanks.

ravi soniravi soni
hi sudheer,
by using Mod function, we can achieve it.
lool below apex code.
for(Integer i=1; i<=100 ; i++){
   Integer count= math.mod(i, 2); 
/*this line will return 0 Or 1. if we are getting number which can be divided by 2 then return 0 or else 1*/
    if(count == 0){
       System.debug(i+' is divisible by 2 '); 
    }
}
let me know by marking it as best answer  if it helps you.
Thank you
 
CharuDuttCharuDutt
Hii Sudheer
Try Below Code
for(Integer i=1;i<=100;i++){
    Integer result = math.mod(i,2);
    if(result == 0){
         System.debug(i+' is divisible by 2 '); 
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
ravi soniravi soni
hi sudheer,
did you try my solution. please let us know by marking it as best answer.
Thank you
Jennifer Barnes 1Jennifer Barnes 1
I think that you have not specified the path itself. The code is not such a simple thing as it might seem at first glance. I started learning Java Script a year ago and realized how much I lack mathematical knowledge. I used to always use https://plainmath.net/post-secondary to solve and find an answer to any college math problems and it helped me a lot. But I never wanted to study on my own, because I spared time and was lazy. Now I realized that mathematics is very closely related to many other sciences and specialties.