Thread: Prime factorization of a number

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    24

    Prime factorization of a number

    So I am writing a code where the program will keep asking for a number and calculate its prime factorization unless the user enters 1 or less. What am I doing wrong?
    Code:
    #include<stdio.h>
    int main()
    {
      int x, i;
    
    
      while (x > 1) {
        printf("Enter an integer:  ");
        scanf("%d", &x);
        printf("The prime factorization of %d is ", x);
    
    
        while (x % 2 == 0) {
          printf("2 ");
          x = x / 2;
        }
        for (i = 3; i < 1009; i = i + 2) {
          while (x % i == 0) {
            printf("%d ", i);
            x = x / i;
          }
        }
    
      }
    
    
      return 0;
    }
    Last edited by Salem; 11-14-2020 at 12:06 AM. Reason: Removed crayola - use copy/paste as TEXT ONLY!!!!!!!!!!!!!!!!!!!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    It terminates after computing for the first input.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that your loop condition involves x, but you modify x after reading it. You need to check what the user entered immediately after reading it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    How do I fix that error?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One way that doesn't require so much changes would be to change your outer while loop condition to 1, i.e., true. This makes your loop an infinite loop, but then you add a check for the user input right after reading it, and if it is 1 or less, break from the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    What would that loop look like?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Um, I just told you.

    Let's put it another way: why does your program "terminates after computing for the first input"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    Not sure if you have learned about functions yet, but they would make your task easier:
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    
    void print_prime_factors(int num)
    {
      printf("The prime factors of %d are ", num);
      // your code here
    }
    
    
    int main()
    {
      int num;
      while(true)
      {
        printf("Enter an integer:  ");
        scanf("%d", &num);
        if (num < 2)
          break;
          
        print_prime_factors(num);
      }
        
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. prime number generation, match 10001 prime number
    By _jamie in forum C Programming
    Replies: 10
    Last Post: 02-11-2020, 07:51 AM
  2. Prime Factorization
    By jack_carver in forum C Programming
    Replies: 0
    Last Post: 07-02-2009, 07:16 AM
  3. Prime Factorization
    By johnkisera123 in forum C Programming
    Replies: 7
    Last Post: 11-05-2008, 10:02 AM
  4. Number Factorization
    By clipsit in forum C Programming
    Replies: 39
    Last Post: 02-08-2008, 10:47 AM
  5. Replies: 2
    Last Post: 09-11-2002, 05:00 PM

Tags for this Thread