Menu
Topics Index
...
`

Classify Integer


If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to print the classification for every integer in the given range. E for even, O for odd, OX for odd and divisible by 3, OXY for odd and divisible by 9, OXYZ for odd and divisible by 27. When printing the integer should be prefixed and suffixed with tilda(~).
RULES: Do not use nested if's or Boolean logical AND(&) operator in the program.

Input (Integer Range) Output Printed
20 to 28 ~20~E~21~OX~22~E~23~O~24~E~25~O~26~E~27~OXYZ~28~E
1 to 10 ~1~O~2~E~3~OX~4~E~5~O~6~E~7~O~8~E~9~OXY~10~E
80 to 85 ~80~E~81~OXYZ~82~E~83~O~84~E~85~O



class ClassifyIntegers
{
    public static void main(String s[])
    {
        classifyIntegers(20, 40);

    }


    public static void classifyIntegers(int start, int end)
    {
    }

}
Topic: Java continue Statement

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

--Here we have to display the type of integer within the given range

E-Even
O-Odd
OX-odd and divisible by 3
OX-odd and divisible by 9
OX-odd and divisible by 27

--But as per the mentioned rules we can not use nested if's or Logical operator(&) operator.

--So,Solution is "NESTED SWITCH STATEMENTS".Switch case will be written inside "For loop" which runs from start till end.

--Basic structure of program:

for(int i=start;i<=end;i++)
{
    switch(i%2)
    {
        case 0:
        print E;
        case 1:
        {
            switch(i%27)
            {
                case 0:
                print OXYZ    
            }
            switch(i%9)
            {
                case 0:
                print OXY
            }
            switch(i%3)
            {
                case 0:
                print OX
            }
            print O;
        }
    }
}

--Here, we first chech if number is divisible by 2 i.e. if it is even or not.if not it will go in case 1 that meand number is odd,now we have to check whether it is divisible by 27 ,9 or 3.inside case 1 of outer switch there will be 3
swich blocks for checking the divisibility by 27 , 9 and 3.
If number is not divisible by any of 3 then it will go at the end and prints "O".

--Regarding the "tilda(~)" sign,in every print() statement first no will be printed then tilda(~) and then type(E,O,OX,OY,OXYZ) and at last "tilda(~)".
Before the loop there will be tilda (~) as there is tilda(~) at the starting in the output.
and we do not have tilda(~) at the end,so for that we chech if(i==28) then print() statement will not be having tilda(~) at the end.

--In this way by using "NESTED SWITCH BLOCK" we can achieve given output.

Posted by Mânïshå Mùlchåndânï    2015-02-06 20:03:47


This dose is now closed and the winners are . The 'lucky liker' is Shubham Bansal. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-09 05:21:19


© meritcampus 2019

All Rights Reserved.

Open In App