Write a program in JAVA which: Asks the user to enter a positive integer greater
ID: 3811453 • Letter: W
Question
Write a program in JAVA which:
Asks the user to enter a positive integer greater than 0
Validates that the entry is a positive integer
Outputs the digits in reverse order with a space separating the digits
Outputs the even digits not in reverse order with a space separating the digits (consider zero to be even)
Outputs the odd digits not in reverse order with a space separating the digits
Allows user is to repeat/continue the program as many times as he/she wants
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
-----------------
SPECIFIC REQUIREMENTS
The program must have the following four void methods:
validate //validate user input
reverse // output reverse digits to screen and txt file
even //output even digits to screen and txt file
odd //output odd digits to screen and txt file
Explanation / Answer
JAVA CODE :
import java.util.*;
import java.lang.*;
import java.io.*;
class Prog1
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter a number: ");
int n = reader.nextInt(); // Scans the next token of the input as an int.
List<Integer> e = new ArrayList<Integer>();
List<Integer> o = new ArrayList<Integer>();
//validating whether input is postive or not
if(n>0){
//printing the digits in reverse order
int temp=n;
System.out.println("The digits in reverse order ");
while(temp!=0){
int x=temp%10;
temp=temp/10;
if(x%2==0){
e.add(x);
}
else{
o.add(x);
}
System.out.print(x+" ");
}
System.out.println();
System.out.println("EVEN DIGITS");
for(int i=0;i<e.size();i++){
System.out.print(e.get(i)+" ");
}
System.out.println();
System.out.println("ODD DIGITS");
for(int i=0;i<o.size();i++){
System.out.print(o.get(i)+" ");
}
}
else
{
System.out.println("THE INPUT IS NOT POSITIVE...PLEase try again");
}
}
}
output :
Enter a number: 102030
The digits in reverse order
0 3 0 2 0 1
EVEN DIGITS
0 0 2 0
ODD DIGITS
3 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.