simple factoring help..

This is a discussion on simple factoring help.. within the C++ Programming forums, part of the General Programming Boards category; Hi there i can't get this simple factoring program to work it work's but it only show one factor of ...

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49

    simple factoring help..

    Hi there i can't get this simple factoring program to work it work's but it only show one factor of a number anyone there i can't get it any help there?? tnx and btw tnx salem for the hints and for correcting my code in translate thread tnx... here's the code..

    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main()
    {
    int num;
    cout << "input num\n";
    cin >> num;
    for(int a=1; a<=num; a++){
    for(int b=1; b<=a; b++){
    if(a*b==num){
    cout << a << " and " << b << " is the factor of " << num;
    }
    }
    }
    getch();
    }
    I really can't get it.....
    Last edited by limitmaster; 08-16-2006 at 06:20 AM.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    Code:
    for(int b=1; b<=num; a++)
    that should be b++
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    Sorry i didn't see that but anyway tnx......^^
    I have another problem the factor show's the number the same the output is like this.....

    Code:
    20
    
    1 and 20 is the factor of 20
    2 and 10 is the factor of 20
    4 and 5 is the factor of 20
    4 and 5 is the factor of 20 ----------------> repeated
    2 and 10 is the factor of 20-----------------> repeated
    1 and 20 is the factor of 20----------------> repeated
    what should i do so it will not repeat anymore......tnx

    --------------------------------------------------------------------------------------------
    nvm the question above i think i got it

    Code:
    for(int a=1; a<=num; a++){
    for(int b=1; b<=a; b++){
    }}
    or is there another way to do this?? tnx^^
    Last edited by limitmaster; 08-16-2006 at 06:18 AM.

  4. #4
    Registered User Rennor's Avatar
    Join Date
    Aug 2006
    Location
    Finland
    Posts
    45
    I didnt crack into the core of your code, was just going to offer:
    Code:
    if( a*b == num && b <= a )
    But yours is better optimized since it gets "faster" in the end

  5. #5
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    okey tnx...^^

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    Another approach, without the need for two for loops is to use the modulos operator

    Code:
    int num;
    std::cout << "input num\n";
    std::cin >> num;
    for (int a=1; a!=num; ++a) {
        if( !(num%a) )
            std::cout << a << " and " << num/a << " is the factor of " << num << std::endl;
    }
    Edit: the only thing you need to take care off is if the number the user inputs is 1.
    Last edited by Mario F.; 08-16-2006 at 06:50 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    wow tnx^^........Im finding some other way to do it and im very thank you for teaching me the other way......^^ tnx again...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 10:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21