Computer Science, asked by aliakhankh55, 5 months ago

Write a program to print the factors of each digit of an entered number.​

Answers

Answered by Anonymous
1

Explanation:

In the above program, number whose factors are to be found is stored in the variable number (60). The for loop is iterated until i <= number is false. In each iteration, whether number is exactly divisible by i is checked (condition for i to be the factor of number ) and the value of i is incremented by 1.

Answered by anindyaadhikari13
1

Question:-

Write a program to print the factors of each digit of an entered number.

Program:-

import java.util.*;

class Prime_Factors

{

static void primeFactors(int n)

{

System.out.print(n+"=");

for(int i=2;n!=0;i++)

{

if(n%i==0)

System.out.print(i);

if(n/i!=1)

System.out.print(" x ");

n/=i;

i--;

}

}

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

System.out.print("Enter a number: ");

int n=sc.nextInt();

while(n!=0)

{

int d=n%10;

printFactors(d);

n/=10;

}

}

}

I have created a method that displays the prime factors and called them inside the loop.

d variable is used for extracting the digits of the number.

Now, let's understand how I have calculated the prime factors. For these, check the code.

static void primeFactors(int n)

{

System.out.print(n+"=");

for(int i=2;n!=0;i++)

{

if(n%i==0)

System.out.print(i);

if(n/i!=1)

System.out.print(" x ");

n/=i;

i--;

}

}

Consider a number,say 4.

In first iteration of the loop,

i=2

n%i=0(since 4%2==0)

So, 2 is a prime factor.

2 gets printed.

Now,

4/2=2

i-- =1

Again, i gets Incremented after first iteration,so i=2

Now,

2%2==0

So,

2 gets printed.

2/2=1

Hence,

4=2x2

In this way, the program works.

Similar questions