Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

An arithmetic sequence is a sequence, in which the difference between any two co

ID: 3854876 • Letter: A

Question

An arithmetic sequence is a sequence, in which the difference between any two consecutive terms is the same, i.e., the difference is a constant. For example, the sequence that begins 1, 4, 7, 10, 13, 16, .. is an arithmetic sequence since the difference between consecutive terms is always 3. Write a Java program to generate arithmetic sequences. Your program will invoke 2 methods: one method to generate the arithmetic sequence and save it in a 1-dimensional array: and one method to print that | 1-dimesional array. Write your program so that it will generate output similar to the sample output below: How many times do you want to find arithmetic sequences? -3 ERROR! Should be positive. REENTER: 3 What is the first number in the sequence: 5 What is the difference between consecutive terms: 3 How many numbers in the sequence (up to 1000): 17 The arithmetic sequence is: 5 8 11 14 17 20 23 26 29 32 35 38 41 44 47 50 53 What is the first number in the sequence: 1 What is the difference between consecutive terms: 2 How many numbers in the sequence (up to 1000): 25 The arithmetic sequence is: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 What is the first number in the sequence: -15 What is the difference between consecutive terms: 5 How many numbers in the sequence (up to 1000): 10 The arithmetic sequence is: -15 -10 -5 0 5 10 15 20 25 30 SOLUTION: import java.util.Scanner: class arithmeticSequence { public static final int SIZE = 1000: public static void main(String[] args) { Scanner input = new Scanner(System.in): int how_many, size: int first, diff: System.out.print ("How many times do you want to find arithmetic sequences? "): how_many = input.nextInt(): //CONTINUE FROM HERE:

Explanation / Answer

ArithmeticSequence.java

import java.util.Scanner;

public class ArithmeticSequence {
public static final int SIZE = 1000;

public static void main(String[] args) {

//Scanner object is used to get the inputs entered by the user
Scanner input = new Scanner(System.in);

//Declaring variables
int how_many, size;
int first, diff;

System.out.print("How many times do you want to find arithmetic sequences?");

/* This while loop continues to execute
* until the user enters a valid positive number
*/
while (true) {
//getting the input entered by the user
how_many = input.nextInt();
if (how_many < 0) {
System.out.print("ERROR! Should be positive. REENTER:");
continue;
} else
break;
}

//This loop execute based on how many times user want to execute
for (int i = 1; i <= how_many; i++) {
//getting the inputs entered by the user
System.out.print("What is the first number in the sequence:");
first = input.nextInt();
System.out.print("What is the difference between consecutive terms:");
diff = input.nextInt();
System.out.print("How many numbers in the sequence (up to 1000):");
size = input.nextInt();

//Creating an 1-Dimensional array based on SIZE
int nos[] = new int[SIZE];

//Calling the methods
nos = generateSequence(first, diff, size, nos);
displayArray(nos, size);

}
}

//This method display the elements in the array
private static void displayArray(int[] nos, int size) {
System.out.println("The arithmetic sequence is : ");
for (int i = 0; i < size; i++) {
System.out.print(nos[i] + " ");
}
System.out.println();
}

/* This method will generate the arithmetic sequence of numbers
* and populate those elements in the array
*/
private static int[] generateSequence(int first, int diff, int size, int[] nos) {
int i = 0;
while (i <= size) {
nos[i] = first;
first += diff;
i++;
}
return nos;
}

}

_____________________

Output:

How many times do you want to find arithmetic sequences?-3
ERROR! Should be positive. REENTER:3
What is the first number in the sequence:5
What is the difference between consecutive terms:3
How many numbers in the sequence (up to 1000):17
The arithmetic sequence is :

5 8 11 14 17 20 23 26 29 32 35 38 41 44 47 50 53
What is the first number in the sequence:1
What is the difference between consecutive terms:2
How many numbers in the sequence (up to 1000):25
The arithmetic sequence is :

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
What is the first number in the sequence:-15
What is the difference between consecutive terms:5
How many numbers in the sequence (up to 1000):10
The arithmetic sequence is :

-15 -10 -5 0 5 10 15 20 25 30


_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote