Perfect number and divisors

This is a discussion on Perfect number and divisors within the C Programming forums, part of the General Programming Boards category; Hi, I have to write a program that displays the nature of a number (perfect, defficient, or abundant) and DISPLAY ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    28

    Perfect number and divisors

    Hi,

    I have to write a program that displays the nature of a number (perfect, defficient, or abundant) and DISPLAY ITS DIVISORS. and this is actually the bad part (lol). i have to use a while loop. i'll post it here what i've written so far. but it always displays a huge sam enumber as a divisor.
    Could anybody correct me?? It's due on monday.
    Thanks a lot.

    Code:
     #include <stdio.h>
       int main (void) {
         int n, cnt, sum_div;
         char ans;
         do{
         printf("-------------------\n");
         printf("Enter a number:\n");
         scanf("%d", &n);
         while((n%cnt==0) && (cnt<=n/2));{
          printf("Divisors: %d,\n", cnt);
          sum_div+=cnt;
          if(sum_div==n)
           printf("---%d is Perfect\n", n);
           else if(sum_div>n)
           printf("---%d is Abundant\n", n);
          else
           printf("---%d is Defficient\n", n);
         }
         printf("Continue? <Y/N>:\n");
         scanf(" %c", &ans);
         while(ans=='Y'||ans=='y')
         }
         printf("Bye:>_\n");
         return(0);
         }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And why do you think cnt isn't 123409857 when you start the program? What else is it supposed to be?

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    My help is due Monday or your Homework assignment is due on Monday?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    28
    Homework is due MOnday. actually i assigned 1 to cnt( counter)
    cnt=1 before the while, i just forgot to write it

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Just copy and paste next time, por favor -_-. And lets go ahead and make this time no exception.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    You need to make cnt do something.
    sum_div needs to be initialized to zero.
    The "Continue? <Y/N>" loop needs some help.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    28
    actually the continue look includes all the program, as the user has to chose whether to conitue or end there.
    i now assigned 1 to cnt and 0 to sum_div.
    is there any pblm with the braces?

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    28
    *continue loop

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Can you copy the code that you can compile it and paste it, please. Tabs and [code][/code] tags are prefered by most of us here.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    28
    ok here's the final thing. but the display of divisors is wrong.
    Code:
    #include <stdio.h>
       int main (void) {
         int n, cnt, sum_div;
         char ans;
         do{
         printf("-------------------\n");
         printf("Enter a number:");
         scanf("&#37;d", &n);
         cnt=2;
         sum_div=0;
         while(n%cnt==0&&cnt<=n/2){
          printf("Divisors: %d,\n", cnt);
          sum_div+=cnt;
          if(sum_div==n)
           printf("---%d is Perfect\n", n);
           else if(sum_div>n)
           printf("---%d is Abundant\n", n);
          else
           printf("---%d is Defficient\n", n);
         }
         printf("Continue? <Y/N>:\n");
         scanf(" %c", &ans);
         while(ans=='Y'||ans=='y')
         printf("Bye:>_\n");
         return(0);
        }

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <ctype.h>
    
    int main (void) {
      float n, cnt, sum_div;
      char ans;
      do{
        printf("-------------------\n");
        printf("Enter a number:");
        scanf("&#37;f", &n);
    
        for(cnt = 2.0f, sum_div=0.0f; (fmod(n,cnt)==0)&&cnt<=n/2.0)
        {
          printf("Divisors: %f,\n", cnt);
          sum_div+=cnt;
    
          if(sum_div==n)
            printf("---%f is Perfect\n", n);
          else if(sum_div>n)
            printf("---%f is Abundant\n", n);
          else
            printf("---%f is Defficient\n", n);
        }
        printf("Continue? <Y/N>:\n");
        scanf(" %c", &ans);
    
      } while(toupper(ans)=='Y');
      printf("Bye:>_\n");
      return(0);
    }
    I changed some of the code around so we can read it. And now is it at least compileable?

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    28
    that looks better. im using C free as a compiler and it's saying sth about C++ and ANSI and stuff like that. plus we haven't rlly done the for loop yet, and im suppose to do it with the while one.
    well actually divisors have to be displayed like this for instance:
    Divisors: 2, 3, 5, 6
    and i just dont know how to have them all in one line, with the spaces.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Because I am a nice guy and because you are being less frustrating than the guy who I have posted the exact answer to his problem twice with no attempt to heed my words:

    Example:
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <ctype.h>
    
    int main (void) {
      float n, cnt, sum_div;
      char ans;
      do{
        printf("-------------------\n");
        printf("Enter a number:");
        scanf("&#37;f", &n);
    
        cnt = 2.0f;
        sum_div = 0.0f;
    
        printf("Divisors");
        ans = ':'; // Just trust me on this one. Your teacher may think you are a genious.
    
        while((fmod(n,cnt)==0)&&cnt<=n/2.0)
        {
          printf("%c %f", ans, cnt); 
          sum_div+=cnt;
          ans = ',';
    
          if(sum_div==n)
            printf("(Perfect)");
          else if(sum_div>n)
            printf("(Abundant)");
          else
            printf("(Defficient)");
        }
    
        printf("\nContinue? <Y/N>:\n");
        scanf(" %c", &ans);
    
      } while(toupper(ans)=='Y');
      printf("Bye:>_\n");
      return(0);
    }
    P.S. The for loop was for my benefit, feel free to change it back to a while loop.
    Last edited by master5001; 10-17-2008 at 05:49 PM.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    To be honest, I would ditch

    Code:
          if(sum_div==n)
            printf("(Perfect)");
          else if(sum_div>n)
            printf("(Abundant)");
          else
            printf("(Defficient)");
    ^ That code.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    28
    lol i know ur being veeery patient with me. im sorry im just a beginner :d:d
    well i tried to compile ur code, it gave me a scary .exe, it's still running now btw while im writing u this. keeps writing 2.00000000000000000 abundant.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Perfect Numbers C Program
    By SlayerBlade in forum C Programming
    Replies: 2
    Last Post: 08-28-2005, 06:11 PM
  2. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 05:25 AM
  3. Replies: 3
    Last Post: 01-14-2003, 10:34 PM
  4. Perfect Number Problem
    By TrazPFloyd in forum C++ Programming
    Replies: 21
    Last Post: 10-20-2002, 12:09 PM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM

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