find no of even factor, express as %

This is a discussion on find no of even factor, express as % within the C++ Programming forums, part of the General Programming Boards category; question: enter a number, find number of even factor and expressed as percentage i have type in the code and ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    4

    find no of even factor, express as %

    question: enter a number, find number of even factor and expressed as percentage

    i have type in the code and compiled it sucessfully. But the reuslt wasn't expected (always come as 0). Can someone please help to solve the problem. I am using a blood shed dev cpp ver 4.01.
    Here is my code

    Code:
    int c(int day)
    //find the even factor of day and expressed as percentage
    //pre: day>0
    //post: return percentage
    {
     int eFactor;//even factor
     int tFactor;//total factor
     int i;//control variable
     int percent;//express as percentage
     
     //initialization
     eFactor = 0;
     tFactor = 0;
    
     for (i=1; i<=num; i++)
     {
      if (num%i==0)
      {
       tFactor++;
       if (i%2==0)
          eFactor++;
      }//end if  
     }//end loop
    
     percent = (eFactor / tFactor)* 100;
     return percent;
    }//end c

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Also the if-clause
    Code:
    if (num%i==0)
    is not neccesary.

    Your function should return a double or a float and not an integer number.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    As a minor point, the number of even factors will be equal to the number of times you can divide by 2.
    Code:
    int num_even(int n) {
        int num=0;
        while(n>1 && n%2==0) {++num;n/=2;}
        return num;
    }
    
    int num_odd(int n) {
         n >>= num_even(n);
         int num=0;
         const int lim = ceil(sqrt(n));
         for(int d=3;n>1 && d<=lim;d+=2) {
             while(n%d==0) {
                 ++num;
                 n/=d; 
             }
        }
        return num;
    }
    also consider what happens with your loop on 12 (3*2*2) this should be 66% but yours(fixing round off) will return 50%

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. could not find -lwsock32.lib
    By thomas_joyee in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2008, 12:28 PM
  3. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 07:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 08:28 AM

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