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
TUSHAR KHORGADETUSHAR KHORGADE 

hello all i am getting error pls coorect me if i am worng i want to print even n odd number with there count with help of using map

Create a map populate it with two records Odd and Even. Run a loop from 1-50, if you find a even number increase the count for even named record in map or else increase count of odd named record. Odd and even are keys, counts are values. Example map syntax: Map<String,integer> CountNumber = new map<String,Inetger>({'Even':4},{'Odd':3});

my code is this

public class NumberTest {
    integer oddCount = 0;
    integer evenCount = 0;
    
    Map<string,integer> countNumber = new Map<string,integer>();
    
    for(int i=1;i<=50;i++)
    {
        if(a[i] % 2 != 0){
            countNumber.put('Odd',oddCount++);
        }
        else{
            countNumber.put('Even',evenCount++);
        }
    }
}
Best Answer chosen by TUSHAR KHORGADE
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Tushar,

I see two main mistakes in your code. 

1) Int data type is not  valid in APEX. Try using integer.
2) % operator is not valid in Apex. Try using the below line.
 
math.mod(1,2) != 0

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

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Tushar,

I see two main mistakes in your code. 

1) Int data type is not  valid in APEX. Try using integer.
2) % operator is not valid in Apex. Try using the below line.
 
math.mod(1,2) != 0

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

Thanks,
 
This was selected as the best answer
TUSHAR KHORGADETUSHAR KHORGADE
thank you Sai Praveen For guiding me ................