给定数字n,请打印第n个偶数。第一个偶数是2,第二个偶数是4,依此类推。
例子:
Input : 3
Output : 6
First three even numbers are 2, 4, 6, ..
Input : 5
Output : 10
First five even numbers are 2, 4, 6, 8, 19..
The nth even number is given by the formula 2*n.
C++
// CPP program to find the nth even number
#include
using namespace std;
// Function to find the nth even number
int nthEven(int n)
{
return (2 * n);
}
// Driver code
int main()
{
int n = 10;
cout << nthEven(n);
return 0;
}
Java
// JAVA program to find the nth EVEN number
class GFG
{
// Function to find the nth odd number
static int nthEven(int n)
{
return (2 * n);
}
// Driver code
public static void main(String [] args)
{
int n = 10;
System.out.println(nthEven(n));
}
}
// This code is contributed
// by ihritik
Python3
# Python 3 program to find the
# nth odd number
# Function to find the nth Even number
def nthEven(n):
return (2 * n)
# Driver code
if __name__=='__main__':
n = 10
print(nthEven(n))
# This code is contributed
# by ihritik
C#
// C# program to find the
// nth EVEN number
using System;
class GFG
{
// Function to find the
// nth odd number
static int nthEven(int n)
{
return (2 * n);
}
// Driver code
public static void Main()
{
int n = 10;
Console.WriteLine(nthEven(n));
}
}
// This code is contributed
// by anuj_67
PHP
Output:20
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。