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
Vikram Singh 157Vikram Singh 157 

Hi ! is there any issue with code

public class Prime {
    Integer i,num;
    public void entnum(Integer n){
        num = n;
        for (i=2;   i<=num; i++){
            if (system.math.mod(num,i) == 0){
                System.debug('Number is not Prime');
                break;
            }
                if(i == num) {
                  system.debug('Number is  Prime');  
               
                } 
            }
        }
       }
Best Answer chosen by Vikram Singh 157
AvaneeshAvaneesh
Hii Vikram
I fixed your code 
public class primefunct {
 Integer i,num;
    public void entnum(Integer n){        
       Integer count = 0;
        
            for (i=2;i<=n; i++)
              {
                if (math.mod(n,i) == 0)
                    count++;  
              } 
        if(count == 1)
            system.debug('prime');
          else
              system.debug('not prime');
            
   }
}
cheers ..................enjoy
Thank you 
Avaneesh Singh

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Vikram,

Please refer the below code for reference.
public class PrimeNumbers {
    public void prime(){
    for(integer i=1;i<=100;i++)
    {
        integer count=0;
        for(integer j=i; j>=1;j--)
        {
            
            if(math.mod(i,j)==0 )
            {
                count++;
            }
        }
        if(count==2 || count==1){
            
                         system.debug(i);
                   
                }                     
                 
            else{
               //system.debug('not prime');      
                }
    }
  }

}
Please mark it as Best Answer if the information is informative.

Thanks
Rahul Kumar
 
AvaneeshAvaneesh
Hii Vikram
I fixed your code 
public class primefunct {
 Integer i,num;
    public void entnum(Integer n){        
       Integer count = 0;
        
            for (i=2;i<=n; i++)
              {
                if (math.mod(n,i) == 0)
                    count++;  
              } 
        if(count == 1)
            system.debug('prime');
          else
              system.debug('not prime');
            
   }
}
cheers ..................enjoy
Thank you 
Avaneesh Singh
This was selected as the best answer