Find all factors of a number

This is a discussion on Find all factors of a number within the C Programming forums, part of the General Programming Boards category; I have to create a function that finds all factors of any particular number. This is what i've got so ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    Question Find all factors of a number

    I have to create a function that finds all factors of any particular number. This is what i've got so far, but it doesn't seem to work all the time. It only really divides the number you input by two, which only gives one factor of even numbers. Therefore, I'm almost positive that my problem exists in the () part of the for() loop.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    main()
    {
    	int i, a;
    	
    	printf("Input a number, I'll tell you its factors.\n");
    	scanf("%d", &a);
    
    	for(i = 2; i <= a; ++i)
    	{
    		if(a % i == 0)
    		{
    			printf("%d * ", a / i);
    		}
    		else
    			break;
    	}
    }
    Any help would be greatly appreciated.

  2. #2
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    double post, sorry.
    Last edited by glUser3f; 10-23-2003 at 12:48 AM.

  3. #3
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    the a % i part is correct, but why do print a/ i? because a %i == 0 this means that i is a factor of a, so you need to print i.
    And why break when (a % i != 0) ? You should continue the loop to check all the numbers that are smaller than a.
    One final note, you don't need <math.h> , this header file is for functions like sqrt and sin.
    HTH

  4. #4
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Or half the numbers smaller than 'a' rounded up to the nearest whole?
    Demonographic rhinology is not the only possible outcome, but why take the chance

  5. #5
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    Originally posted by Azuth
    Or half the numbers smaller than 'a' rounded up to the nearest whole?
    I was going to suggest this when he/she gets the program working well.
    Do not forget that 1 and a are factors of a too, so you need to print them when the loop starts/ends.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Simply remove the
    Code:
    else
        break;
    and start the for() loop at 1 to get glUser3f's suggestion about 1 and a
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to find maximum number
    By mutombo in forum C Programming
    Replies: 3
    Last Post: 02-27-2009, 03:36 AM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 10:21 PM
  4. Help with homework please
    By vleonte in forum C Programming
    Replies: 20
    Last Post: 10-13-2003, 11:16 AM
  5. Replies: 2
    Last Post: 08-03-2003, 10:01 AM

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