function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SaifSaif 

I would like to Generate Even and Odd Numbers below 10?

I would like to Generate Even and Odd Numbers below 10 based on input like if I set input as 2 it should return only Even Numbers2,4,6,8,10 if I set input 1 it should return 1,3,5,7,9 Here the code is I am unable to out of this error please help me out.
public class evenoddex {
   public integer n=10; 
   public integer a;
   public void getInput(integer x){
        a=x;
       system.debug('Your Input=='+a);
    }
   public void displayEvenOdd(){
        if(a<0){
            system.debug('Please enter postive Numbers');
        }
       else{
           if(a<n){
               for(integer i=0; i<n; i=i+2){
                   system.debug('Numbers=='+a[i]);
               }
           }
           else{
                  system.debug('Please enter Below 10');
           }
       }
    } 
}

Calling Class
-----------------------
evenoddex s=new evenoddex();
s.getInput(1);
s.displayEvenOdd();

Thanks,
 
ajay Duggi(Heptarc)ajay Duggi(Heptarc)
Dear Saif,

Try to use MOD function and divide number by 2 and check if the reminder is 0 or 1, if it is 0 take as an even number and if it is 1 take as an odd number.
public class communityevenodd {
    public Integer i {get;set;}
    public void print(){
        
        for(i=0;i<=10;i++){
        
     if(math.mod(i, 2) == 0){
    system.debug('this is even: ' + i);
     } else {
    system.debug('this is odd: ' + i);
        }
        }      
     }
  }
communityevenodd var=new communityevenodd();(Log executeAnonymous) 
var.print();
Please let me know if it is helpful to you.
Rohit Kumar SainiRohit Kumar Saini
Hi Saif,

There are other ways too to do it depending on what is the real use of this class/logic. I updated 2 lines in your code (for loop) and it should work now. Below is full class code.
public class evenoddex {
   public integer n=10; 
   public integer a;
   public void getInput(integer x){
        a=x;
       system.debug('Your Input=='+a);
    }
   public void displayEvenOdd(){
        if(a<0){
            system.debug('Please enter postive Numbers');
        }
       else{
           if(a<n){
               for(integer i=a; i<=n; i=i+2){
                   system.debug('Numbers=='+i);
               }
           }
           else{
                  system.debug('Please enter Below 10');
           }
       }
    } 
}

Highlighted changed lines. Please let me know if you have any questions. Thanks.

 
Rakesh Tiwari 3Rakesh Tiwari 3
Hi Please Try Below Code to Print Even / Odd  Numbers In Between 0 to 10.

To Print Even Number :

Integer i= 0; 
for(i=0; i<=100; i++){
Integer J = i/2;
Integer K = J*2;
if (K==i)
system.debug(+i); 
        
 }

To Print odd Number :

Integer i= 0; 
for(i=0; i<=10; i++){
Integer J = i/2;
Integer K = J*2;
if (K!=i)
system.debug(+i); 
        
 }