Just learning, problem with an excercise.

This is a discussion on Just learning, problem with an excercise. within the C Programming forums, part of the General Programming Boards category; I have to do a program that evaluates if the digits of a number are alternatingly evens and odds. Example ...

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

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because when you do dig = temp%10, temp is 0, and 0%10 is 0, which coincidentally is even.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    16
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. Awkward problem in learning c++
    By TheUnknownFacto in forum C++ Programming
    Replies: 6
    Last Post: 05-17-2007, 01:43 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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