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
pallipalli 

Even numbers printing 1 to 50 in developer console

hi,

 

how print even numbers between 1 to 50 in developer console class......

 

i write a code here ...........

 

An error is generate ... line9:18 no aviable alternaive at character '%'...

 

 

class

================

public class ListEvenNumbers
{
public void evenMethod()
{
Integer limit=50;
System.debug('Printing even numbers between 1 and '+limit);
for(Integer i=1;i<=limit;i++)
{
if(i%2==0)
{
System.debug('Display the even numbers'+i);
}
}
}
}

 

 

please help me

Best Answer chosen by Admin (Salesforce Developers) 
PrakashbPrakashb

Limit is a salesforce keyword.

 

Replace limit with some other variable.

All Answers

bob_buzzardbob_buzzard

You don't have the '%' operator for mod in Apex, you have to use Math.mod,  e.g.

 

if (0==math.mod(i, 2))
{
   ...
}

 

pallipalli

hi now here an error ,

expecting a semo colon,found "limit"

 

Integer limit=50;



 

please help me

PrakashbPrakashb

You might have missed some semi colon or braces issue. Can you post your code??

 

pallipalli

public class ListEvenNumbers
{
public void evenMethod()
{
integer limit=50;

System.debug('Printing even numbers between 1 and '+limit);

for(Integer i=1;i<=limit;i++)
{
if (0==math.mod(i, 2))
{
System.debug('Display the even numbers'+i);
}
}
}
}

PrakashbPrakashb

Limit is a salesforce keyword.

 

Replace limit with some other variable.

This was selected as the best answer
pallipalli

tHANK U VERY MUCH............

farha amreenfarha amreen
please provide me the apex code of above program...urgent