Prime factor

This is a discussion on Prime factor within the C Programming forums, part of the General Programming Boards category; Just wondering if someone can tell me why there is no output for this program. I've attempted to debug it ...

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Prime factor

    Just wondering if someone can tell me why there is no output for this program.

    I've attempted to debug it but a fresh view would be appreaciated.
    Code:
    /* divisors.c -- nested ifs display divisors of a number */
    
    #include <stdio.h>
    #include <stdlib.h>
    #define NO 0
    #define YES 1
    
    int main(void)
    
    {
    
    
        long num, div;
        int prime; /*potential divisors */
    
    
        printf("Please enter an integer for analysis; ");
        printf("Enter q to quit.\n");
        while (scanf("ld", &num) == 1)
        {
              for (div = 2, prime = YES; (div * div) <= num; div++)
              {
                  if (num % div == 0)
                  {
                     if ( (div * div) != num)
                        printf("%ld is divisible by %ld and %ld.\n", num, div, num / div);
    
                     else
                         printf("%ld is divisible by %ld.\n", num, div);
                     prime = NO;  /* Number is not  prime */
                   }
               }
    
    
         if (prime == YES)
            printf("%ld is prime.\n", num);
         printf("Please enter another integer for analysis; ");
         printf("Enter q to quit.\n");
    
    
         }
    
         system("pause");
         return (0);
    
    
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    while (scanf("%ld", &num) == 1)
    Enjoy.
    My best code is written with the delete key.

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    its always something like that.

    Why doesn't the compiler give me a syntax error. Is it compiler specific else it means that it is valid syntax. I'm using dev c++ 4.0.


    thanks.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > Why doesn't the compiler give me a syntax error.
    It will if you specify
    -Wall

    While you're at it, add
    -w -ansi -pedantic -O2

    That will really flush out a lot of problems
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Originally posted by Salem
    > Why doesn't the compiler give me a syntax error.
    It will if you specify
    -Wall

    While you're at it, add
    -w -ansi -pedantic -O2

    That will really flush out a lot of problems

    sorry, you guys are the C-gods. I don't understand what you just said.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I don't understand what you just said.
    He said to set your compiler to it's $$$$$iest setting so that such errors are more likely to be automatically found.
    My best code is written with the delete key.

  7. #7
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    oh, i see. thank you. i'll do that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating prime factor of a huge number.
    By Bakster in forum C Programming
    Replies: 15
    Last Post: 02-20-2009, 12:06 PM
  2. last prime factor
    By frango9000 in forum C Programming
    Replies: 7
    Last Post: 07-27-2006, 01:42 PM
  3. Replies: 3
    Last Post: 03-29-2005, 04:24 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Prime Factor Fxn
    By alpha in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2003, 11:44 AM

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