Question

In: Computer Science

Write a program with class name ForLoops that uses for loops to perform the following steps:1....

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

Solutions

Expert Solution

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");
}
}


Related Solutions

Description: Write and test a MASM program to perform the following tasks: 1. Display your name...
Description: Write and test a MASM program to perform the following tasks: 1. Display your name and program title on the output screen. 2. Display instructions for the user. 3. Prompt the user to enter two numbers. 4. Calculate the sum, difference, product, (integer) quotient and remainder of the numbers. 5. Display a terminating message. Requirements: 1. The main procedure must be divided into sections:  introduction  get the data  calculate the required values  display the results...
Write a C program that loops and asks you every time to enter the name of...
Write a C program that loops and asks you every time to enter the name of a text file name, then it will read the file and perform statistics on its contents and display the results to the screen and to an output file called out.txt. The input files can be any text file. The program should calculate these integers as it reads character by character as we did in the last class example filecopy and make use of the...
Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
IN C++ Write a program that uses nested loops to collect data and calculate the average...
IN C++ Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the ­­program should display the...
write a program that uses exactly four for loops to print the sequence of letters below...
write a program that uses exactly four for loops to print the sequence of letters below A A A A A A A A A A B B B B B B B C C C C C C E E E E E
Write a C++ program that uses nested loops to collect data and calculate the average rainfall...
Write a C++ program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number...
Write a program to perform the following two tasks: 1. The program will accept a string...
Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string. 2. Then...
Write a program to perform the following two tasks: 1. The program will accept a string...
Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string. 2. Then...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following method: • public String getName() Gets the name string. • public int consonants()...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following methods. • public String getName() Gets the name string. • public int consonants()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT