Question

Find the factorial of a number in given range using recursion.

				
					ENTER THE LOWER RANGE
1
ENTER THE UPPER RANGE
5
FACTORS OF 1-->1 
FACTORS OF 2-->1 2 
FACTORS OF 3-->1 3 
FACTORS OF 4-->1 2 4 
FACTORS OF 5-->1 5 
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class factors
{
    long lowerRange=0,upperRange=0,i=0;
    public void accept()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER THE LOWER RANGE");
        lowerRange=sc.nextLong();
        System.out.println("ENTER THE UPPER RANGE");
        upperRange=sc.nextLong();

    }

    public void printFactors(long num,long i)
    {
        if(i<=num)
        {
            if(num%i==0)
            {
                System.out.print(i+" ");

            }
            printFactors(num,i+1);
        }

    }

    public void display()
    {
        for(i=lowerRange;i<=upperRange;i++)
        {
            System.out.print("FACTORS OF "+i+"-->");
            printFactors(i,1);
            System.out.println();
        }
    }

    public static void main()
    {
        factors ob1=new factors();
        ob1.accept();
        ob1.display();

    }
}  



				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *