📌  相关文章
📜  找到系列14、28、20、40 …的N个项。

📅  最后修改于: 2021-05-25 19:38:07             🧑  作者: Mango

给定一个数字N  。任务是在以下系列中找到第N个术语:

例子:

Input : N = 5
Output : 32

Input : N = 6
Output : 64

方法:

  1. 用14初始化第一个数字。
  2. 运行从i = 2到N的循环,并执行以下步骤:
    • 对于偶数我,将上一个学期加倍。例如,如果i = 2,则当前项将是2 *(当i = 1时的项),即2 * 14 = 28。
    • 对于奇数i,从上一项中减去8。
    • 退出循环并打印最终号码。

下面是上述方法的实现:

C++
// CPP program to find the Nth term of
// the series 14, 28, 20, 40, …..
 
#include 
using namespace std;
 
// Function to find the N-th term
int findNth(int N)
{
    // initializing the 1st number
    int b = 14;
 
    int i;
 
    // loop from 2nd term to nth term
    for (i = 2; i <= N; i++) {
        // if i is even, double the
        // previous number
        if (i % 2 == 0)
            b = b * 2;
        // if i is odd, subtract 8 from
        // previous number
        else
            b = b - 8;
    }
 
    return b;
}
 
// Driver Code
int main()
{
    int N = 6;
 
    cout << findNth(N);
 
    return 0;
}


Java
// Java program to find the Nth term of
// the series 14, 28, 20, 40,
 
import java.io.*;
 
class GFG {
   
 
// Function to find the N-th term
 static int findNth(int N)
{
    // initializing the 1st number
    int b = 14;
 
    int i;
 
    // loop from 2nd term to nth term
    for (i = 2; i <= N; i++) {
        // if i is even, double the
        // previous number
        if (i % 2 == 0)
            b = b * 2;
        // if i is odd, subtract 8 from
        // previous number
        else
            b = b - 8;
    }
 
    return b;
}
 
// Driver Code
 
 
    public static void main (String[] args) {
        int N = 6;
 
    System.out.print(findNth(N));
    }
}
// This code is contributed by shs


Python 3
# Python 3 program to find the Nth term
# of the series 14, 28, 20, 40, …..
 
# Function to find the N-th term
def findNth(N):
     
    # initializing the 1st number
    b = 14
 
    # loop from 2nd term to nth term
    for i in range (2, N + 1):
     
        # if i is even, double the
        # previous number
        if (i % 2 == 0):
            b = b * 2
             
        # if i is odd, subtract 8 from
        # previous number
        else:
            b = b - 8
 
    return b
 
# Driver Code
N = 6
 
print(findNth(N))
 
# This code is contributed
# by Akanksha Rai


C#
// C# program to find the Nth term of
// the series 14, 28, 20, 40,
 
using System;
 
public class GFG{
    // Function to find the N-th term
static int findNth(int N)
{
    // initializing the 1st number
    int b = 14;
 
    int i;
 
    // loop from 2nd term to nth term
    for (i = 2; i <= N; i++) {
        // if i is even, double the
        // previous number
        if (i % 2 == 0)
            b = b * 2;
        // if i is odd, subtract 8 from
        // previous number
        else
            b = b - 8;
    }
 
    return b;
}
 
// Driver Code
     
    static public void Main (){
    int N = 6;
    Console.WriteLine(findNth(N));
    }
}
// This code is contributed by ajit


PHP


Javascript


输出:
64