long veriable causes problem

From: Ronen Kfir (ronenk_at_tauex.tau.ac.il)
Date: 04/30/04


Date: 30 Apr 2004 01:01:27 -0700

Hi,
The following code works only with int veriable defenitions. with long
I get wrong results, -1 does not terminate program & negative numbers
give back odd numbers.

please help.

TIA
Ronen

                 //calculates an admin digit based on //
                //program rules.//

#include<stdio.h>

                  //veriable definition//
long result(long x);
long bikoret;
long number;

void main()
{
puts ("\nPlease enter a positive integer");
scanf ("%d",&number); //first number input//

while (number!=-1 //exit from loop if number=-1//
{
if (number>=0) //if number>0 comete //
                         //the following code//
{
bikoret=result(number); //send veriavle //
                          //number as an argoment //
                           //to result() function. //
                         //Then asign the return value of result() to
//
                        //bikoret veriable//
printf ("Administration digit is %d\n", bikoret); //print output of
bikoret//
}
else
{
puts ("\npositive integres only!"); //if number<0//
                                   //don't calculate admin digit//
}
scanf ("%d",&number); //next number input//
}
}

long result (long x) //function calculates //
                       //admin digit//
{
long temp, result, i=0;
long sum=0;
while(x>0) //exit rule from loop is x reaces 0.//
{
temp=x%10; //find the right most digit.//
x-=temp; //deduct the right most digit //
                   //from x//
x/=10; //reduce the zero left after deduction.//
i++; //counter of digits.//
if (i%2==0) //find when x has to be multiple //
   sum+=2*temp; //by 2, & when by 1.//
else
{
if (i%2==1)
sum+=1*temp;
       }
}

result = 10-sum%10;
                                    //isolating digitis. find how much
needs//
if (result == 10) result = 0; //to comlete to 10//
return(result); //return value of result to //
                                      //program//
}