Basic Programming C# Examples

C# Program to Check If Given Number is perfect number or not

The Program in  C# Program to Check If Given Number is perfect number or not is given below:

using System;

namespace PerfectNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter a number to check if it is a perfect number:");
            int n = int.Parse(Console.ReadLine());

            int sum = 0;
            for (int i = 1; i < n; i++)
            {
                if (n % i == 0)
                {
                    sum += i;
                }
            }

            if (sum == n)
            {
                Console.WriteLine("Yes, " + n + " is a perfect number.");
            }
            else
            {
                Console.WriteLine("No, " + n + " is not a perfect number.");
            }

            Console.ReadLine();
        }
    }
}

Output:

Hello Codeauri Family, enter a number to check if it is a perfect number:
28
Yes, 28 is a perfect number.

Pro-Tips💡

Here are the step by step execution of above program:

  1. The code starts by importing the System namespace.
  2. The program defines a Program class with a Main method that takes an array of strings as arguments (string[] args).
  3. In the Main method, the program prompts the user to enter a number. The number is then stored in the n variable.
  4. The program uses a for loop to iterate from 1 to n - 1. For each iteration, the program checks if the current iteration number (i) is a factor of n (n % i == 0).
  5. If the current iteration number is a factor of n, the program adds it to the sum variable.
  6. After the loop, the program checks if sum is equal to n.
  7. If sum is equal to n, the program prints a message indicating that n is a perfect number.
  8. If sum is not equal to n, the program prints a message indicating that n is not a perfect number.
  9. The program ends with a ReadLine method call, which waits for the user to press Enter before closing the program.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X