Basic Programming C# Examples

C# Program to Check length of Given string is odd or even

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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X