In this tutorial I will show how to generate Odd numbers in c#. I have given two example first example is very simple. First example I have used simple for loop to find Odd numbers. Second example I have created a Boolean type function then used the function for finding Odd Numbers. Both the examples check reminder of a number. If the reminder not 0 then the number must be odd number.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OddNumber
{
class Program
{
static void Main()
{
Console.WriteLine("--First Odd numbers Example--");
for (int i = 0; i <= 20; i++)
{
if (i % 2 != 0)
{
Console.WriteLine(i);
}
}
Console.WriteLine("--Second Odd numbers Example--");
for (int i = 0; i <= 10; i++)
{
if (IsOdd(i))
{
Console.WriteLine(i);
}
}
Console.ReadLine();
}
public static bool IsOdd(int value)
{
return value % 2 != 0;
}
}
}
Материалы на данной страницы взяты из открытых источников либо размещены пользователем в соответствии с договором-офертой сайта. Вы можете сообщить о нарушении.