HELP!

L

luis guballo

Please edit the codes the I've written here for me to have the correct answer I need. Anyways this program aims to give all the factors of a given positive integer. Example, I entered 6. Then it should output 1,2,3 as its factors.

Here's the code:

#include <stdio.h>
#include <conio.h>
int main ()
{
int num, rem, factor = 1, ctr = 1;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0 )
printf("The number you have entered is invalid");
if (num == 0)
printf("The factor is %d", factor);
else
rem = num % ctr;
if (rem = 0)
for(ctr = 1; ctr <= num; ctr++)
{
factor = num / ctr;
printf("The factor: %d", factor);
}
else
ctr = ctr + 1;
getch();
return 0;
}
 
E

Eric Sosman

Please edit the codes the I've written here for me to have the correct answer I need. Anyways this program aims to give all the factors of a given positive integer. Example, I entered 6. Then it should output 1,2,3 as its factors.

I'll help with your homework, but I won't write it for you.
Here's the code:

#include <stdio.h>
#include <conio.h>

Get rid of this; it's not C.
int main ()
{
int num, rem, factor = 1, ctr = 1;
printf("Enter a number: ");
scanf("%d", &num);

Check the value returned by scanf(), which tells you how
many numbers it managed to convert. For example, if the user
responds to "Enter a number: " by typing "forty-two", scanf()
will not understand it and will return 0 to tell you that it
couldn't convert anything. You shouldn't go on to check `num'
until you're sure a value has actually been stored in it.

(General note: Although scanf() looks fairly straightforward,
it is startlingly difficult to use for interactive input. Use
it in "toy" programs like this one, but don't expect to be able
to go much further with it.)
if (num < 0 )
printf("The number you have entered is invalid");

... and after this test, the program runs happily onward
with the invalid `num'. Perhaps you should halt the program
here if the test fails.
if (num == 0)
printf("The factor is %d", factor);
else
rem = num % ctr;

Here's where the serious difficulty begins. It looks like
you want to test whether `num' is divisible by `ctr', and keep
on trying successively larger `ctr' values. But the divisibility
check is outside the loop that increases `ctr', so it's only made
for the very first `ctr' value (and always says "Yes, 1 is a
factor." You need to reorganize the code so that it checks each
new `ctr' value for divisibility as it's encountered.
if (rem = 0)

Here's another problem: The `=' and `==' operators mean
different things. As written, this line sets `rem' to 0 and
then says "The test always comes out false."
for(ctr = 1; ctr <= num; ctr++)
{
factor = num / ctr;

Since this loop never executes (because of the faulty `if'
above) you haven't seen that it doesn't work. I'm not sure just
what you want it to do; perhaps it's a confused attempt at a
divisibility test.
printf("The factor: %d", factor);
}
else
ctr = ctr + 1;
getch();

Get rid of this; it's not C.
return 0;
}

General outline of a solution to your homework problem:

Read a value
if (reading was unsuccessful) {
emit a complaint
} else if (the value is invalid) {
emit a complaint
} else {
for (each candidate divisor) {
if (it divides the value evenly) {
print it
}
}
}
 
O

osmium

luis guballo said:
Please edit the codes the I've written here for me to have the correct
answer I need. Anyways this program aims to give all the factors of a
given positive integer. Example, I entered 6. Then it should output 1,2,3
as its factors.

I suspect that what your instructor really wants is *prime* factors,
although your summary does not say that. 4 is a factor of 20, but not a
prime factor.
 
K

Keith Thompson

osmium said:
I suspect that what your instructor really wants is *prime* factors,
although your summary does not say that. 4 is a factor of 20, but not a
prime factor.

Perhaps, but showing all the factors of a number is a reasonable exercise.
 
K

Keith Thompson

Eric Sosman said:
I'll help with your homework, but I won't write it for you.


Get rid of this; it's not C.

Sure it is. It's just not *portable* C.

[...]
Get rid of this; it's not C.

Sure it is. It's just not *portable* C.

The getch() function, declared in <conio.h>, is a Windows-specific
(also MS-DOS) that prompts the user to type <Enter> to continue.
It's convenient for certain Windows-based development environments
in which the easiest way to execute a program after you've compiled
and linked it creates a new terminal window that vanishes when the
program finishes. The output of a simple portable "Hello, world"
program, when executed in such an environment, will appear and vanish
so quickly you probably won't be able to read it. The "getch()"
call is a workaround for that.

The environment *should* provide a way to keep the window open after
the program terminates, so the program doesn't have to take any
special action. Unfortunately, as far as I know, typical Windows
development environments don't do this. (I could easily be mistaken
on this point.)

An alternative is to execute the program from a command prompt rather
than from within the IDE, but that's annoying; you have to figure
out where the IDE decided to put the executable, and then you have
to switch back and forth between the IDE and the command prompt.

Or you can call the standard getchar() rather than getch(), but (a) that
doesn't show a prompt, and (b) like calling getch(), it makes the
program wait for input even when it's not necessary.
 
K

Kenny McCormack

I suspect that what your instructor really wants is *prime* factors,
although your summary does not say that. 4 is a factor of 20, but not a
prime factor.

Maybe he is trying to find perfect numbers...

--
"The smart way to keep people passive and obedient is to strictly limit the
spectrum of acceptable opinion, but allow very lively debate within that
spectrum...."

- Noam Chomsky, The Common Good -
 
K

Keith Thompson

David Harmon said:
On Fri, 02 Aug 2013 11:04:55 -0700 in comp.lang.c, Keith Thompson


I think that might not be *precisely* what it does.
Carry on.

I believe you're right. Apparently it reads a single character from the
keyboard without echoing it. I confused it with system("pause"), which
is often used on Windows for a similar purpose.

Thanks for pointing out my mistake -- but telling us what it actually
does would have been even more useful.

(There's another function of the same name that's part of curses.)

A simple and portable getchar() call would serve the same purpose,
except that it typically requires you to type Enter, since stdin is
usually line-buffered.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top