In this problem you have to determine weather a number is multiple of 11.
Now what you have to just find the sum of all the digit of the number then divide it by 11.
if it's divisible by 11 then the number also divisible by 11.
Or, just sum the digit of the number which is odd and which is even. then find the difference between the odd position digit sum and even position digit sum. if it's 0 or divisible by 11 then the number also divisible by 11.
at first try yourself, and if you can't after trying then only see the code otherwise you can't improve yourself in programming.
#include<stdio.h>
#include<string.h>
int main()
{
int n,i,r,s;
char a[1000];
while(gets(a))
{ s=0;
n=strlen(a);
i=0;
if(a[i]=='0'&&n==1)
break;
for(i=0;i<n;i=i+2)
{
s+=a[i]-'0';
}
for(i=1;i<n;i=i+2)
{
s-=a[i]-'0';
}
if(s==0||s%11==0)
printf("%s is a multiple of 11.\n",a);
else
printf("%s is not a multiple of 11.\n",a);
}
return 0;
}
Now what you have to just find the sum of all the digit of the number then divide it by 11.
if it's divisible by 11 then the number also divisible by 11.
Or, just sum the digit of the number which is odd and which is even. then find the difference between the odd position digit sum and even position digit sum. if it's 0 or divisible by 11 then the number also divisible by 11.
at first try yourself, and if you can't after trying then only see the code otherwise you can't improve yourself in programming.
#include<stdio.h>
#include<string.h>
int main()
{
int n,i,r,s;
char a[1000];
while(gets(a))
{ s=0;
n=strlen(a);
i=0;
if(a[i]=='0'&&n==1)
break;
for(i=0;i<n;i=i+2)
{
s+=a[i]-'0';
}
for(i=1;i<n;i=i+2)
{
s-=a[i]-'0';
}
if(s==0||s%11==0)
printf("%s is a multiple of 11.\n",a);
else
printf("%s is not a multiple of 11.\n",a);
}
return 0;
}
No comments:
Post a Comment