Thread: Write a program to find the factors of any number entered.

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    11

    Question Write a program to find the factors of any number entered.

    Code:
    #include<stdio.h>
    #include<math.h>
    main()
    {
        int num, i, rem;
        printf ("Enter the number to be factorised:");
        scanf ("\n%d",&num);
        printf ("The factors of %d =",num);
        for (i=0;i<=num;i++)
        {
            rem = num%i;
            if (rem==0)
            printf( "\t%d",i);
        }
    }

    I couldn't fix the error in this code. It's not working for me. Please help.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    scanf() should not have a \n in it.

    The variable i should start at 1, instead of 0 in the for loop
    Last edited by Adak; 01-20-2010 at 11:12 AM.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    11
    Quote Originally Posted by Adak View Post
    scanf() should not have a \n in it.
    Ya, I tried that too. even then the program is closing itself. It's not executing.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    11
    Yes. It worked. Thanks.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Programs are different with this, but for this particular program I had to use the "big gun" console pauser:

    Code:
    //has 2 lines
    while((n = getchar()) != '\n';
    n = getchar();
    scanf()'s always have this problem, so be aware of it. It's surely the most common question we're asked about on the forum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  2. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  3. Replies: 13
    Last Post: 01-13-2008, 09:38 PM
  4. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  5. Need HELP with Program
    By Jeff in forum C Programming
    Replies: 25
    Last Post: 09-23-2004, 08:03 PM