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
Hariharan M 18Hariharan M 18 

solve the below expression by having n value and x value1x+2x+3x+....nx print the answer

Can anyone solve this?
Best Answer chosen by Hariharan M 18
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hariharan,

Can you try the below code. You can call the method as below.

public class SeriesSum {
    public static void sumofseries(Integer n, Integer x){
       Integer sum=0;
        for(Integer i = 1 ; i<=n ;i++){
   sum= sum+i;
        Integer totalvalue= x*sum;
        system.debug('answer is '+totalvalue);


The logic is simple 1x+2x+3x+4x+... nx= x(1+2+3+4+5+..n)

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.