Dado un número n, imprima el n-ésimo número impar. El primer número impar es 1, el segundo es 3 y así sucesivamente.
Ejemplos:
Input : 3 Output : 5 First three odd numbers are 1, 3, 5, .. Input : 5 Output : 9 First 5 odd numbers are 1, 3, 5, 7, 9, ..
El enésimo número impar viene dado por la fórmula 2*n-1.
C++
// CPP program to find the nth odd number #include <bits/stdc++.h> using namespace std; // Function to find the nth odd number int nthOdd(int n) { return (2 * n - 1); } // Driver code int main() { int n = 10; cout << nthOdd(n); return 0; }
Java
// JAVA program to find the nth odd number class GFG { // Function to find the nth odd number static int nthOdd(int n) { return (2 * n - 1); } // Driver code public static void main(String [] args) { int n = 10; System.out.println(nthOdd(n)); } } // This code is contributed // by ihritik
Python3
# Python 3 program to find the # nth odd number # Function to find the nth odd number def nthOdd(n): return (2 * n - 1) # Driver code if __name__=='__main__': n = 10 print(nthOdd(n)) # This code is contributed # by ihritik
C#
// C# program to find the nth odd number using System; class GFG { // Function to find the nth odd number static int nthOdd(int n) { return (2 * n - 1); } // Driver code public static void Main() { int n = 10; Console.WriteLine(nthOdd(n)); } } // This code is contributed // by inder_verma
PHP
<?php // PHP program to find the // Nth odd number // Function to find the // Nth odd number function nthOdd($n) { return (2 * $n - 1); } // Driver code $n = 10; echo nthOdd($n); // This code is contributed // by inder_verma ?>
Javascript
<script> // Javascript program to find the nth odd number // Function to find the nth odd number function nthOdd(n) { return (2 * n - 1); } // Driver code var n = 10; document.write( nthOdd(n)); </script>
Producción:
19
Publicación traducida automáticamente
Artículo escrito por rupesh_rao y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA