Simple program #3 (Print even and odd number
upto n)
Even number upto: 20 are:
2 4 6 8 10 12 14 16 18 20
Odd number upto: 20 are:
1 3 5 7 9 11 13 15 17 19
The program is written in C#
programming language and will accept a number as input. The logic of the
program is to print even and odd number up to n.
Let’s find out a simple and easy way
to code the program.
Practical Implementation:
using System;
namespace patternProblem.Simple_program
{
class Simple3 //Print even and odd number upto n
{
static void Main(string[] args)
{
Console.Write("Enter a number upto which you want to print even and
odd number: ");
int
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("-----Output-----");
Console.WriteLine();
Console.WriteLine("Even number upto: {0} are: ", n);
for
(int i = 1; i <= n; i++)
{
if (i % 2 == 0)
{
Console.Write(i + " ");
}
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Odd number upto: {0} are: ", n);
for
(int i = 1; i <= n; i++)
{
if (i % 2 != 0)
{
Console.Write(i + " ");
}
}
Console.WriteLine();
Console.ReadKey();
}
}
}
Output:
The input
number here is 20. So, the program will print even and odd number separately upto
20. The output is shown below:
Enter a number upto which you want to print even
and odd number:
20
Even number upto: 20 are:
2 4 6 8 10 12 14 16 18 20
Odd number upto: 20 are:
1 3 5 7 9 11 13 15 17 19
Press any key to continue . . .
For any
query, comment us below.
Skip to
Main Table Simple program
Keep learning and sharing...
No comments:
Post a Comment