|
This is what my instructor has told me to do.
For this project, you will read in values from the user until he enters the letter ‘q’. Each parameter should be a positive integer, but if the user enters a value that is not a positive integer, you should tell him that your program only accepts positive integers, then prompt him to enter a number. (See example below)
If the user enters a positive integer, you will determine whether or not the number is prime or composite. A number is defined to be prime if the only divisors of that number are 1 and itself. If a number is not prime, then it is composite. In other words:
Assuming n,d Î Z
Prime(n) = false => n/d element Z where d Element {1, n}
Prime(n) = true otherwise
Your program should test the primality of each number on the command line. The output should be the number followed by the word “PRIME” or “COMPOSITE” depending on whether the number is prime or composite. Also, if the number happens to be composite, your program should also print out the largest integer, other than 1 or n, that divides the number evenly.
In addition, you need to use at least 3 functions in your program. You can make the functions do whatever you would like, but you must have at least 3.
Here is a possible execution of your program. The user input is bolded.
Please enter a positive integer: 10
10 COMPOSITE 5
Please enter a positive integer: 7
7 PRIME
Please enter a positive integer: a
‘a’ is not a positive integer.
Please enter a positive integer: 93
93 COMPOSITE 31
Please enter a positive integer: 101
101 PRIME
Please enter a positive integer: q
Thank you for using my program."
This is what i have created so far:
#include <stdio.h> #include <math.h> #include <string.h> int prime(n); void main () {
int n; int d; int z; int i=1; char ch;
printf ("Enter a positive integer:"); scanf ("%d", &n);
for (d = 1; d <= i; d++) { z=0; printf ("%d Prime",n); } while (i <= n){ } if (i%d==0) { } if(z==2){ printf("%d",i); i++; } else { printf ("%d composite\n", n, n+1); }
printf ("\nPress 'q' to exit\n"); printf ("\nThank you for using my program"); scanf("%c", &ch); while (ch != 'q') { scanf("%c", &ch); } }
This post has been edited by hnkplaya: 19 Jul, 2007 - 10:17 AM
|