Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
Manogna Kovi

# Write an apex program to print prime numbers

Hi Manogna,

Check once below links :

https://developer.salesforce.com/forums/?id=906F00000005J7fIAE

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks

Salesforce latest interview questions  :

```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');
}
}
}

}```

Malika Pathak 9

Hi Manogna,

```public class FindPrimeNumber{
public void getPrime(){
integer x=0;
for(x=0;x<10;x++)
{
integer count=0;
integer y=0;
for( y=x; y>=1;y--)
{

if(math.mod(x,y)==0 )
{
count++;
}
}
if(count==2 || count==1){

system.debug(x);

}

else{
system.debug('This is not prime number ');
}
}
}

}```

if you find this helpful mark it as the best answer.
Neeraj Agrawal 17
Hi, Please check below to check whether number is prime
public class PrimeNumber {

static integer remainder;
static boolean isPrime=true;

public static void func(Integer num){

for(integer i=2;i<=num/2;i++)
{
remainder=math.mod(num,i);
System.debug(num+ 'Divided by'+ i + 'gives a remainder'+remainder);

//if remainder is 0 than numberToCheckber is not prime and break loop. Elese continue loop
if(remainder==0)
{
isPrime=false;
break;
}
}

if(isPrime)
{
System.debug(num + 'is a Prime numberToCheckber');
}
else
{
System.debug(num + 'is not a Prime numberToCheckber');
}
}
}
Ankit Gupta SFDCLearner
Hi Use below apex code and VF page for test.
Apex class

public class calculation {
Public integer myInputQueryString {
get;
set;
}
Public string myoutputString {
get;
set;
}
string val = '';
Public void myInputQuery() {
if (myInputQueryString != null) {
system.debug('not null');

for (integer i = 0; i < myInputQueryString; i++) {

if (math.mod(i, 2) == 0) {
system.debug('this is even number: ' + i);

val = val + string.valueOf(i) + ', ';
myoutputString = val;

}

}
myInputQueryString = 0;
}
}

Public void myOddNumbers() {
if (myInputQueryString != null) {
system.debug('not null');

for (integer i = 0; i < myInputQueryString; i++) {

if (math.mod(i, 2) != 0) {
system.debug('this is even number: ' + i);

val = val + string.valueOf(i) + ', ';
myoutputString = val;

}

}
myInputQueryString = 0;
}
}
Public void myPrimeNumbers() {

if (myInputQueryString != null) {
system.debug('not null');

for (integer i = 0; i < myInputQueryString; 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);
val = val + string.valueOf(i) + ', ';
myoutputString = val;

}
}
}

}
}

VF Page
<apex:page controller="calculation" id="vfPageId">
<apex:form id="formId">
<script>
function CheckNull() {
debugger;
var dd = document.getElementById("vfPageId:formId:inputnum1").value;
if (dd == '') {
}
}
</script> Input Number:
<apex:inputText value="{!myInputQueryString}" id="inputnum1" />
<apex:pageblock id="evenPBId">
<apex:commandButton value="Even Numbers" reRender="DisplayEvenID" action="{!myInputQuery}" onClick="CheckNull();" />
<b>Even Numbers are: </b>
<apex:outputText value="{!myoutputString}" id="DisplayEvenID" />
<br />
<apex:commandButton value="Odd Numbers" reRender="DisplayOddID" action="{!myOddNumbers}" onClick="CheckNull();" />
<b>Odd Numbers are: </b>
<apex:outputText value="{!myoutputString}" id="DisplayOddID" />
<br />
<apex:commandButton value="Prime Numbers" reRender="DisplayPrimeID" action="{!myPrimeNumbers}" onClick="CheckNull();" />
<b>Prime Numbers are: </b>
<apex:outputText value="{!myoutputString}" id="DisplayPrimeID" />
</apex:pageblock>
</apex:form>
</apex:page>
Shruthi MN 88
@Neeraj Agrawal 17

why have you used n/2 when we can do without that