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

1. ## 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 !

2. 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;
}```

3. Originally Posted by john.c
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. 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);`

5. 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. Originally Posted by splitfunc0
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