Answer to Question #284273 in C# for Kim

Question #284273

Create a program that will generate the sample screen output below:

Enter how many number(s) to be checked: 28

Number Sum of Factors Remarks

1 0 Deficient

2 1 Deficient

3 1 Deficient

4 3 Deficient

5 1 Deficient

6 6 Perfect

7 1 Deficient

8 7 Deficient

9 4 Deficient

10 8 Deficient

11 1 Deficient

12 16 Abundant

13 1 Deficient

14 10 Deficient

15 9 Deficient

16 15 Deficient

17 1 Deficient

18 21 Abundant

19 1 Deficient

20 22 Abundant

21 11 Deficient

22 14 Deficient

23 1 Deficient

24 36 Abundant

25 6 Deficient

26 16 Deficient

27 13 Deficient

28 28 Perfect

Summary

Deficient number(s): 22

Abundant number(s): 4

Perfect number(s): 2

Total number(s): 28


1
Expert's answer
2022-01-03T14:25:01-0500
using System;

namespace Deficient_numbers
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            Console.Write("Enter how many number(s) to be checked: ");
            int count = int.Parse(Console.ReadLine());
            Console.Write("Select a range of random numbers from 0 to: ");
            int range = int.Parse(Console.ReadLine());
            // counters
            int abundant = 0;
            int deficient = 0;
            int perfect = 0;
            for (int i = 1; i <= count; i++)
            {
                int n = rnd.Next(range);
                Console.Write($"[{i}] ");
                
                // Checking Abundant numbers
                int sum = 0;
                for(int k = 1; k < n; k++) {
                    if(n % k == 0) 
                        sum = sum + k;
                }
                if (n < sum) {
                    Console.WriteLine($"{n} = Abundant");
                    abundant++;
                    continue;
                }
                
                if(isDef(n)) {  // Checking deficient numbers
                    Console.WriteLine($"{n} = Deficient.");
                    deficient++;
                    continue;
                } 
                
                if (is_perfect(n)) { // Checking perfect numbers
                    perfect++;
                    Console.WriteLine($"{n} = Perfect");
                }
            }

            Console.WriteLine($"\nSummary:\nDeficient number(s): {deficient}\nAbundant number(s): {abundant}\nPerfect number(s): {perfect}\nTotal number(s): {count}");
            Console.ReadLine();
        }
        static int divsum(int n)   // For deficient numbers
        {   
            int sum = 0;
            for (int i = 1; i <= (Math.Sqrt(n)); i++) {   
                if (n % i == 0) {  
                    if (n / i == i)   
                        sum = sum + i;
                    else               {   
                        sum = sum + i;   
                        sum = sum + (n / i);   
                    }   
                }   
            }
            return sum;   
        }   
    
        static bool isDef(int n)   // Checking deficient numbers
        {   
            return (divsum(n) < (2 * n));   
        }
        
        static bool is_perfect(int num) // Checking perfect numbers 
        {
            int sum = 0;
            for (int j = 1; j < num; j++)
                if (num % j == 0)
                    sum += j;
            
            if(sum == num && num != 0)
                return true;
            return false;
        }
        
        static Random rnd = new Random(DateTime.Now.Millisecond);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS