Программа для n-го четного числа

Опубликовано: 30 Декабря, 2021

Для числа n выведите n-е четное число. Первое четное число - 2, второе - 4 и так далее.

Примеры:

Ввод: 3
Выход: 6
Первые три четных числа - 2, 4, 6, ..

Ввод: 5
Выход: 10
Первые пять четных чисел - это 2, 4, 6, 8, 19 ..

Рекомендуется: сначала попробуйте свой подход в {IDE}, прежде чем переходить к решению.

The nth even number is given by the formula 2*n.

C ++

// CPP program to find the nth even number
#include <bits/stdc++.h>
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 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

<?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
?>

Выход:

 20
Хотите учиться на лучших видео и практических задачах, ознакомьтесь с базовым курсом C ++ для базового и продвинутого уровня C ++ и курсом C ++ STL для языка и STL. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .