Saturday, March 12, 2011

Find if a number is divisible by 3, without using %,/ or *. You can use atoi().

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int fnSum(int);

char ch;
int main()
{
char cNumber[100];
int nsum=0,i=0;
printf("Enter the number\n");
scanf("%s",&cNumber);


nsum = fnSum(cNumber);


if(nsum == 3 || nsum == 6||nsum == 9 )
printf("Given number is divisible by 3\n");
else
printf("Given number is not divisible by 3\n");


return 0;
}

int fnSum(char *cNumber)
{
int nsum=0,i=0;

while (*(cNumber+i) != NULL)
{
ch=(*(cNumber+i));

nsum +=atoi(&ch);

i++;

}

if(nsum > 10)
{
sprintf(cNumber,"%d",nsum);
nsum = fnSum(cNumber);
}
return nsum;
}

OUTPUT:


Enter the number
366
Given number is divisible by 3
Press any key to continue . . .

No comments:

Post a Comment