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

C++. Write a program that uses for loops to perform the following steps: a. Prompt the...
C++. Write a program that uses for loops to perform the following steps: a. Prompt the user to input two positive integers. variables: firstNum and secondNum (firstNum must be less than secondNum). Validate the user's input; prompt the user again if firstNum is not less than secondNum (use for loop). b. Output all odd numbers between firstNum and secondNum. (use for loop). c. Output the sum of all even numbers between firstNum and secondNum. (use for loop). d. Output the...
Write a program that loops, prompting the user for their full name, their exam result (an...
Write a program that loops, prompting the user for their full name, their exam result (an integer between 1 and 100), and then writes that data out to file called ‘customers.txt’. The program should check inputs for validity according to the following rules: First and last names must use only alphabetical characters. No spaces, hyphens or special characters. Names must be less than 20 characters long. Exam result (an integer between 1 and 100 inclusive) The file should record each...
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...
Program Description Write and test a MASM program to perform the following tasks: Display your name...
Program Description Write and test a MASM program to perform the following tasks: Display your name and program title on the output screen. Display instructions for the user. Prompt the user to enter three numbers (A, B, C) in descending order. Calculate and display the sum and differences: (A+B, A-B, A+C, A-C, B+C, B-C, A+B+C). Display a closing message. Program Requirements The program must be fully documented and laid out according to the CS271 Style Guide. This includes a complete...
Write a program in java which is in main and uses no classes, methods, loops or...
Write a program in java which is in main and uses no classes, methods, loops or if statements Ask the user to enter their first name Ask the user to enter their last name Print their initials followed by periods (ie. Clark Kent = C. K.) Print the number of letters in their last name
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...
Python Write a program that loops, prompting the user for their full name, their exam result...
Python Write a program that loops, prompting the user for their full name, their exam result (an integer between 1 and 100), and then writes that data out to file called ‘customers.txt’. The program should check inputs for validity according to the following rules: First and last names must use only alphabetical characters. No spaces, hyphens or special characters. Names must be less than 20 characters long. Exam result (an integer between 1 and 100 inclusive) The file should record...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT