AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Reverse Jalaj number

Jalaj was deeply studying in the afternoon and came across a concept called as reverse of a number

Hearing this for the first time, he was eager to learn and searched the Internet and decided to design a program, which can reverse any number. But, he could not convert his logic into code.

So, he is now requesting you to design a very efficient method to reverse a number and print it as a EVEN or ODD, as the case may be.

Input

The first line contains T, the number of test cases. Followed by T lines each contains a single number.

Output:

For every test case print the EVEN if reverse of the given number is even, else print ODD.

Constraints:

1<=T<=10000
1<=N<=10 raised to the power 100

Sample Input
5
1
3
6
15
23

Sample Output
ODD 1
ODD 3
EVEN 6
ODD 15
EVEN 23

corejava x 351
programming x 167
Posted On : 2016-11-01 13:05:03.0
profile Akash Prasad - anyforum.in Akash Prasad
2550
up-rate
4
down-rate

Answers


Check this code and let me know if the conditions are satisfied or not.

import java.util.Scanner;
public class RevEvenOdd
{
public static void main(String[] args)
{
int t;
int temp = 0;
Scanner s = new Scanner(System.in);
t = s.nextInt();
int[] a = new int[t];
if(t>=1 && t<=10000)
{
for (int i = 0; i < t; i++)
{
a[i] = s.nextInt();
}
for(int i=0; i<a.length;i++)
{
temp = 0;
while(a[i]>0)
{
temp = temp*10 + a[i] %10;
a[i] = a[i]/10;
}
a[i] = temp;
}
for(int i = 0 ; i < t ; i++)
{
if(a[i] % 2 != 0)
{
temp = 0;
while(a[i]>0)
{
temp = temp*10 + a[i] %10;
a[i] = a[i]/10;
}
a[i] = temp;

System.out.println("ODD :" +a[i]);
}
else if(a[i] % 2 == 0)
{
temp = 0;
while(a[i]>0)
{
temp = temp*10 + a[i] %10;
a[i] = a[i]/10;
}
a[i] = temp;
System.out.println("EVEN :"+a[i]);
}
}
}
}
}


Posted On : 2016-11-05 17:12:08
Satisfied : 0 Yes  3 No
profile Sudheer Kumar Javvadi - anyforum.in Sudheer Kumar Javvadi
0960
Reply This Thread
up-rate
5
down-rate

import java.util.Scanner;

public class ReverseOddEven {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int T=input.nextInt();
Integer N=0;
Integer R=0;
if(1<=T && T<=10000 ){
System.out.println("");
for(int i=1;i<=T;i++){
try{
N=input.nextInt();
if(1<=N && N<=Math.pow(100, 10)){
if(N.toString().length()>1){
R=Integer.parseInt(new StringBuffer(N.toString()).reverse().toString());
}else{
R=N;
}
System.out.println((R%2==0?"EVEN":"ODD")+ " "+N);
}else{
System.out.println("Invalid Input!");
break;
}
}catch (Exception e) {
e.printStackTrace();
break;
}
}
}else{
System.out.println("Invalid Input!");
}
}
}


Note:
---------------------------
While giving input press enter or one more new line after last number as below:
Input:
----------------------------------
5
1
3
6
15
23

Output:
--------------------------
ODD 1
ODD 3
EVEN 6
ODD 15
EVEN 23

Posted On : 2016-11-09 23:29:57
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523187021168
Reply This Thread
up-rate
4
down-rate

Check this updated code , As per mentioned Sample input and output i think my code satisfies the condition.Let me know if the conditions are satisfied or not.

import java.util.Scanner;
public class RevEvenOdd
{
public static void main(String[] args)
{
int t;
int temp = 0;
Scanner s = new Scanner(System.in);
t = s.nextInt();
int[] a = new int[t];
if(t>=1 && t<=10000)
{
for (int i = 0; i < t; i++)
{
a[i]=s.nextInt();
}
for (int i = 0; i < a.length; i++)
{
if(a[i]>=1&&a[i]<=Math.pow(10,100))
{
temp = 0;
while(a[i]>0)
{
temp = temp*10 + a[i] %10;
a[i] = a[i]/10;
}
a[i] = temp;
}
}
for(int i = 0 ; i < a.length ; i++)
{
if(a[i] % 2 != 0)
{
if(a[i]>=1&&a[i]<=Math.pow(10,100))
{
temp = 0;
while(a[i]>0)
{
temp = temp*10 + a[i] %10;
a[i] = a[i]/10;
}
a[i] = temp;
System.out.println("ODD :" +a[i]);
}
else
{
System.out.println("ODD :" +a[i]);
}
}
else if(a[i] % 2 == 0)
{
if(a[i]>=1&&a[i]<=Math.pow(10,100))
{
temp = 0;
while(a[i]>0)
{
temp = temp*10 + a[i] %10;
a[i] = a[i]/10;
}
a[i] = temp;
System.out.println("EVEN :"+a[i]);
}
else
{
System.out.println("EVEN :" +a[i]);
}
}
}
}
}
}


My code shows exactly mentioned in the as in the sample input and output.

Sample Input
5
1
3
6
15
23

Sample Output
ODD 1
ODD 3
EVEN 6
ODD 15
EVEN 23

Posted On : 2016-11-10 20:11:22
Satisfied : 1 Yes  0 No
profile Sudheer Kumar Javvadi - anyforum.in Sudheer Kumar Javvadi
0960
Reply This Thread
up-rate
3
down-rate
Comments
Hi sudheerkumar,
What´s the problem in my code? Isn´t it shorter and easy to understand?
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1870  21168
Posted On :2016-11-10 21:46:13.0
Leave a Comment
No Issues in your code.As per my understanding first we need to take all elements from console and print the all elements at a time once the condition satifies.

But in your code each element takes from console and print them.
Find the below output details from your code and my code.

Check my code and let your comments on it.
My code takes all elements at time and print them all elements:

profile Sudheer Kumar Javvadi - anyforum.in Sudheer Kumar Javvadi
0  96  0
Posted On :2016-11-11 20:31:07.0
Leave a Comment
All right. My code also take all elements at a time. After running the code, just paste all inputs and press Enter. It´ll display the desired output.
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1870  21168
Posted On :2016-11-12 14:50:12.0
Leave a Comment

package in.anyforum;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ReverseNumOddEven {
public static void main(String[] args) {
System.out.println("Enter number of input lines: ");
Scanner in = new Scanner(System.in);
int T = in.nextInt();

System.out.println("Enter "+T+" numbers to check:");
List<String> list = new ArrayList<String>(T);
for(int count=T;count>0;count--){
list.add(check((Integer)in.nextInt()));
}

for(String s : list)
System.out.println(s);
in.close();
}

static String check(Integer input) {
Integer output = Integer.parseInt(new StringBuilder(input.toString()).reverse().toString());

String outputString = null;
if(output>0 && output %2 == 0){
outputString = "EVEN "+input.toString();
} else if(output>0) {
outputString = "ODD "+input.toString();
} else {
outputString = "Invalid input";
}
return outputString;
}
}

Posted On : 2016-12-30 16:45:50
Satisfied : 0 Yes  0 No
profile Rishi Raj - anyforum.in Rishi Raj
040
Reply This Thread
up-rate
0
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in