- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to find Nth number of the series 1, 6, 15, 28, 45, …..
In this problem, we are given an integer value N. Our task is to create a program to Find Nth number of the series 1, 6, 15, 28, 45, …
In the series, every element is 2 less than the mean of the previous and next element.
Let’s take an example to understand the problem,
Input
N = 5
Output
45
Solution Approach
The Nth term of the series 1, 6, 15, 28, 45, … can be found using the formula,
TN = 2*N*N - N
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; #define mod 1000000009 int calcNthTerm(long n) { return (((2 * n * n) % mod) - n + mod) % mod; } int main(){ long N = 19; cout<<N<<"th Term of the series is "<<calcNthTerm(N); return 0; }
Output
19th Term of the series is 703
Advertisements