GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Jan-2009, 08:53
Zurswe Zurswe is offline
New Member
 
Join Date: Jan 2009
Posts: 1
Zurswe is an unknown quantity at this point

Program showing splitter


Write 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  
Old 11-Jan-2009, 11:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Program showing splitter


Quote:
Originally Posted by Zurswe
...a program that loads an integer and print out all numbers that is a splitter to the integer.

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:
Originally Posted by Zurswe
It should preferably be as simple as possible

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  
Old 12-Jan-2009, 15:50
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Program showing splitter


CPP / C++ / C Code:
#include <iostream>

using namespace std;

void factors(const int i)
{
    if(i > 1)
    {
        cout << 1 << " ";
        for(int z = 2; z < i; z++)
        {
            if(i % z == 0)
            {
                cout << z << " ";
            }
        }
    }
    cout << i << endl;
}

int main()
{
    cout << "Enter an integer: ";
    int i;
    cin >> i;
    factors(i);

    return 0;
}


Output:

Code:
$ ./splitter Enter an integer: 14 1 2 7 14 $ ./splitter Enter an integer: 23 1 23 $ ./splitter Enter an integer: 13 1 13 $ ./splitter Enter an integer: 9 1 3 9 $ ./splitter Enter an integer: 12223 1 17 719 12223 $ ./splitter Enter an integer: 1212 1 2 3 4 6 12 101 202 303 404 606 1212 $ ./splitter 1444440 Enter an integer: 14440 1 2 4 5 8 10 19 20 38 40 76 95 152 190 361 380 722 760 1444 1805 2888 3610 7220 14440 $ ./splitter Enter an integer: 14445456 1 2 3 4 6 8 12 16 24 48 257 514 771 1028 1171 1542 2056 2342 3084 3513 4112 4684 6168 7026 9368 12336 14052 18736 28104 56208 300947 601894 902841 1203788 1805682 2407576 3611364 4815152 7222728 14445456

MxB
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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
Forum Jump

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

All times are GMT -6. The time now is 21:01.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.