In: Computer Science
Write a program with class name ForLoops that uses for loops to perform the following steps:1. Prompt the user to input two positive integers: firstNum and secondNum (firstNum must be smallerthan secondNum).2. Output all the even numbers between firstNum and secondNum inclusive.3. Output the sum of all the even numbers between firstNum and secondNum inclusive.4. Output all the odd numbers between firstNum and secondNum inclusive.5. Output the sum of all the odd numbers between firstNum and secondNum inclusive. Java programming
import java.util.Scanner;
public class ForLoops {
public static void main(String args[]) {
int firstNum,secondNum,odd_sum=0,even_sum=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter two positive integers: ");
firstNum=sc.nextInt();
secondNum=sc.nextInt();
if(firstNum<secondNum)
{
System.out.print("All the even numbers: ");
for(int i=firstNum;i<=secondNum;i++)
{
if(i%2==0)
{
System.out.print(i+" ");
even_sum+=i;
}
}
System.out.println("\nSum of all even numbers: "+even_sum);
System.out.print("All the odd numbers: ");
for(int i=firstNum;i<=secondNum;i++)
{
if(i%2!=0)
{
System.out.print(i+" ");
odd_sum+=i;
}
}
System.out.println("\nSum of all odd numbers: "+odd_sum);
}
else
System.out.println("firstNum is not smaller than secondNum");
}
}