Exercise
Function Isnumber
Objective
Write a C# function that tells if a string is an intenger number. It should be used like this:
if (IsNumber ("1234"))
System.Console.WriteLine ("It is a numerical value");
Write Your C# Exercise
C# Exercise Example
// Import the System namespace to use basic classes like Console
using System;
class Program
{
// Main method to drive the program
public static void Main()
{
// Test the IsNumber function
if (IsNumber("1234"))
{
Console.WriteLine("It is a numerical value");
}
else
{
Console.WriteLine("It is not a numerical value");
}
if (IsNumber("abc"))
{
Console.WriteLine("It is a numerical value");
}
else
{
Console.WriteLine("It is not a numerical value");
}
}
// Function to check if a string is a number
public static bool IsNumber(string str)
{
// Try to parse the string as an integer
return int.TryParse(str, out _); // If it can be parsed as an integer, return true, else false
}
}