Search This Blog

Sunday, December 12, 2010

MENU BASED PROGRAM: SUM OF FACTORS OF A NUMBER, CHECK IF NO. IS PERFECT OR NOT - USING SWITCH CASE & TAKING INPUT FROM THE USER

import java.io.*;
class NumberProblems
{
    int SumOfFactors(int n)
    {
        int s=0;   //to store the sum of all the factors
        int i;
        for(i=1;i<n;i++)
        {
            if(n%i==0)
            {
                s=s+i;
            }
        }
        return s;
    }


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

        if(s==n)    //condition for checking whether the number is perfect or not
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    void perfectNosBelow(int lim)
    {
        int n,i,s;
        for(n=1; n<lim; n++)
        {
            s=0;
            for(i=1;i<n;i++)
            {
                if(n%i==0)
                {
                    s=s+i;
                }
            }

            if(s==n)
            {
                System.out.print(n+"=(");
                for(i=1;i<n;i++)            //additional loop to print factors in the given format
                {
                    if(n%i==0)
                    {
                        System.out.print(i+", ");
                        if(i!=3)
                        {
                            System.out.print(",");
                          }
                    }
                }
                System.out.println(")"); //backspace
            }
        }
    }


    void m() throws IOException
    {
       
        System.out.println("***********************MENU***************************");
        System.out.println("Enter 1 to calculate sum of all the factors of a number");
        System.out.println("Enter 2 to check if a number is perfect or not");
        System.out.println("Enter 3 to find all perfect numbers below a limit");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));   //required for taking input
        int ch=Integer.parseInt(br.readLine());
        switch(ch)
        {
           

        case 1:

        System.out.println("\n Enter the number for which you require the sum of all its factors \n");
        int num=Integer.parseInt(br.readLine());       
        int s=SumOfFactors(num);
        System.out.println("\n Sum of factors of " +num +" is = "+s);
        break;
       
        case 2:       
        System.out.println("\n Enter the number for which you need to check if it is perfect or not \n");
        int number=Integer.parseInt(br.readLine());
        boolean ans=isPerfect(number);
        if(ans==true)
        {   
            System.out.println("\n Number " +number+ " is perfect");
        }
        else
        {
            System.out.println("\n Number " +number+ " is not perfect");
        }
        break;
       
        case 3:

        System.out.println("\n Enter the limit below which you need to find all the perfect numbers \n");
        int limit=Integer.parseInt(br.readLine());
        perfectNosBelow(limit);
        break;
       
        default:
        System.out.println("Wrong choice");
}   
    }
}


Output:
***********************MENU***************************
Enter 1 to calculate sum of all the factors of a number
Enter 2 to check if a number is perfect or not
Enter 3 to find all perfect numbers below a limit
1

 Enter the number for which you require the sum of all its factors

6

 Sum of factors of 6 is = 6

No comments:

Post a Comment