
Uses a while loop to perform the following steps: -Prompt the user to input two integers: firstNum and secondNum where secondNum is at least 10 greater than firstNum, both numbers are positive integers, and secondNum is less than 1000.
-Verify that the user entered acceptable numbers, and if not, provide error feedback and prompt them again.
-Output all results to a file in the same directory as the program, placing an appropriate label between each section of output. Note that your program must be able to run repeatedly overwriting the file from the previous run.
-Output all odd numbers between firstNum and secondNum inclusive, one number per line.
-Output the sum of all numbers between firstNum and secondNum exclusive.
Uses a for loop to perform the following steps:
-Continue writing to the same file as before. -Write a label as before. -Output all numbers from secondNum to firstNum in a single line with commas separating the numbers.
Write the date and time as the last line in the file in the format yyyy-mm-dd hh:mm:ss.

The given code prompt the user to input two integers and validate them. Then. we have calculated the odd numbers between two input values,sum of values and output all numbers from second input number to first one.
import java.io.*;
import java.util.*;
import java.text.*;
class Examples{
public static void main(String[] args) throws Exception{
Scanner input=new Scanner(System.in);
System.out.print("Enter Num1: ");
int firstNum =input.nextInt();
System.out.print("Enter Num2: ");
int secondNum =input.nextInt();
while(firstNum<0){
System.out.print("Number should be positive.!Re-enter: ");
firstNum =input.nextInt();
}
while(secondNum<0){
System.out.print("Number should be positive.!Re-enter: ");
secondNum =input.nextInt();
}
while(secondNum>1000){
System.out.print("Second Number should be less than 1000!Re-enter: ");
secondNum =input.nextInt();
}
while(secondNum<firstNum){
System.out.print("Second Number should be at least 10 greater than firstNum.Re-enter: ");
secondNum =input.nextInt();
}
int sum=0;
File f=new File("c:/numbers.txt");
BufferedWriter bw=new BufferedWriter(new FileWriter(f,true));
bw.write("Odd numbers from "+Integer.toString(firstNum)+" to "+Integer.toString(secondNum));
bw.newLine();
for(int i=firstNum;i<=secondNum;i++){
sum+=i;
if((i%2)!=0){
bw.write(Integer.toString(i));
bw.newLine();
}
}
bw.write("Sum of Numbers: "+sum);
bw.newLine();
bw.write("Output all numbers from secondNum to firstNum:");
bw.newLine();
for(int i=secondNum;i>=firstNum;i--){
bw.write(Integer.toString(i)+",");
}
bw.newLine();
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
bw.write(sdf.format(d));
bw.close();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.