0% found this document useful (0 votes)
3 views

Code

This C# program prompts the user to enter a four-character element in a series, validates the input, and calculates the next element based on specific rules. If the input is valid, it increments the characters and numbers accordingly, handling cases where the number reaches 99 or the characters reach 'Z'. The program returns 'Input is invalid' for any incorrect input format.

Uploaded by

gobinath
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Code

This C# program prompts the user to enter a four-character element in a series, validates the input, and calculates the next element based on specific rules. If the input is valid, it increments the characters and numbers accordingly, handling cases where the number reaches 99 or the characters reach 'Z'. The program returns 'Input is invalid' for any incorrect input format.

Uploaded by

gobinath
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

using System;

class HelloWorld {

static void Main() {


Console.WriteLine("Enter the element in the series:");
string input = Console.ReadLine();
string nextElement = FindNextElement(input);
Console.WriteLine("Next element in the series: " + nextElement);
}

public static string FindNextElement(string input1)


{
if (input1.Length != 4 || !IsValidInput(input1))
{
return "Input is invalid";
}

if (input1 == "ZZ99")
{
return input1;
}

char firstone = input1[0];


char secondone = input1[1];
int number = int.Parse(input1.Substring(2, 2));

if (number == 99)
{
if (secondone == 'Z')
{
firstone++;
secondone = 'A';
}
else
{
secondone++;
}
number = 0;
}
else
{
number++;
}

return $"{firstone}{secondone}{number:00}";
}
public static bool IsValidInput(string input)
{
char firstChar = input[0];
char secondChar = input[1];
int number;

return (firstChar >= 'A' && firstChar <= 'Z') &&


(secondChar >= 'A' && secondChar <= 'Z') &&
int.TryParse(input.Substring(2, 2), out number) &&
(number >= 0 && number <= 99);
}
}

You might also like