Computer Science, asked by dhanviii13, 2 months ago

Class PrimeFac contains an array of 50 integers. Some os the members of the class are as under:
Data Members:
num[ ] array to store integers
Freq[ ] array to store the frequency of prime factors of
Numbers
Functions: PrimeFac()
constructor to initialize the array elements to O
void enter()
to enter the values in array num[ ]
void frefac()
to determine the frequency of prime factors of the
numbers stored in num[] and assign it to freq[ ]
void disp( )
to display both the arrays
There is no need to make the main function.​

Answers

Answered by LeelaGhosh1
5

Required Program -

import java.io.*;

public class PrimeFac{

int num[]; /*Array for numbers is declared.*/

int Freq[]; /* Array for frequency of prime factors.*/

PrimeFac(){ //required default constructor

num = null; //Arrays are initialized with "null" .

Freq = null;

}

void enter() throws IOException{ /*InputStreamReader and Buffered Reader are used. */

InputStreamReader ir = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(ir);

num = new int[50] ; //Size is assigned to the array.

System.out.println("Enter 50 numbers one-by-one on different lines:");

for(int i = 0; i<50; i++)

num[i] = Integer.parseInt(br.readLine());

}

void frefrac(){

Freq = new int[50];

int c = 0; //To count frequency of prime factors for each number.

int n = 0; //To store each number from the array.

int primechk = 0; /*To count number of factors for each factor of each     number and to see if it is a prime factor or not.*/

for(int i = 0; i<50; i++){

n = num[i];

for(int j = 2; j<=n; j++){ /*Loop for finding number of prime factors of each number.*/

if(n%j==0){ //If j is a factor of the number.

for(int k = 1; k<=j;k++){ /*Loop for checking if the factor of the number is prime or not.*/

if(j%k==0) //If k is a factor of j.

primechk++; //Counter increases by one.

}

if(primechk == 2){ /*If the factor of the number is prime.*/

primechk = 0; /*Variable for checking prime is re-initialized with zero. */

for( /*No initialization*/ ; n%j==0; c++ )  /*Loop to count number of times a prime factor can divide the number.*/                                                                 n = n/j; /*Until the number is no longer divisible by that prime factor.*/

}  

}

/*Now, the frequency of the current prime factor is recorded in c and the control moves to the other prime factor.*/

}

Freq[i] = c; //Frequency of prime factors is stored.

c=0; /*Counter Variable for number of prime factors is re-initialized to zero.*/

}

}

void disp(){

System.out.println("The numbers are:");

for(int i = 0; i<50;i++)

System.out.print(num[i] + " , ");

System.out.println(""); //To move to next line.

System.out.println("The frequencies of prime factors of the numbers are:");

for(int j = 0; j<50; j++)

System.out.print(Freq[j] +" , ");

}

}

Algorithm -

=> An object of PrimeFac is created and the arrays num[] and Freq[] are initialized with null.

=> When enter() is invoked using the Object an array size of 50 is assigned to num[] and the 50 numbers are accepted and stored .

=> When frefrac() is invoked using the Object the array Freq[] is assigned with a size of 50 for storing the frequency of prime factors. Now -

  • Each number from num[] is stored in n and its factors are found using a loop.
  • For each factor, number of divisors are found and stored in primechk.
  • If number of divisors for the factor is 2 then it is a prime factor, primechk is now re-initialized with zero.
  • Another loop is used to continue to divide the number by the prime factor found until no longer divisible
  • the number of times it could be divided is stored in variable c
  • the loop moves to the next factor and repeats the process,
  • the total number of prime factors for the number is recorded and stored in the current cell of the array Freq[]
  • finally, the variable c is reinitialized with 0 and the loop moves to the next number from array num[] .

=> When disp() is invoked, the elements from both num[] and Freq[] are printed.

Note -

=> As no main method is present, this program will only work on IDE'S like bluej.

=> An object of PrimeFac will have to be manually created and the methods will have to be invoked by clicking on them one by one.

=> If the number is itself prime, then it can only have one prime factor i.e. itself and its frequency will be recorded as 1 .

=> I have used InputStreamReader and BufferedReader classes from java.io for input on the terminal window but you can also use the Scanner class .

=> Always use this order for invoking the methods after creating the object

  1. enter()
  2. frefrac()
  3. disp()

Output -

=> Refer to the attachments for output and other screenshots.

Attachments:
Answered by sumansen18
0

Answer:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

Similar questions