Function Isprimetarea - C# Programming Exercise

In this exercise of C#, you will learn how to create a function named IsPrime that receives an integer number and returns true if the number is prime, or false if it is not. Prime numbers are those that have only two divisors: 1 and the number itself. This type of exercise is very useful for understanding how to implement mathematical algorithms in C# and how to use conditionals to check properties of numbers. In the example, if the function receives the number 127, it returns true, since 127 is a prime number. This exercise will help you understand the concept of primality and its implementation in C#.

Learn how to check if a number is prime with this hands-on function in C#, which will strengthen your understanding of working with numbers and algorithms in this programming language.

 Category

Functions

 Exercise

Function Isprimetarea

 Objective

Write a C# function named "IsPrime", which receives an integer number and retuns true if it is prime, or false if it is not:

if (isPrime(127)) ...

 Write Your C# Exercise

// Importing the System namespace to access basic system functions
using System;

class Program
{
    // Function to check if a number is prime
    public static bool IsPrime(int number)
    {
        // A prime number is greater than 1 and has no divisors other than 1 and itself
        if (number <= 1)
        {
            return false; // Numbers less than or equal to 1 are not prime
        }

        // Loop through numbers from 2 to the square root of the number
        for (int i = 2; i * i <= number; i++)
        {
            // If the number is divisible by any number in the range, it is not prime
            if (number % i == 0)
            {
                return false; // Not a prime number
            }
        }

        // If no divisors are found, the number is prime
        return true;
    }

    // Main method where the IsPrime function is called
    public static void Main()
    {
        // Test the function with the number 127
        if (IsPrime(127))
        {
            Console.WriteLine("127 is a prime number.");
        }
        else
        {
            Console.WriteLine("127 is not a prime number.");
        }
    }
}

 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 Parameters of Main, Sum

    In this exercise of C#, you will learn how to create a program named sum that receives two integer numbers from the command line and displays their sum. This t...

  •  Function SumDigits

    In this exercise of C#, you will learn how to create a function named SumDigits that receives a number and returns the result of summing its digits. Thi...

  •  Function Factorial

    In this exercise of C#, you will learn how to create a recursive function to calculate the factorial of a number. The factorial of a number is ex...

  •  Function Parameters of Main, Reverse

    In this exercise of C#, you will learn how to create a program named reverse that receives several words from the command line and display...

  •  Function GetInt

    In this exercise of C#, you will learn how to create a function named GetInt, which displays on screen the text received as a parameter, asks the user f...

  •  Function tasks database

    In this exercise of C#, you will improve a "tasks database" program by splitting it into multiple functions. This type of exercise is great for l...