Skip to main content

Abundant Number Program Solution (Java) | Recursive Technique | ISC level

 /**

 * Write a Java program to print all the possible Abundant numbers (integers) between

M and N ( 1 > M,N < 100000) .

In number theory, an abundant number is a number for which the sum of its proper

divisors are greater than the number itself.

The first few abundant numbers are:

12, 18, 20, 24, 30, 36, 40, 42, 48, 54, 56, 60, 66, 70, 72, 78, 80, 84, 88, 90, 96, 100, 102,...

The integer 12 is the first abundant number. Its proper divisors are 1, 2, 3, 4 and 6

for a total of 16.

 */

import java.util.*;

public class AbundantNum

{

    public static int factors(int x, int f) //recursive function to find the sum of factors of the number

    {

        if(x==f)

            return 0;

        else if(x%f==0)

            return f+factors(x,f+1);

        else

            return factors(x,f+1);

    }


    public static void main()

    {

        Scanner sc=new Scanner(System.in);

        System.out.print("\nEnter the lower limit : ");

        int M=sc.nextInt();

        System.out.print("\nEnter the upper limit : ");

        int N=sc.nextInt();

        if(M>1 && M<100000 && N>1 && N<100000)

        {

            for(int i=M;i<=N;i++)

            {

                if(factors(i,1)>i)

                    System.out.print(i+", ");

            }

        }

        else

        {

            System.out.println("OUT OF RANGE");

            main();

        }

    } // end of main

} // end of class

Comments

Popular posts from this blog

Finding Factorial of a number | C programming

  Q.6      WAP in C/ C++/ java to find the factorial of the number. Solution :  #include <stdio.h> void   main () {      int   n ;      printf ( " \n Enter any positive number : " );      scanf ( " %d " ,  & n );      if  ( n < 0 )     {          printf ( " \n Invalid input! Enter the value again -->" );          main ();  // calls the main() function again     }      else     {          int   fact = 1 ;          for ( int   i = 1 ;  i <= n ;  i ++ )         {  ...

Neon Number | C programming

 Q.8     WAP in C/ C++/ java to check whether the number is a Neon number or not. Input: 9 Output: Neon Number Explanation: the square is 9*9 = 81 and the sum of the digits of the square ( 8+1) is 9. Solution :  #include <stdio.h> int   sumofdigits ( int  x)  // function to compute sum of digits {      int   sum = 0 ;      while (x > 0 )     {          sum += x % 10 ;         x = x / 10 ;     }      return   sum ; } int   main () {      int   n ;         printf ( " \n Enter any positive number : " );      scanf ( " %d " ,  & n );      if  ( n < 0 )     { ...

Harshad Number | C programming

  Q.10     WAP in C/ C++/ java to check whether a number is a  Harshad Number  or not.  In recreational mathematics, a Harshad number in a given number base is an integer that is divisible by the sum of its digits when written in that base.         Example: Number 200 is a Harshad Number because the sum of digits 2 and 0 and 0 is 2(2+0+0) and 200 is divisible by 2. Number 171 is a Harshad Number because the sum of digits 1 and 7 and 1 is 9(1+7+1) and 171 is divisible by 9.     Solution :  #include <stdio.h> int   sumofdigits ( int  x)  // function to compute sum of digits {      int   sum = 0 ;      while (x > 0 )     {          sum += x % 10 ;         x = x / 10 ;     }      ...