# Thread: Just learning, problem with an excercise.

1. ## Just learning, problem with an excercise.

I have to do a program that evaluates if the digits of a number are alternatingly evens and odds. Example = 23232, 1234, 9634, etc.

Code:
```#include <stdio.h>

int main()
{
int a;
int cont=0;
printf("Ingrese un entero :");
scanf("%i",&a);

int temp=a;

while(temp>0)
{
temp=temp/10;
cont++;
}

printf("\n Número de dígitos : %d",cont);

int arr[cont], dig=(temp%10), i=0;
temp=a;

while (i<cont){
if (dig%2==0){
arr[i]=0;
i++;
temp=temp/10;
dig=temp%10;}

else if (dig%2!=0){
arr[i]=1;
i++;
temp=temp/10;
dig=temp%10;}
}

i=0;
int t=(i+1), alter;

while (i<cont){
if (arr[i]!=arr[t]){
alter=1;
i++;
t++;}

else{
alter=0;
i++;
t++;}
}

if (alter==1)
printf ("\n El número sí es alternante");
else
printf ("\n El número no es alternante");

i=0;    // I did this to see if it detection of even or odds worked right
while (i<cont){
printf ("\n %i (0=par, 1=impar)", arr[i]);
i++;
}
}```
The problem is that in this part:
Code:
```while (i<cont){
if (dig%2==0){
arr[i]=0;
i++;
temp=temp/10;
dig=temp%10;}

else if (dig%2!=0){
arr[i]=1;
i++;
temp=temp/10;
dig=temp%10;}
}```
The first digit to enter will always be taken as an even one, and I don't know why.

2. Because when you do dig = temp%10, temp is 0, and 0%10 is 0, which coincidentally is even.

3. Thanks.

Popular pages Recent additions