OOP-JAVA Practical 8
- Aim: Write a program that reads an integer and displays all its smallest factors in increasing order. For example if input number is 120, the output should be as follows:2,2,2,3,5.
- Code:
import java.util.*; public class practical8{ public static void main(String args[]){ Scanner s=new Scanner(System.in); System.out.println("Enter the Positive Integers:"); int a=s.nextInt(); System.out.println("Smallest factors are:"); int i=2; while(i<=a){ if(a%i==0){ if(a==i){ System.out.println(i+"."); } else{ System.out.print(i+","); } a=a/i; } else{ i++; } } } }
- Output:
Comments
Post a Comment