Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,665 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,161 people online right now. Registration is fast and FREE... Join Now!




Please revise my program

 
Reply to this topicStart new topic

Please revise my program

hnkplaya
post 19 Jul, 2007 - 09:59 AM
Post #1


New D.I.C Head

*
Joined: 19 Jul, 2007
Posts: 1


My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

PennyBoki
post 19 Jul, 2007 - 10:45 AM
Post #2


system("revolution");

Group Icon
Joined: 11 Dec, 2006
Posts: 2,009



Thanked 5 times

Dream Kudos: 500

Expert In: Java,C++,C

My Contributions


Hi


QUOTE
int prime(n);

is not working
should be
CODE
int prime(int n);



QUOTE
int i=1;
why i is set to 1?

your for loop will only go ones so it just as same as if you have no for loop.


a while loop is empty that means in this case that it'll never stop looping, so
your program will be at dead end i.e. it'll never finish.


an if statement is also empty.

the while loop is at the end is somewhat ok, so what if the user just enters chars different than q


You need to work a little more harder to improve the code and it's logic.
User is offlineProfile CardPM

Go to the top of the page

madgundam
post 19 Jul, 2007 - 03:46 PM
Post #3


New D.I.C Head

*
Joined: 19 Jul, 2007
Posts: 2


My Contributions


as said by PennyBoki. Many 'while loop' will go into infinite loop. You really need to develop the logic first and then go for coding just using some loops and conditions wont help you.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 05:49AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month