I have written a program that determines whether numbers are prime or composite.
ID: 3543697 • Letter: I
Question
I have written a program that determines whether numbers are prime or composite. I have to add a few features to it. I need to:
display largest prime number that divides composite value evenly
display prime factors of a composite value
display largest integer factor set of a composite value
use at least three additional functions
Here is my code so far:
#include <stdio.h>
void main() {
int input1 = 0, output1, quotient;
int output2, quotient2;
void largest_prime_evenly(int comp) {
do {
comp = quotient2
quotient2 = quotient2 - 1;
output2 = comp % quotient2;}
while (output2 != 0 );
if (output2 = 0) {
printf("The largest prime number that divides this number evenly is %d", output2);
}
}
void prime_factors_composite(int);
printf("This program tells you if a positive integer is prime or composite. Enter a number: ");
scanf("%d", &input1);
while(input1 != -1) {
if(input1 > -1) {
quotient = input1;
do {
quotient = quotient - 1;
output1 = input1 % quotient;
} while (output1 != 0);
while (output1 = 0 || quotient > 1) {
printf("This number, %d, is Composite. Its greatest common multiple is %d. ", input1, quotient);
comp = largest_prime_evenly(output1);
break;
}
while (output1 == 0 || quotient == 1) {
printf("This number is PRIME. ");
break;
}
}
else{
printf("You entered a negative number. ");
}
printf("Type -1 to stop or type another number to continue ");
scanf("%d", &input1);
}
}
Explanation / Answer
// I guess you want something like this..... Try running this code in your machine
#include <stdio.h>
int IsPrime(unsigned int number) {
if (number <= 1) return 0; // zero and one are not prime
unsigned int i;
for (i=2; i*i<=number; i++) {
if (number % i == 0) return 0;
}
return 1;
}
int get_next_prime(int x) {
while(1) {
x++;
if(IsPrime(x))
return x;
}
}
void get_factors(int x) {
int i = 2;
while(1) {
if(x==1) break;
if(x%i == 0) {
printf("%d ",i);
x /= i;
}
else
i = get_next_prime(i);
}
printf(" ");
return;
}
int main() {
int n;
printf("Enter n: ");
scanf("%d",&n);
if(IsPrime(n))
printf("%d is prime ",n);
else {
printf("%d is composite and it's factors are: ",n);
get_factors(n);
}
return 0;
}