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

# sum Of OddValues from 1 to 10 using loops, can anyone write code ?

Best Answer chosen by Hariharan M 18
CharuDutt
Hii HariHaran
Try Below Code
```public class SumOfOddNumbers {
public static void oddmethod(){
integer SumOdd = 0;
for(integer i=0;i<=10;i++){
if( Math.mod(i,2) != 0){
system.debug(i);
SumOdd+= i;
}
}
system.debug(' SumOdd ==>   '+  SumOdd   );
}
}```
Please Mark It As Best Asnwer If It Helps
Thank You!

Sai Praveen (Salesforce Developers)
Hi Hariharan,

Please find the below code for sum of odd numbers between 1 to 10. You can execute this in anonomous window and get the result in logs.

```Integer sum =0 ;
for(Integer i = 0 ; i<=10 ;i++){
if (Math.mod(i, 2) != 0) {
sum= sum+i;
}

}
System.debug('-->SUm'+sum);```

Let me know if you face any issues.

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

Thanks,
CharuDutt
Hii HariHaran
Try Below Code
```public class SumOfOddNumbers {
public static void oddmethod(){
integer SumOdd = 0;
for(integer i=0;i<=10;i++){
if( Math.mod(i,2) != 0){
system.debug(i);
SumOdd+= i;
}
}
system.debug(' SumOdd ==>   '+  SumOdd   );
}
}```
Please Mark It As Best Asnwer If It Helps
Thank You!
This was selected as the best answer
Hariharan M 18
solve the below expression by having n value and x value1x+2x+3x+....nx
print the answer, can someone answer this?
Mohamed Ahshan

Hi bro,
I can see that you are studying in AJ Skill an year ago

Bro i have a question for you, Are you a Salesforce Developer now?