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
Prajwal B 1Prajwal B 1 

code to print the factorial of a number

VinayVinay (Salesforce Developers) 
Hi Prajwal,

Check below example for facorial of number.
trigger fact on Object__c (before Insert, before Update) {
    for(Object__c  m : Trigger.new)
    {
       if(trigger.isInsert || trigger.isUpdate)
        {
   
   long fac=1;

   for(integer j = 1; j<= m.Factorial__c; j++) {
      fac = fac * j;
   }
   
   m.Output_of_the_Factorial__c = 'Factorial of ' + m.Factorial__c + ' = ' + fac;

    }
  }
}

https://salesforceforfresher.wordpress.com/2021/06/13/factorial-salesforce-apex-trigger/

Please mark as Best Answer if above information was helpful.

Thanks,
Arun Kumar 1141Arun Kumar 1141
Hello Prajwal,

You can use this sample 
public class FindFactorial {
    public static void fact(Integer num){

        Long answer = 0;
        while(num != 0){
            answer = answer * num;
            num--;
        }

        system.debug(answer);
    }
}

Please mark it as best answer if it helps you.

Thanks.