Write an algorithm and draw a flowchart to print the given series 2, 8, 18, 32, ---- up to 10th term
Answer this plz.
Answers
Answer:
// C++ program to print the series
// 2, 1, 4, 3, 6, 5, …. up to N terms
#include <iostream>
using namespace std;
// Function to print the series
void printPattern(int N)
{
for (int i = 1; i <= N; i++) {
// Find and print the ith term
cout <<" "<<((i % 2 == 0) ? (i - 1) : (i + 1));
}
}
// Driver coding
int main()
{
// Get the value of N
int N = 10;
// Print the Series
printPattern(N);
return 0;
}
Output: 2 1 4 3 6 5 8 7 10 9
Explanation:
Input: N = 4
Output: 2, 1, 4, 3
Explanation:
Nth Term = (N % 2 == 0) ? (N - 1) : (N + 1)
1st term = (1 % 2 == 0) ? (1 - 1) : (1 + 1)
= (1 + 1)
= 2
2nd term = (2 % 2 == 0) ? (2 - 1) : (2 + 1)
= (2 - 1)
= 1
3rd term = (3 % 2 == 0) ? (3 - 1) : (3 + 1)
= (3 + 1)
= 4
4th term = (4 % 2 == 0) ? (4 - 1) : (4 + 1)
= (4 - 1)
= 3
Therefore, Series = 2, 1, 4, 3
Input: N = 7
Output: 2, 1, 4, 3, 6, 5, 8