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 

create list on numbers

can anyone solve the below questions?
create list on numbers
-> 1.find biggest number in that list
-> 2.find sum of odd and even numbers in the list (seperately)
-> 3.find any negative numbers in the list
-> 4. count number of negative numbers in the list and print the count
-> 5.reverse a list and store in a new list, print the new list
Best Answer chosen by Hariharan M 18
PriyaPriya (Salesforce Developers) 
Hey Hariharan,

1. To find biggest number in that list
List<Decimal> samplevalues = new List<Decimal>();
samplevalues.add(55.0);
samplevalues.add(75.5);
samplevalues.add(99.3);
Decimal maxvalue = samplevalues[0];
        For (integer i =0;i<samplevalues.size();i++)
        {
            
            if( samplevalues[i] > maxvalue)
                maxvalue = samplevalues[i];             
        }    
system.debug('the max value is'+maxvalue);
2. sample logic to caluclate the sum of odd number in collection 
List<Integer> intList = new List<Integer>() ;
intList.add(10);
intList.add(20);
intList.add(30);
intList.add(40);
intList.add(50);
Integer sum =0 ; 
for(Integer i = 0 ; i<intList.size() ;i++){
   if (Math.mod(i, 2) != 0) {
   sum= sum+intList[i];
   }
    
}

System.debug('-->SUm'+sum);
3. to count the number of negative number, take the reference from here :-
https://stackoverflow.com/questions/65123750/how-to-count-all-the-negative-numbers-in-a-list

5. To reverse the list and store in new list :-
List<Object> someList = new List<Object>{1,2,3,4,5};
List<Object> reversed = new List<Object>();

for(Integer i = someList.size() - 1; i >= 0; i--){
    reversed.add(somelist[i]);
}
 

Kindly mark it as the best answer if it works for you.

 

Thanks & Regards,

Priya Ranjan