Thread: Factorizing Algorithm

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    1

    Factorizing Algorithm

    hi! need advice with this task

    Write a function that receives an integer and if the integer is larger than 1, it print out its factors.
    The function prototype will look like: void printFactor (long unsigned int number);
    For example, if number is 24, this function should print 2 2 2 3

    For any prime number like 13, it should only print the number itself.

    Use this function to write a code that gets a number from the user and prints its factors until the user enters 0.

    A sample run of your code should look like:

    Please enter a positive integer number (0 to end): 12
    Factors of 12 are: 2 2 3

    Please enter a positive integer number (0 to end): 1000
    Factors of 1000 are: 2 2 2 5 5 5

    Please enter a positive integer number (0 to end): 17
    Factors of 17 are: 17

    Please enter a positive integer number (0 to end): 81
    Factors of 81 are: 3 3 3 3

    Please enter a positive integer number (0 to end): 0
    Thanks and have a good day!

    I understand the main idea (attached picture) but I stacked.

    started with a first loop, but I only have an endless loop
    Code:
    #include<stdio.h>
    
    int main(void)
    {
    int i;
    int number =8;
    
    
        for(i=2; i<number; i++)
            while(number%i==0)
            printf("\n%d", i);
    }
    Attached Images Attached Images Factorizing Algorithm-img_20170706_202904-jpg 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for(i=2; i<number; i++)
    Well look at your algorithm.

    Code:
    for ( i = 2 ; !isPrime(number); i = nextPrime(i) ) {
      while ( number % i == 0 ) {
        printf("%d ", i );
        number /= i;
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-20-2011, 10:58 PM
  2. Algorithm Help
    By (TNT) in forum Tech Board
    Replies: 2
    Last Post: 07-28-2007, 02:22 AM
  3. algorithm help--
    By revelation437 in forum C++ Programming
    Replies: 6
    Last Post: 03-06-2004, 07:02 PM
  4. MD5 Algorithm
    By Inquirer in forum C Programming
    Replies: 2
    Last Post: 12-28-2003, 11:55 PM
  5. my grandfather's chess algorithm can beat your grandfather's chess algorithm...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 08-17-2001, 06:52 PM

Tags for this Thread