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
likitha veldandilikitha veldandi 

how to print the odd numbers between 1-50 in reverse order using while

Abdul KhatriAbdul Khatri
Class
public class MathUtil {
    public static Boolean isOddNumber(Integer num) {
        return math.mod(num, 2) != 0;
    }
}
Test Class
@isTest
public class MathUtil_Test {

    testmethod static void test_odd_number(){
        
        system.assert(MathUtil.IsOddNumber(3) == true);
        system.assert(MathUtil.IsOddNumber(2) == false);
    }
}


You can run the below code for testing in Anonymous Window
Integer i = 50;
While (i > 0) {
    if(MathUtil.isOddNumber (i))
        system.debug(i);  
    i--;
}

 
likitha veldandilikitha veldandi
I would like to know how to execute by using math.mod?
Abdul KhatriAbdul Khatri
Was this helpful?

Please mark the answer best if it helped.
Danish HodaDanish Hoda
Hi Likitha,
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.
Abdul KhatriAbdul Khatri
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.
 
-- >> (50, 49, 48, 47, 46, 45, 44, 43, 42, 41, ...)

 
likitha veldandilikitha veldandi
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.
Maharajan CMaharajan C
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   );



Thanks,
Maharajan.C
Maharajan CMaharajan C
Output will be like below:

Odd Numbers ==> (49, 47, 45, 43, 41, 39, 37, 35, 33, 31, ...)
Deepali KulshresthaDeepali Kulshrestha
Hi likitha,

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
SATISH SURYAWANSHISATISH SURYAWANSHI
Hi likitha,
Please refer my code,
 
for(Integer i=50;i>=0;i--)
{
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