The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


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.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-19-2007, 10:38 AM
hnkplaya hnkplaya is offline
Novice
Join Date: Jun 2007
Posts: 7
iTrader: (0)
hnkplaya is on a distinguished road
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);
}
}

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by hnkplaya : 07-19-2007 at 11:19 AM.
Reply With Quote
  #2 (permalink)  
Old 07-19-2007, 10:42 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
That code is not Visual basic, its C++ isnt it?, please confirm what it really is and i can move to the correct area.
Reply With Quote
  #3 (permalink)  
Old 07-19-2007, 10:43 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Out of curiosity, why do you use for ... loops?
I think for this one, it's better if you use do .. while...

do something, while the user does not input -1 for example, or in your case 'q'

Quote:
That code is not Visual basic, its C++ isnt it?, please confirm what it really is and i can move to the correct area
I think it's C. wrong section eh?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by HelloWorld : 07-19-2007 at 11:13 AM.
Reply With Quote
  #4 (permalink)  
Old 07-19-2007, 11:16 AM
hnkplaya hnkplaya is offline
Novice
Join Date: Jun 2007
Posts: 7
iTrader: (0)
hnkplaya is on a distinguished road
yeah this is for Microsoft visual C. I wasnt to sure what to use so I just started off my program with for

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 07-19-2007, 11:18 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote:
Originally Posted by hnkplaya View Post
yeah this is for Microsoft visual C. I wasnt to sure what to use so I just started off my program with for
Should have posted here:
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

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to HelloWorld For This Useful Post:
admin (07-19-2007)
  #6 (permalink)  
Old 07-19-2007, 11:18 AM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 308
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
Yup, wrong section - admin, move it to the C++ forum and we'll answer it there
Reply With Quote
The Following User Says Thank You to ccoonen For This Useful Post:
admin (07-19-2007)
  #7 (permalink)  
Old 07-19-2007, 11:36 AM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
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.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #8 (permalink)  
Old 07-19-2007, 11:43 AM
hnkplaya hnkplaya is offline
Novice
Join Date: Jun 2007
Posts: 7
iTrader: (0)
hnkplaya is on a distinguished road
Quote:
Originally Posted by rpgfan3233 View Post
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.
when i type in the code everything is prime. My logic to the program is incorrect i believe because any number i type in it ends up to be prime. Also if the number is not prime it should have "#inputted composite (biggest divisible number)"

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #9 (permalink)  
Old 07-23-2007, 09:44 AM
hnkplaya hnkplaya is offline
Novice
Join Date: Jun 2007
Posts: 7
iTrader: (0)
hnkplaya is on a distinguished road
Quote:
Originally Posted by hnkplaya View Post
when i type in the code everything is prime. My logic to the program is incorrect i believe because any number i type in it ends up to be prime. Also if the number is not prime it should have "#inputted composite (biggest divisible number)"

Any suggestions on what i should do?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #10 (permalink)  
Old 07-23-2007, 11:33 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
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?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 05:48 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50