c-sharp
  1. c-sharp-program-to-check-prime-or-armstrong-number-using-user-defined-function

Program to Check Prime or Armstrong Number Using User-defined Function - (C# Basic Programs)

C# is a powerful programming language that can help you create a wide range of applications. In this tutorial, we will discuss a C# program to check whether a given number is prime or an Armstrong number using user-defined functions.

Syntax

The syntax for checking whether a number is prime or an Armstrong number using user-defined functions in C# is as follows:

static bool IsPrime(int number) {
    // code to determine if number is prime
}

static bool IsArmstrong(int number) {
    // code to determine if number is an Armstrong number
}

// call the user-defined functions in your main program

Example

using System;

namespace PrimeAndArmstrong
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a number: ");
            int number = Convert.ToInt32(Console.ReadLine());

            if (IsPrime(number))
            {
                Console.WriteLine(number + " is a prime number.");
            }
            else
            {
                Console.WriteLine(number + " is not a prime number.");
            }

            if (IsArmstrong(number))
            {
                Console.WriteLine(number + " is an Armstrong number.");
            }
            else
            {
                Console.WriteLine(number + " is not an Armstrong number.");
            }

            Console.ReadLine();
        }

        static bool IsPrime(int number)
        {
            bool isPrime = true;
            for (int i = 2; i <= number / 2; i++)
            {
                if (number % i == 0)
                {
                    isPrime = false;
                    break;
                }
            }
            return isPrime;
        }

        static bool IsArmstrong(int number)
        {
            int sum = 0;
            int originalNumber = number;

            while (number > 0)
            {
                int digit = number % 10;
                sum = sum + (digit * digit * digit);
                number = number / 10;
            }

            if (originalNumber == sum)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

Output

Enter a number: 153
153 is not a prime number.
153 is an Armstrong number.

Explanation

This program uses two user-defined functions to check whether a given number is prime or an Armstrong number. The IsPrime function checks whether the number is divisible by any number between 2 and the number divided by 2. The IsArmstrong function calculates the sum of the cubes of the digits of the number, and checks whether the sum is equal to the original number.

Use

This program can be used to check whether a given number is prime or an Armstrong number. It can be used in other programs that need to check whether a number is prime or an Armstrong number.

Summary

In this tutorial, we have discussed a C# program to check whether a given number is prime or an Armstrong number using user-defined functions. We have seen the syntax, example, output, explanation, and use of checking whether a number is prime or an Armstrong number. By using user-defined functions in C#, programmers can write clear and reusable code that can be used in a variety of applications.

Published on: