By running the Danish Logic I see the following result so not sure what actually you were looking for becuase his result include both odd and even numbers. Have you tried running the logic and make sure it suits your requirement. Thanks.
Yeah i tried it Abdul Khatri,even i noticed that the result included both odd and even numbers.Iam trying to execute the program with basic concepts using WHILE LOOP and IF condition.Thanks Danish Hoda and Abdul Khatri.
In below we are using the while loop and If condition only. Additionally we are using the Math class to find the remainder because apex will not support the Mod operator(%) like other languages.
Integer i = 50; list<integer> oddList = new list<integer>(); While (i > 0) { if( Math.mod(i,2) != 0) { system.debug(i); oddList.add(i); } i--; } system.debug(' Odd Numbers ==> '+ oddList );
You can run the below code for testing in Anonymous Window
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_math.htm
Please mark the answer best if it helped.
You can use this:
integer i=51;
list<integer> lst = new list<integer>();
while(i>0){
integer rem = math.mod((i-1), i);
lst.add(rem);
i--;
}
system.debug('-- >> '+lst);
Please mark the answer Best if it suits your reuirement.
Integer i = 50;
list<integer> oddList = new list<integer>();
While (i > 0) {
if( Math.mod(i,2) != 0)
{
system.debug(i);
oddList.add(i);
}
i--;
}
system.debug(' Odd Numbers ==> '+ oddList );
Thanks,
Maharajan.C
Odd Numbers ==> (49, 47, 45, 43, 41, 39, 37, 35, 33, 31, ...)
Please refer to my code below:
public class QUES {
public static void addOddNum(){
Integer n =50;
Integer sum=0;
while(n>0){
if(math.mod(n,2)==1){
System.debug(n);
}
n=n-1;
}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Please refer my code,
{
if (0!=math.mod(i, 2))
{
System.debug('Display the odd numbers '+ i);
}
}
I have tested this code in my Anonymous Window. You can run the below code for testing in Anonymous Window.
Please mark the answer Best if it suits your reuirement.
Thanks and Regards,
Satish Suryawanshi