Computer Science, asked by BrainlyProgrammer, 9 months ago

Wap to print the prime factor of a number.
Example:6
Prime Factors:2,3​

Answers

Answered by raiankush999
1

Answer:

for c/c++

# include <stdio.h>

# include <math.h>

  

// A function to print all prime factors of a given number n

void primeFactors(int n)

{

    // Print the number of 2s that divide n

    while (n%2 == 0)

    {

        printf("%d ", 2);

        n = n/2;

    }

  

    // n must be odd at this point.  So we can skip 

    // one element (Note i = i +2)

    for (int i = 3; i <= sqrt(n); i = i+2)

    {

        // While i divides n, print i and divide n

        while (n%i == 0)

        {

            printf("%d ", i);

            n = n/i;

        }

    }

  

    // This condition is to handle the case when n 

    // is a prime number greater than 2

    if (n > 2)

        printf ("%d ", n);

}

Answered by Oreki
2

import java.util.Scanner;

public class PrimeFactors {

   static void printPrimeFactors(int number) {

       System.out.print("Prime factors - ");

       for (; number % 2 == 0; number /= 2)

           System.out.print(2 + " ");

       for (int i = 3; i < Math.sqrt(number) + 1; i += 2)

           for (; number % i == 0; number /= i)

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

       if (number > 2)

           System.out.print(number);

   }

   public static void main(String[ ] args) {

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

       printPrimeFactors(new Scanner(System.in).nextInt( ));

   }

}

Attachments:
Similar questions