Series: The Event Organizing Company “Buzzcraft” focuses event management in a way that creates a win-win situation for all involved stakeholders. Buzzcraft don’t look at building one time associations with clients, instead, aim at creating long-lasting collaborations that will span years to come. This goal of the company has helped them evolve and expand big with organizing many events till date.
The number of events that the company organizes every month is recorded sensibly and is seemed to have followed a specific series like: 30, 35, 38, 41, 54, 53 …
Write a program which takes an integer N as the input and will output the series till the Nth term.
Input Format:
First line of the input is an integer N.
Output Format:
Output a single line the series till Nth term, each separated by a comma.
Refer sample input and output for formatting specifications.
Sample Input 1:
5
Sample Output 1:
30 35 38 41 54
Sample Input 2:
10
Sample Output 2:
30 35 38 41 54 53 78 71 110 95
Solution
The series starts with 30 and 35 for the first two terms, and then alternates between adding 8 and 6 to the previous term for even and odd positions respectively. This means that the third term is 30 + 8 = 38, the fourth term is 35 + 6 = 41, the fifth term is 38 + 8 = 46, the sixth term is 41 + 6 = 47, and so on.
The program takes an integer N as input, representing the number of terms to generate. It first initializes an array of length N to store the series. It then sets the first two terms of the series to 30 and 35 respectively.
It then uses a for loop to calculate the remaining terms of the series. For each term, it checks whether the index i is even or odd using the modulo operator (%). If i is even, it calculates the term as the previous even term plus the even increment (8), and updates the even increment by adding 8 to it. If i is odd, it calculates the term as the previous odd term plus the odd increment (6), and updates the odd increment by adding 6 to it.
Finally, it prints out the entire series by looping through the array and printing each term separated by a space.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] series = new int[n];
series[0] = 30;
if (n == 1) {
System.out.print(series[0]);
return;
}
series[1] = 35;
// table of 8 and 6
int evenIncrement = 8;
int oddIncrement = 6;
for (int i = 2; i < n; i++) {
int eight = 8;
int six = 6;
if (i % 2 == 0) {
series[i] = series[i - 2] + evenIncrement;
evenIncrement += eight;
// sauravhathi
} else {
series[i] = series[i - 2] + oddIncrement;
oddIncrement += six;
}
}
for (int i = 0; i < n; i++) {
System.out.print(series[i] + " ");
}
}
}
// c++
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int series[n];
series[0] = 30;
if (n == 1) {
cout << series[0];
return 0;
}
series[1] = 35;
// table of 8 and 6
int evenIncrement = 8;
int oddIncrement = 6;
for (int i = 2; i < n; i++) {
int eight = 8;
int six = 6;
if (i % 2 == 0) {
series[i] = series[i - 2] + evenIncrement;
evenIncrement += eight;
// sauravhathi
} else {
series[i] = series[i - 2] + oddIncrement;
oddIncrement += six;
}
}
for (int i = 0; i < n; i++) {
cout << series[i] << " ";
}
return 0;
}
n = int(input())
series = [30]
if n == 1:
print(series[0])
exit()
series.append(35)
evenIncrement = 8
oddIncrement = 6
for i in range(2, n):
eight = 8
six = 6
if i % 2 == 0:
series.append(series[i - 2] + evenIncrement)
evenIncrement += eight
# sauravhathi
else:
series.append(series[i - 2] + oddIncrement)
oddIncrement += six
for i in range(n):
print(series[i], end=" ")
Happy Learning – If you require any further information, feel free to contact me.