The Program in C# Program to Check length of Given string is odd or even given below:
using System;
namespace StringLengthCheck
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family,enter a string here to check whether the string has even or odd length: ");
string inputString = Console.ReadLine();
if (inputString.Length % 2 == 0)
{
Console.WriteLine("Okay, This is even length");
}
else
{
Console.WriteLine("Okay, This is Odd length");
}
}
}
}
Output:
Hello Codeauri Family,enter a string here to check whether the string has even or odd length:
is it true codeauri?
Okay, This is even length
Pro-Tips💡
Here are the step by step execution of above program:
We start by including the System
namespace, which provides access to various types and methods used in the program, including the Console
class for reading input from the user and writing output to the console.
We define a Program
class, which contains the Main
method that is the entry point of the program.
In the Main
method, we first write a prompt to the console asking the user to enter a string using Console.WriteLine("Enter a string: ")
.
We then read the user’s input using string inputString = Console.ReadLine()
. The input entered by the user is stored in the inputString
variable.
Next, we use an if
statement to check if the length of the string is even or odd. We use the inputString.Length
property to get the length of the string, and then use the modulo operator %
to see if the length divided by 2 has a remainder of 0.
If the result of inputString.Length % 2
is 0, the length of the string is even, and we print “Even length” to the console using Console.WriteLine("Even length")
.
If the result of inputString.Length % 2
is not 0, the length of the string is odd, and we print “Odd length” to the console using Console.WriteLine("Odd length")
.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.