![]() |
|
#1
|
|||
|
|||
Program showing splitterWrite a program that loads an integer and print out all numbers that is a splitter to the integer.
Example: Enter a number: 12 Integer splitter is: 1 2 3 4 6 12 Thank you in advance. It should preferably be as simple as possible .. |
|||
|
#2
|
|||
|
|||
Re: Program showing splitterQuote:
I find the "splitter" nomenclature to be somewhat quaint. I assume that you mean to find all of the positive factors of a positive integer. Well, for a positive integer n you can make a loop with a counter that goes from 1 up to and including n. Each time through the loop you see whether n divided by the loop counter has a remainder of zero. If it does, then print out that counter value. In C (and C++) for positive integers, the "%" operator gives the remainder of a division of the first number by the second. Bottom line: The expression n % i gives a value of zero if (and only if) i is a factor of n. Quote:
What the heck does that mean? There is a famous quote attributed to Albert Einstein: "Everything should be made as simple as possible, but not one bit simpler." Not knowing what you are required to use in your program (or not allowed to use) makes it pretty difficult to assess the meaning of "simple as possible," since we have no context. So what's your point? Regards, Dave Footnote: Since 1 and n are always factors of a positive integer n, your program could check to make sure that n is greater than 1. (If it is equal to 1, then print out "1" and quit.) If n is greater than 1, then print "1", make the loop counter go from 2 through n-1, printing factors as they are found. Then, finally, print the value of n . This program is more desirable from some points of view, and never performs math for which results are known ahead of time. Is it "simpler"? I mean that it may or may not be desirable from a teaching point of view (in general I think it is very wasteful making a program perform unnecessary tasks like dividing by 1) but "simplicity," like "beauty," may very well lie in the eye of the beholder. Last edited by davekw7x : 11-Jan-2009 at 12:34.
|
|
#3
|
||||
|
||||
Re: Program showing splitterCPP / C++ / C Code:
Output: Code:
MxB |
Recent GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Equation solver | RazoR | C Programming Language | 3 | 18-May-2008 10:24 |
| Two-Tier data dissemination code installation problem | nidhibansal1984 | Computer Software Forum - Linux | 6 | 16-Sep-2007 11:13 |
| BOOKEEPING program, HELP!! | yabud | C Programming Language | 10 | 17-Nov-2006 04:48 |
| Help with a complex program | lordfuoco | C++ Forum | 5 | 24-Jun-2006 07:03 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The