Thread: What's wrong with this program?

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    8

    What's wrong with this program?

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    main()
    {
        int p,N[100] = {0},d,c,E;
        printf("Input a number: ");
        scanf("%d",&p);
        for(d=1,c=0;d<p;d++)
        {
            if(p%d==0)
            {
                N[c]=d;
                c++;
            }
        }
        c=0;
        while(N[c]!=0)
        {
            E=E+N[c];
        }
        printf("\n%d",E);
    }
    So I have made a program for computing the sum of the factors of the input but the problem is that it doesn't print the sum. I can't see the error in this code so can someone indicate the problem. Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    Code:
    #include <stdio.h>
    
     
    main()
    {
        int p,N[100] = {0},d,c,E=0;
        printf("Input a number: ");
        scanf("%d",&p);
        for(d=1,c=0;d<=p;d++)
        {
            if(p%d==0)
            {
                N[c]=d;
                c++;
            }
        }
        c=0;
        while(N[c]!=0)
        {
            E=E+N[c];
            c++; // see here since you are not incrementing c in your code, the while loop becomes an infinite  one.
        }
        printf("\n%d",E);
    }
    its something common for beginners. you take care of the important part(to find the sum or the product or whatever but forget to do the smaller steps.) increment the index c in the while loop until there is a zero..hope it helps!!

    but im not getting the right answer when i input 20 the answer should be 1+2+5+10+20=38.
    a number is a factor of itself you missed that point there..
    by the way initialize E to zero...
    Last edited by theju112; 08-08-2012 at 11:19 AM.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    8
    When I tried what you told me. The sum is something unexpected. When I enter "32", it prints a unexpected number. But thanks for the answer, I didn't saw that error. But still more problem.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    Quote Originally Posted by KiRa11Love View Post
    When I tried what you told me. The sum is something unexpected. When I enter "32", it prints a unexpected number. But thanks for the answer, I didn't saw that error. But still more problem.
    initiaize E to zero(see line 6 in the code)...otherwise some garbage value will be stored there..thats your problem i guess..my code works fine for 32..it gives 63...hit like if it works

    hint: if you input 20, the sum of factors includes 20 also...are you checking if a number is a factor of itself?? just see upto what number you are checking for factors!
    Last edited by theju112; 08-08-2012 at 10:47 AM.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    8
    It worked!... Thanks Man... Now I can continue on my life. XD

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This looks like C, not C++. Mix up?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Something is wrong with my program
    By november1992 in forum C Programming
    Replies: 1
    Last Post: 05-07-2012, 11:49 AM
  2. what is wrong in the following program?
    By Vivek Kopparthi in forum C Programming
    Replies: 3
    Last Post: 06-08-2011, 04:45 AM
  3. What is wrong with my program
    By bijan311 in forum C++ Programming
    Replies: 18
    Last Post: 12-13-2009, 05:04 PM
  4. What is wrong with the following program?
    By roaan in forum C Programming
    Replies: 14
    Last Post: 08-27-2009, 12:52 PM
  5. do you see anything wrong with this program?
    By seal in forum C Programming
    Replies: 3
    Last Post: 09-22-2005, 07:43 AM