Horje
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++

<?php
// PHP program to find the
// nth even number
   
// Function to find the
// nth even number
function nthEven($n)
{
    return (2 * $n);
}
   
// Driver code
$n = 10;
echo nthEven($n);
   
// This code is contributed
// by anuj_67
?>

Javascript

// JavaScript program to find the nth even number
 
   
// Function to find the nth even number
function nthEven(n)
{
    return (2 * n);
}
   
// Driver code
let n = 10;
console.log(nthEven(n));
 
 
// This code is contributed by phasing17

Output

20

Time Complexity: O(1)

Auxiliary Space: O(1)




Reffered: https://www.geeksforgeeks.org


C++ Programs

Related
Program to find the Radius of the incircle of the triangle Program to find the Radius of the incircle of the triangle
C++ Program to Make a Simple Calculator C++ Program to Make a Simple Calculator
Difference between continue and break statements in C++ Difference between continue and break statements in C++
upper_bound and lower_bound for non increasing vector in c++ upper_bound and lower_bound for non increasing vector in c++
Possible number of Rectangle and Squares with the given set of elements Possible number of Rectangle and Squares with the given set of elements

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
10