Thread: factor of integers trouble

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    16

    factor of integers trouble

    so basically im writing a program that output the positive factors of a integer that a user inputs....for the life of me i cant figure out how to get the program to output the positive integers i believe i need a mod but every way i tried to do it it just wont work... this is what i have so far. i tried user for statements and if statements but it just isnt output the correct numbers any help would be amazing! thanks.

    Code:
    #include <stdio.h>
    
    int main () {
        int integer;
        
        //This will take in the input of the integer
        do{    
        printf("Enter a positive integer greater than one.\n");
        scanf("%d", &integer);
        
        //This will print the appropriate invalid number message
        if (integer<=1)
         printf("Sorry, that input is not valid.\n");
    }
        //This will execute as soon as integer is greater than 1
        while (integer<2);
        
        
    
        
        
        
      system("PAUSE");
      return 0;
    }

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    What are you expecting it to output ?

    There isn't a single output statement past your input loop.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    16
    there isnt i know because i cant figure out a exact statement that will output the factors of whatever the user inputs for example if he inputs 10 itll print out 1,2,5,10 thats all i want it to do for now eventually after i get that down i want it to print the number of factors the number has the sum of the factors of the integer and the product of the factors, but i need to get the initial factors first i want to try the other stuff on my own before i pray for help lol. thanks!

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    16
    ok got it to print out proper outputs awesome ya! cool now where i start getting more irritated haha so check it out:

    now what im trying to do is get it to produce a count of the total number of factors any helpful hints ive been running through pages of sites trying to find anything that may help but nothing any help would be amazing even if its just something i should use

    Code:
    #include <stdio.h>
    
    int main () {
        int integer, factors, number_factors, sum, product;
        
        //This will take in the input of the integer
        do{    
        printf("Enter a positive integer greater than one.\n");
        scanf("%d", &integer);
        
        //This will print the appropriate invalid number message
        if (integer<=1)
         printf("Sorry, that input is not valid.\n");
    }
        //This will execute as soon as integer is greater than 1
        while (integer<2);
    
        //This will print out the factors    
        for(factors=1;factors<=integer;factors++){
          if(integer % factors == 0)
             printf("%d ",factors);
    }
        //This will go to the next line
        printf("\n");
        
        
        
      system("PAUSE");
      return 0;
    }

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Code:
    int n = 0;
    for(factors=1;factors<=integer;factors++){
          if(integer &#37; factors == 0) {
             printf("%d ",factors);
             n++;
          }
    }
    printf ("There are %d factors", n);
    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. Integers into array.
    By livestrng in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 11:35 PM
  4. Replies: 6
    Last Post: 08-04-2003, 10:57 AM
  5. if-else prob..very basic so sorry to trouble u guys
    By bugeye in forum C Programming
    Replies: 2
    Last Post: 01-26-2002, 08:55 AM