Program for n-th even number
Given a number n, print the nth even number. The 1st even number is 2, 2nd is 4 and so on.
Examples:
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++
#include <bits/stdc++.h>
using namespace std;
int nthEven( int n)
{
return (2 * n);
}
int main()
{
int n = 10;
cout << nthEven(n);
return 0;
}
|
Java
class GFG
{
static int nthEven( int n)
{
return ( 2 * n);
}
public static void main(String [] args)
{
int n = 10 ;
System.out.println(nthEven(n));
}
}
|
Python3
def nthEven(n):
return ( 2 * n)
if __name__ = = '__main__' :
n = 10
print (nthEven(n))
|
C#
using System;
class GFG
{
static int nthEven( int n)
{
return (2 * n);
}
public static void Main()
{
int n = 10;
Console.WriteLine(nthEven(n));
}
}
|
PHP
<?php
function nthEven( $n )
{
return (2 * $n );
}
$n = 10;
echo nthEven( $n );
?>
|
Javascript
function nthEven(n)
{
return (2 * n);
}
let n = 10;
console.log(nthEven(n));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
20 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...