This is a program that will take int values entered at the command line, determine the prime and composite values, store them each in a list then print each list
This program was above and beyond anything I “needed” to do but I wanted to have it in my portfolio. It covers a lot of attributes. Doubly linked lists, pointers, libraries, selection etc.
#include<stdlib.h>
#include"doubly.h"
//This is a program to determine all the prime and and composite numbers between 2 and a user defined positive integer
// Primes and composites will be be stored in two seperate linked lists
// This program could technically test any group with an upper bound as high as the highest range of unsigned int
// but it seems to have issues with extremely high numbers
int main()
{
unsigned int x, i, value, primeFlag = 0; // x,i, and primeFlag are for my nested for loop(testing for prime/comp)
List *myListPrime, *myListComp;// A list for each type.
Node *tmp;// only purpose is to allow access to the functions in my doubly library, not needed for anything else in this instance
myListPrime = (List *)malloc(sizeof(List));// mallocing the lists
myListComp = (List *)malloc(sizeof(List));
myListPrime->start = myListPrime->end = myListComp->start = myListComp->end = tmp = NULL;// Starting them off as empty
printf("Please enter a whole number value greater than 2: ");
scanf("%ud", &value);
for(x=2; x<=value; x++)// nitty gritty, a nested for loop for testing, this goes from x until we reach the user entered value
{
for(i=2; i<=x; i++)// this takes each number from 2 until user entered value and then tests it with each integer from 2 until itself
{
if(x % i == 0 && i != x)// this is how I test for composite, if any value has no remainder when divided by any lesser value
{ // then it is a proven composite
newNode(myListComp, tmp, x);// so the value is added to the comp list
primeFlag = 0;// this prevents the comp value from being added to the prime list
break;// this stops the loop as there is no need to continue once value is proven as comp
}
else
{
primeFlag = 1;// if there is remainder then we set primeFlag = to 1, as there is a POSSIBILITY it is a prime
}
}
if(primeFlag == 1)// if the entire loop finished without reaching break then that means it must be prime
{
newNode(myListPrime, tmp, x); // add value to prime list
}
}
printf("Composites: \n"); // these next lines now print the composite list and the prime list
printList(myListComp, tmp);
printf("Primes: \n");
printList(myListPrime, tmp);
return (0);
}