Thread: Help please , cant find mine error in code .

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    10

    Help please , cant find mine error in code .

    Hey , in this "mini-proj" i should have make an Lucky ticket checker..
    all work fine. except one thing . when i put "0" in the start of the number it calculate me a different Sum of numbers. youll see it if you tryin to Compile and run the Code. if some one can check mine incorrect itll be a pleasure thank you!

    Code:
    /*
    
    #include<stdio.h>
    int isLuckyNumber(int number);
    int sumDigit(int number);
    int sumEvenNumber(int number);
    int totalLuckyTicket();
    
    
    int main()
    {
        int number = 023232;
        int piterLucky = isLuckyNumber(number);
        if(piterLucky==1)
        {
            printf("Number %d is LuckyTicket\n",number);
        }
        else
        {
            printf("Number %d is NOT LuckyTicket\n",number);
        }
    
    
        printf("There is %d Of lucky Tickets.",totalLuckyTicket());
    
    
        return 0;
    }
    
    
    int isLuckyNumber(int number)
    {
        int sumDig = sumDigit(number);
        int sumEvenNum = sumEvenNumber(number);
        if(sumDig==sumEvenNum*2)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    
    
    }
    
    
    
    
    int sumDigit(int number)
    {
        int res = 0;
        for(; number!=0 ; number /= 10)
        {
            res = res+number%10;
    
    
        }
        return res;
    }
    
    
    int sumEvenNumber(int number)
    {
        int sumE=0;
        for(; number!=0; number /= 10)
        {
            if((number%10)%2==0)
            {
            sumE = sumE + number%10;
            }
        }
        return sumE;
    
    
    }
    
    
    int totalLuckyTicket()
    {
        int count=0;
        int x;
        for(x=1 ; x<=999999 ; x++)
        {
            if(isLuckyNumber(x)==1)
            {
                count++;
            }
        }
            return count;
    
    
    
    
    }
    Im kinda of sure the problem is in this Part :

    Code:
    int sumDigit(int number){
        int res = 0;
        for(; number!=0 ; (number = number/10))
        {
            res = res+number%10;
    
    
        }
        return res;
    }
    for example to understanding more : when i put int number = 023232 it will change the whole number to "9889" . but when i put 232320 it will print the right number . thats what im tryin to fix !
    Last edited by Melkweg; 07-31-2018 at 04:32 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,239
    If an integer literal starts with a 0 it's interpretted as octal (base 8) instead of decimal (base 10). Just get rid of the 0. When reading in an integer with scanf make sure you use "%d" and not "%i" since "%i" will interpret a leading zero as indicating octal, whereas "%d" will ignore it and always interpret the input as decimal, which is usually what you want.
    Code:
    #include <stdio.h>
     
    int isLuckyNumber(int number);
    int sumDigit(int number);
    int sumEvenNumber(int number);
    int totalLuckyTicket();
     
    int main() {
        int number = 23232;
     
        if (isLuckyNumber(number))
            printf("Number %d is a LuckyTicket\n", number);
        else
            printf("Number %d is NOT a LuckyTicket\n", number);
     
        printf("There are %d lucky Tickets.\n", totalLuckyTicket());
     
        return 0;
    }
     
    int isLuckyNumber(int number) {
        return sumDigit(number) == sumEvenNumber(number) * 2;
    }
     
    int sumDigit(int number) {
        int res = 0;
        for( ; number != 0; number /= 10)
            res += number % 10;
        return res;
    }
     
    int sumEvenNumber(int number) {
        int sumE = 0;
        for( ; number != 0; number /= 10)
            if (number % 2 == 0)
                sumE += number % 10;
        return sumE;
    }
     
    int totalLuckyTicket() {
        int count = 0;
        for (int x = 1; x <= 999999; x++)
            if (isLuckyNumber(x))
                count++;
        return count;
    }
    Your logic makes me feel like a dick. - Bob Fossil

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    10
    Quote Originally Posted by john.c View Post
    If an integer literal starts with a 0 it's interpretted as octal (base 8) instead of decimal (base 10). Just get rid of the 0. When reading in an integer with scanf make sure you use "%d" and not "%i" since "%i" will interpret a leading zero as indicating octal, whereas "%d" will ignore it and always interpret the input as decimal, which is usually what you want.
    Code:
    #include <stdio.h>
     
    int isLuckyNumber(int number);
    int sumDigit(int number);
    int sumEvenNumber(int number);
    int totalLuckyTicket();
     
    int main() {
        int number = 23232;
     
        if (isLuckyNumber(number))
            printf("Number %d is a LuckyTicket\n", number);
        else
            printf("Number %d is NOT a LuckyTicket\n", number);
     
        printf("There are %d lucky Tickets.\n", totalLuckyTicket());
     
        return 0;
    }
     
    int isLuckyNumber(int number) {
        return sumDigit(number) == sumEvenNumber(number) * 2;
    }
     
    int sumDigit(int number) {
        int res = 0;
        for( ; number != 0; number /= 10)
            res += number % 10;
        return res;
    }
     
    int sumEvenNumber(int number) {
        int sumE = 0;
        for( ; number != 0; number /= 10)
            if (number % 2 == 0)
                sumE += number % 10;
        return sumE;
    }
     
    int totalLuckyTicket() {
        int count = 0;
        for (int x = 1; x <= 999999; x++)
            if (isLuckyNumber(x))
                count++;
        return count;
    }

    so there is no Way to put "0" in the first of numbers? thats what im tryin to re make .. and ty for the answer!

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,239
    If you think about it, it doesn't make sense to allow 0's at the beginning of an integer since it wouldn't make any difference to the value. That's why the language designer felt free to give a leading 0 a special meaning, similar to a leading 0x meaning hexidecimal. So 010 is 8 decimal and 0x10 is 16 decimal.


    As I mentioned, you can allow the user to enter leading zeros if you use the "%d" format with a scanf. It will just ignore them so when you print the number out the leading zeroes will be gone unless you tell printf to add them in like this:
    Code:
            printf("Number %06d is a LuckyTicket\n", number);
    Your logic makes me feel like a dick. - Bob Fossil

  5. #5
    Registered User
    Join Date
    Oct 2017
    Posts
    36
    I can not find within your program the lines where you prompt the user for an input. If i am wrong please put the out to me. Thanks

  6. #6
    Registered User
    Join Date
    Jul 2018
    Posts
    10
    Quote Originally Posted by splitfunc0 View Post
    I can not find within your program the lines where you prompt the user for an input. If i am wrong please put the out to me. Thanks
    you meant to "scanf"? .. cause im not using it .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mine sweeper code help..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 01-01-2009, 09:44 AM
  2. Can not find error in this code
    By cjohnman in forum C Programming
    Replies: 5
    Last Post: 05-06-2008, 11:04 AM
  3. Can't Find Error in Code
    By DarkDot in forum C++ Programming
    Replies: 6
    Last Post: 03-29-2007, 09:56 PM
  4. Can't find error in code
    By blackgingr in forum C Programming
    Replies: 10
    Last Post: 11-14-2002, 09:00 PM
  5. HELP! can't find that one error in my code
    By andyt2000 in forum C++ Programming
    Replies: 1
    Last Post: 09-21-2001, 09:25 PM

Tags for this Thread