  bros.sfdc

how to find that number is even/odd or prime number or armstrong or a palindrome.

Hi,

I am new to salesforce.pl anyone can help me how to implement trigger for the following

When a number is inserted, how to find that number is even/odd or prime number or armstrong or a palindrome. ngabrani

Programming for these type of tasks is similar in all languages. The operators supported by Apex are availabler here -

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_expressions_operators_understanding.htm

As an example for even/odd, you could do the following. Lets say x is the original number.

y = x/2;

z = y * 2;

If(z == x) { System.Debug('Even'); }

else System.Debug('Odd');

As an example if x is 7, y will be set to 3 and z will be set to 6. As z will not be equal to x, Odd will be printed. Jeff May

Welcome to Salesforce!!

To get familiar with the Apex programming language, start with the easy one --even/odd.  Here is a link to the Apex Dev Guide (http://www.salesforce.com/us/developer/docs/apexcode/index.htm).  Google will also be your best friend.  Seach for 'salesforce apex <whatever>".  And, of course, we are here to help you over the rough spots. Beer Nutthawan
Try this
Integer i = 5;
if(math.mod(i, 2) == 0){
system.debug('this is even: ' + i);
}else{
system.debug('this is odd: ' + i);
} Tushar Tyagi 45
// Let say this trigger is for an account object. And on account object, there is a field called number__c.

trigger WhatNumber on Account(before insert)
{

for(Account acc:trigger.new)
{
string PallindromeNo=checkPallindrome(acc.number__c);
system.debug('No is '+PallindromeNo);
if(acc.number__c/2==0)
{
system.debug('EVEN NUMBER');
}

else
{
system.debug('ODD NUMBER');
}
}

public string checkPallindrome(integer x)
{

integer reversedInteger = 0, remainder, originalInteger;
// reversed integer is stored in variable
while( x!=0 )
{
remainder = x%10;
reversedInteger = reversedInteger*10 + remainder;
x /= 10;
}

// palindrome if orignalInteger and reversedInteger are equal
if (originalInteger == reversedInteger)
{
return 'Pallindrome';
}
else
{
return 'NOTpallindrome';
}

}
} Midles Smith
for me for me, this thread solution worked  https://salesforce.stackexchange.com/questions/164931/is-there-a-way-to-call-javascript-after-action-on-visualforce-page Deepali Kulshrestha
Hi,

public class CheckNum {
public static void func(Integer num){
if(math.mod(num,2)==0){
System.debug('num is Even');
}else if(math.mod(num,2)!=0){
System.debug('num is Odd');
}
//Checking if the number is an armstrong number or not
Integer originalnum, remainder;
Double result = 0, n = 0 ;
originalnum = num;

while (originalnum != 0)
{
originalnum /= 10;
++n;
}
originalnum = num;
while (originalnum != 0)
{
remainder = math.mod(originalnum,10);
result += math.pow(remainder, n);
originalnum /= 10;
}
if(result == num)
System.debug(num+' is an Armstrong num');
else
System.debug(num+' is not an Armstrong num');
String str1='';
String stringNum = String.valueOf(num);
str1 = stringNum.reverse();
if(str1==num)
System.debug('Number is pallindrome');
}
}

For trigger replace 'num' with the field which you want to check or the number which you are inserting in the database.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com Ankit Wadibhasme
Hi developer, To try This code

Integer x = 10;
if(math.mod(x, 2) == 0){
system.debug('this is even number: ' + x);
}
else
{
system.debug('this is odd number: ' + x);
}