C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to determine whether a given number is an abundant numberThe abundant number can be called as an excessive number and defined as the number for which the sum of its proper divisors is greater than the number itself. A first abundant number is the integer 12 having the sum (16) of its proper divisors (1, 2, 3, 4, 6) which is greater than itself (12). Examples: 12, 18, 20, 24, 30, 36 In this program, we have to check whether a given number is an abundant number using the algorithm given below. AlgorithmMAIN
CheckAbundant (n)
GetSum(n)
Java programimport java.util.*; public class Abundant_number { public static void main(String[] args) { System.out.println("Enter the number?"); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Abundant_number a = new Abundant_number(); if(a.checkAbundant(n)) { System.out.println("The number is Abundant."); } else { System.out.println("The number is not Abundant."); } } public int getSum(int n) { 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); } } } sum = sum - n; return sum; } public boolean checkAbundant(int n) { return (getSum(n) > n); }} Output: Enter the number? 20 The number is Abundant C program# include <stdio.h> # include <string.h> # include <stdbool.h> # include <math.h> int getSum(int n) { int sum = 0; for (int i=1; i<=sqrt(n); i++) { if (n%i==0) { if (n/i == i) sum = sum + i; else { sum = sum + i; sum = sum + (n / i); } } } sum = sum - n; return sum; } bool checkAbundant(int n) { return getSum(n) > n; } int main() { int n; printf("Enter the number?"); scanf("%d",&n); checkAbundant(n)? printf("The number is Abundant.\n") : printf("The number is not Abundant.\n"); return 0; } Output: Enter the number? 34 The number is not Abundant. Python Program:def is_abundant(n): fctr_sum = sum([fctr for fctr in range(1, n) if n % fctr == 0]) return fctr_sum > n a = int(input("Enter the number?")); if is_abundant(a): print("The number is Abundant."); else: print("The number is not Abundant.") Output: Enter the number?24 The number is Abundant. C# programusing System; public class AbundantNumber { public static int getSum(int n) { 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); } } } sum = sum - n; return sum; } static bool Abundant(int n) { return (getSum(n) > n); } public static void Main() { Console.WriteLine("Enter the number?"); int n = Convert.ToInt32(Console.ReadLine()); if(Abundant(n)) { Console.WriteLine("The number is Abundant."); } else { Console.WriteLine("The number is not Abundant."); } } } Output: Enter the number? 67 The number is not Abundant. PHP Program<?php echo "Enter the number?"; $n=readline(); $k = abundant($n) ? "The number is Abundant." : "The number is not Abundant."; echo($k); function value($n) { $sum = 0; for ($i = 1; $i <= sqrt($n); $i++) { if ($n % $i == 0) { if ($n / $i == $i) $sum = $sum + $i; else { $sum = $sum + $i; $sum = $sum + ($n / $i); } } } $sum = $sum - $n; return $sum; } function abundant($n) { return (value($n) > $n); } ?> Output: Enter the number? 56 The number is Abundant.
Next Topic#
|