Find the Nth term of the series 14, 28, 20, 40,…..
Last Updated :
08 Jul, 2022
Given a number . The task is to find the N-th term in the following series:
14, 28, 20, 40, 32, 64…..
Examples:
Input : N = 5
Output : 32
Input : N = 6
Output : 64
Approach:
- Initialize first number with 14.
- Run a loop from i = 2 to N and do following steps:
- For Even i, double the previous term. For example if i = 2, current term will be 2*(term when i = 1), that is 2*14 = 28.
- For Odd i, subtract 8 from the previous term.
- Exit the loop and print the final number.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int findNth( int N)
{
int b = 14;
int i;
for (i = 2; i <= N; i++) {
if (i % 2 == 0)
b = b * 2;
else
b = b - 8;
}
return b;
}
int main()
{
int N = 6;
cout << findNth(N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findNth( int N)
{
int b = 14 ;
int i;
for (i = 2 ; i <= N; i++) {
if (i % 2 == 0 )
b = b * 2 ;
else
b = b - 8 ;
}
return b;
}
public static void main (String[] args) {
int N = 6 ;
System.out.print(findNth(N));
}
}
|
Python 3
def findNth(N):
b = 14
for i in range ( 2 , N + 1 ):
if (i % 2 = = 0 ):
b = b * 2
else :
b = b - 8
return b
N = 6
print (findNth(N))
|
C#
using System;
public class GFG{
static int findNth( int N)
{
int b = 14;
int i;
for (i = 2; i <= N; i++) {
if (i % 2 == 0)
b = b * 2;
else
b = b - 8;
}
return b;
}
static public void Main (){
int N = 6;
Console.WriteLine(findNth(N));
}
}
|
PHP
<?php
function findNth( $N )
{
$b = 14;
for ( $i = 2; $i <= $N ; $i ++)
{
if ( $i % 2 == 0)
$b = $b * 2;
else
$b = $b - 8;
}
return $b ;
}
$N = 6;
echo findNth( $N );
?>
|
Javascript
<script>
function findNth(N)
{
let b = 14;
for (let i = 2; i <= N; i++)
{
if (i % 2 == 0)
b = b * 2;
else
b = b - 8;
}
return b;
}
N = 6;
document.write(findNth(N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...