C# Sharp Exercises: Take a positive number and return the nth odd number
C# Sharp Basic: Exercise-75 with Solution
Write a C# Sharp program which takes a positive number and return the nth odd number.
Sample Solution:
C# Sharp Code:
using System;
using System.Linq;
namespace exercises
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("1st odd number: "+test(1));
Console.WriteLine("2nd odd number: " + test(2));
Console.WriteLine("4th odd number: " + test(4));
Console.WriteLine("100th odd number: " + test(100));
}
public static int test(int n)
{
return n * 2 - 1;
}
}
}
Sample Output:
1st odd number: 1 2nd odd number: 3 4th odd number: 7 100th odd number: 199
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# Sharp program to check the length of a given string is odd or even. Return 'Odd length' if the string length is odd otherwise 'Even length'.
Next: Write a C# Sharp program to get the ASCII value of a given character.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework