Q:

Write a C# Sharp program which takes a positive number and return the nth odd number

0

Write a C# Sharp program which takes a positive number and return the nth odd number

Sample Output:

1st odd number: 1
2nd odd number: 3
4th odd number: 7
100th odd number: 199

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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;
        }
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now