C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to determine whether a given number is a Deficient numberThe deficient number can be defined as the number for which the sum of the proper divisors is lesser than the number itself. For example, the number 21 with its proper divisors (1, 3 and 7) has sum (11) lesser than itself. AlgorithmMAIN
Def(n)
divsum(n)
Java Programimport java.util.*; class Deficient { static int divsum(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); } } } return sum; } static boolean isDef(int n) { return (divsum(n) < (2 * n)); } public static void main(String args[]) { System.out.println("Enter the number?"); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if (isDef(n)) System.out.println("The number is deficient."); else System.out.println("The number is not deficient"); } } Output: Enter the number? 23 The number is deficient. C Program#include<stdio.h> #include<math.h> int divsum(int n) { int i; 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); } } } return sum; } int isDef(int n) { return (divsum(n) < (2 * n)); } void main() { char var[100]; int n; printf("Enter the number?"); scanf("%d",&n); if(isDef(n) == 0) printf("The number is not deficient."); else } Output: Enter the number? 21 The number is deficient. Python Programimport math def divsum(n) : sum = 0 i = 1 while i<= math.sqrt(n) : if (n % i == 0) : if (n / i == i) : sum = sum + i else : sum = sum + i; sum = sum + (n / i) i = i + 1 return sum def isDef(n) : return (divsum(n) < (2 * n)) n = int(input("Enter the number?")) if ( isDef(n) ): print("The number is deficient.") else : print("The number is not deficient.") Output: Enter the number? 45 The number is deficient. C# Programusing System; class Deficient{ static int divsum(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); } } } return sum; } static bool isDef(int n) { return (divsum(n) < (2 * n)); } public static void Main() { Console.WriteLine("Enter the number?"); int n = Convert.ToInt32(Console.ReadLine()); if(isDef(n)) { Console.WriteLine("The number is deficient."); } else { Console.WriteLine("The number is not deficient."); } } } Output: Enter the number? 45 The number is deficient. PHP Program<?php function divsum($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); } } } return $sum; } function isDef($n) { return (divsum($n) < (2 * $n)); } echo "Enter the number?"; $n=readline(); $ds = isDef(n) ? "The number is defecient." : "The number is not deficient."; echo($ds); ?> Output: Enter the number? 45 The number is deficient.
Next Topic#
|