In this article, we will write a Java code to evaluate the following series. The return type of the output should be an integer.
1+3-5+7-9…+/-n.
Input and Output Format
- Input consists of an odd positive integer n.
- The output is a single integer.
Refer sample output for formatting specifications
Sample Input 1:
9
Sample Output 1:
-3
Sample Input 2:
11
Sample Output 2:
8
Program to Generate the series in Java
Following is the steps to generate series:
- Input the number “n” from the user to which we want to generate a series.
- Pass the number to consecutiveSumSubOfOddNos() method.
- Inside the method, create an empty array list.
- Now, iterate using loop and check if the index is odd then append the specified element to the list.
- Now, get the 0th element from the list and store it in the n1 variable.
- Now, traverse over the list, and check if the index is odd then get the element at the specified position and add it to value in the n1 variable.
- Otherwise, subtract it with the value in the n1 variable.
- At last, return the value in the n1 variable.
import java.util.*; public class Main { public static int consecutiveSumSubofOddNos(int n) { List<Integer> l1 = new ArrayList<Integer>(); for (int i = 1; i <= n; i++) if (i % 2 != 0) l1.add(i); int n1 = l1.get(0); for (int i = 1; i < l1.size(); i++) if (i % 2 != 0) n1 = n1 + l1.get(i); else n1 = n1 - l1.get(i); return n1; } public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); System.out.println(consecutiveSumSubofOddNos(n)); } }
Output
Generate the series 1,4,7…. up to 10 terms
Write a code to generate the following series. The return type should be the sum of the elements of this series
1+4+7+10+13+16+19+22+25+28 = 145
Input and Output Format
- Input consists of a positive integer n.
- The output is a single integer.
Refer sample output for formatting specifications
Sample Input 1:
10
Sample Output 1:
145 (1+4+7+10+13+16+19+22+25+28)
Program to generate the series in Java
Following is the step to generate the sum of the above given series:
- Input the nth term from the user.
- Pass the number to the seriesGeneration() method.
- Inside the method, declare and initialize sum and num variable to 0 and 1.
- Use for loop, and add each value in num variable to sum variable and increment num variable to 3 for each iteration.
- Repeat this process till the nth term is reached.
- At last, print the value in the sum variable.
package com.company; import java.util.Scanner; public class Solution19 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); seriesGeneration(n); } private static void seriesGeneration(int n) { int sum = 0,num = 1; for(int i = 1 ;i <= n ;i++){ System.out.println(num+ " "); sum = sum + num; num = num + 3; } System.out.println(sum); } }
Output
Generate the series 1,8,27… Up to 10 terms
Write a code to generate the following series. The return type should be the sum of the elements of this series
1 8 27 .... nth term
Input and Output Format
- Input consists of a positive integer n.
- The output is a single integer.
Refer sample output for formatting specifications
Sample Input 1:
10
Sample Output 1:
3025 (1+8+27+64+125+216+343+512+729+1000)
Program to Generate series 1,8,27….. in Java
Following is the step to generate the sum of the above given series:
- Input the nth term from the user.
- Pass the number to the seriesGeneration() method.
- Inside the method, declare and initialize sum and num variable to 0 and 1.
- Use for loop, and take the cube of the value in num variable and store it in value variable. And then add this value to the sum variable.
- Repeat this process till the nth term is reached.
- At last, print the value in the sum variable.
package com.company; import java.util.Scanner; public class Solution19 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); seriesGeneration(n); } private static void seriesGeneration(int n) { int sum = 0,num = 1; for(int i = 1 ;i <= n ;i++){ int value = num * num * num; System.out.println(value+ " "); sum = sum + value; num++; } System.out.println(sum); } }
Output
Thus, in this way, we learn how to generate different series in Java.