Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

rite a program that reads in an integer and decomposes it into its prime factors

ID: 3746259 • Letter: R

Question

rite a program that reads in an integer and decomposes it into its prime factors. The prime factors of a numbers are a group of prime numbers that when multiplied together equal that number. If the number of prime factors is greater than one, the program should place a comma between the prime factors (but no comma at the end). If the integer number is 1, the program should print out 1, if the number is zero or negative the program should print out 0. (A prime number is a number that is only divisible by 1 and itself, such as 2,3,5,7,13). To get full points, the program should include a boolean function called isprime that takes as parameter one integer and returns true/false depending on whether the number is a prime number or not. Dæmi um notkun /Examples: Input 765 24 Output 3, 3, 5, 17 2. 2, 2. 3

Explanation / Answer

We will write C code for it as folows:

#include<iostream.h>

int Isprime(int val)

{

int flag=0;

for(int i=0;i<=n/2;i++)

{

if(n%2==0)

flag=1;

}

if(flag==0)

return 1;//i.e prime number

else

return 0;

}

int main()

{

int num,k=0,flag=0;

int factors[50];

cin>>num;

if(num>0)

{

for(i=0;i<num/2;i++)

{

if(Isprime(i))

{

if(num%i==0)

{

factors[k++]=i;//store the prime number in factors

num=num/i;

flag=1;//we found the factor

}

}

}

if(flag==1)

{

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

cout<<factors[i]<<",";

}

}

else

{

cout<<0;//printing 0 for negative numbers and 0

}

}