Wednesday, July 8, 2020

Write a program to input a number and find it’s all Prime Factor and print it?(12==2,2,3….all factors are prime)

43)  Write a program to input a number and find it’s all Prime Factor and print it?(12==2,2,3….all    factors are prime)

import java.util.*;
public class Prime_factor
{
    public static void main(String args[ ])
   {
      Scanner sc = new Scanner (System.in);
      System.out.println("Enter a number");
      int a = sc.nextInt();
      int t = a/2;
      System.out.print("PRIME FACTOR of "+a+" = ");
 if(a==1)
 {
   System.out.println(a);
 } 
     
      for(int i = 2;i<=t;)
      {
          if(a%i==0)
          {
             System.out.print(i+" ");
             a=a/i;
          }
          else
          {
             i++;
          }
      }
 
   }
   }

Output : 

No comments:

Post a Comment

if you have any doubts, Please let me know.
Your Truly
Shashank Porwal

 jhvghvgk ;nkjn ,nkjhj