C# program to find Sum of first n Odd Natural Numbers
Program
using System;
namespace SumOfNOddNumbers
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the value of n:\t");
int n = int.Parse(Console.ReadLine());
int i, oddSum = 0;
for(i = 1; i <= n; i++)
{
if((i % 2) != 0)
{
oddSum += i;
}
}
Console.WriteLine("Sum of odd numbers from 1 to {0} is {1}\t", n, oddSum);
}
}
}
Output
Enter the value of n: 15
Sum of odd numbers from 1 to 15 is 64