GTU OOP Program - 8

8). 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.

import java.util.Scanner;

class Program_08 {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter an integer: ");
        int num = input.nextInt();
        int index = 2; // Num to test as factors

        // finding all the smallest factors
        while (num / index != 1) {
            // Test as a factor of num
            if (num % index == 0) {
                System.out.print(index + ", ");
                num /= index;
            } else {
                index++;
            }

        }
        System.out.println(num + ".");
    }
}

Output
cse gtu, gtu study material for computer engineering students gujarat technological university oop lab manual solution

gtu oop lab manual,

oop GTU Practical,

oop Programs,

Object oriented Programming,

Gtu study material 3140705,

OOP 3140705,

oop practical list,

Java Programming,

gtu oop practical,

oop lab solution gtu

Comments

YouTube

Popular posts from this blog

GTU OOP Program - 18

GTU OS Program - 7

GTU OS Program - 3