![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |||
| Need help with a problem I am trying to make a program that indicates prime numbers and this is what i have so far but i am not quite sure what the problem is. This is what the instructor has told me to make " 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 ÎZ where d Ï {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." AND THIS IS THE PROGRAM I HAVE WRITTEN 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); } } Last edited by hnkplaya : 07-19-2007 at 11:19 AM. |
| |
| ||||
| That code is not Visual basic, its C++ isnt it?, please confirm what it really is and i can move to the correct area. ![]() |
| ||||
| Quote:
http://www.programmerstalk.net/forum2.html Many C/C++ experts there, even I'm learning C++ too now ![]() But I hope I can catch it a little faster so that I can continue again with my lovely ASP.NET ![]() |
| The Following User Says Thank You to HelloWorld For This Useful Post: | ||
admin (07-19-2007) | ||
| |||
| Yup, wrong section - admin, move it to the C++ forum and we'll answer it there ![]() __________________ Day Cares | Golf Courses | Disc Golf Courses | Campgrounds | Ice Rinks | Paintball Fields | Dentists | Plastic Surgeons | Aging Jokes Catholic Churches | Lutheran Churches | Methodist Churches | Episcopal Churches | Clean Jokes |
| The Following User Says Thank You to ccoonen For This Useful Post: | ||
admin (07-19-2007) | ||
| |||
| Can you explain the problem a bit more? I understand that you don't know what the problem with the code is, but what does it do that is wrong? For example, what output do you get that you shouldn't be getting? Give some sample output, like the example that you included from your instructions. __________________ "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." -- Bjarne Stroustrup, creator of what is now known as C++ For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that. |
| |||
| Quote:
|
| |||
| Quote:
Any suggestions on what i should do? |
| ||||
| Sorry, what does this line do? Code: int prime(n); I personally never tried C programming out lol... It's kind of similar with C++ though (which is something that I'm doing right now lol...) I'm just wondering how to get the logic to figure out whether it's a composite, composite means that it has at least one factor right? |
![]() |
| Thread Tools | |
| Display Modes | |
| |