Write a java program which: 1. Asks the user to enter a positive integer greater
ID: 3809802 • Letter: W
Question
Write a java program which:
1. Asks the user to enter a positive integer greater than 0
2. Validates that the entry is a positive integer
3. Outputs the digits in reverse order with a space separating the digits
4. Outputs the even digits not in reverse order with a space separating the digits (consider zero to be even)
5. Outputs the odd digits not in reverse order with a space separating the digits
6. Allows user is to repeat/continue the program as many times as he/she wants
7. Keeps a record in a txt file named outDataFile.txt with the history of all numbers entered and the associated results,
in the following format:
the original number is 1023
the number reversed 3 2 0 1
the even digits are 0 2
the odd digits are 1 3
-----------------
the original number is 102030
the number reversed 0 3 0 2 0 1
the even digits are 0 2 0 0
the odd digits are 1 3
-----------------
Explanation / Answer
Program
import java.util.Scanner;
public class Numbers {
public static void main(String arg[]){
Scanner sca=new Scanner(System.in);
int n,num,i,j;
System.out.print("Enter number : ");
n=sca.nextInt();
num=n;
int even[]=new int[10];
int odd[]=new int[10];
System.out.println("The original number is "+n);
System.out.print("The number reversed ");
int x;
i=0;
j=0;
while(num>0){
x=num%10;
if(x%2==0)
even[i++]=x;
else
odd[j++]=x;
System.out.print(x+" ");
num/=10;
}
System.out.print(" The even digits are ");
for(int k=i-1;k>=0;k--)
System.out.print(even[k]+" ");
System.out.print(" The even digits are ");
for(int k=j-1;k>=0;k--)
System.out.print(odd[k]+" ");
}
}
Result:
Enter number : 1023
The original number is 1023
The number reversed 3 2 0 1
The even digits are 0 2
The even digits are 1 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.