Function Isnumber - C# Programming Exercise

In this exercise of C#, you will learn how to create a function that tells if a text string represents an integer number. This function should be used like this: if (IsNumber("1234")) System.Console.WriteLine("It is a numerical value");. If the string is a valid integer number, the function will return true and print the message "It is a numerical value". This exercise is ideal for learning how to work with type conversions in C#, check if a string can be interpreted as a number, and how to perform this validation before doing mathematical or logical operations in a program. Moreover, this type of function can be useful when receiving user input and ensuring that the entered values are integers before processing them. With this exercise, you will improve your ability to handle data types and perform checks efficiently in your programs.

This exercise is an excellent way to understand how to use validation functions in C# and how to work with text strings to ensure data correctness.

 Category

Functions

 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

// 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
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Function calculator, params of Main

    In this exercise of C#, you will learn how to create a program that performs mathematical operations such as addition, subtraction, multiplication, or division...

  •  Function calculator, params and return value of Main

    In this exercise of C#, you will create a program that calculates mathematical operations like addition, subtraction, multiplication, or division, by analyzing...

  •  Function MinMaxArray

    In this exercise of C#, you will create a function named MinMaxArray that receives an array of numbers and returns the minimum and maximum values using ...

  •  Function Reverse, recursive

    In this exercise of C#, you need to create a program that uses recursion to reverse a string of characters. The program should receive a string as input...

  •  Function WriteRectangle

    In this exercise of C#, you need to create two functions: one called WriteRectangle to display a filled rectangle on the screen using asterisks, and another ca...

  •  Function Palindrome, iterative

    In this exercise in C#, you need to create an iterative function that checks if a string is symmetric (a palindrome). A palindrome is a word, number, phrase, or other...