UVa – 10931 – Parity

Solution 1: Using Array

#include <bits/stdc++.h>

using namespace std;

long long A[220000000];

int main()
{
    long long n,a,c,d,x,i,m,j,k;
    while(cin>>a)
    {
        if(a==0)
        {
            break;
        }
        n=a;
        i=0;
        while(n!=0)
        {
            d=n%2;
            A[i]=d;
            n=n/2;
            i++;
        }
        reverse(A,A+i);
        c=0;
        for(j=0; j<i; j++)
        {
            if(A[j]==1)
            {
                c++;
            }
        }
        cout<<"The parity of ";
        for(j=0; j<i; j++)
        {
            cout<<A[j];
        }
        cout<<" is "<<c<<" (mod 2)."<<endl;
    }
    return 0;
}

Solution 2: Using String

#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long n,a,c,d,i;
    while(cin>>a)
    {

        string s;
        if(a==0)
        {
            break;
        }
        n=a;
        i=0;
        while(n!=0)
        {
            d=n%2;
            s+=d+'0';    ///  s+=d+48 can be written...same thing.
            n=n/2;
            i++;
        }
        reverse(s.begin(),s.end());
        c=0;
        for(i=0; i<s.size(); i++)
        {
            if(s[i]=='1')
            {
                c++;
            }
        }
        cout<<"The parity of "<<s<<" is "<<c<<" (mod 2)."<<endl;
    }
    return 0;
}

UVa – 10929 – You Can Say 11

Note: Following is a shortcut for determining whether a number is divisible by 11 or not.  I got it on the internet.

Divisibility by 11:
How to check a number is divisible by 11?
It is very simple, check the number, if the difference of the sum of digits at odd places and the sum of its digits at even places, is either 0 or divisible by 11, then clearly the number is divisible by 11.

Examples:
Is the number 2547039 divisible by 11?
First find the difference between sum of its digits at odd and even places.
(Sum of digits at odd places) – (Sum of digits at even places)
= (9 + 0 + 4 + 2) – (3 + 7 + 5)
= 15 – 15 = 0
The number is 0, so the number 2547039 is divisible by 11.

Is the number 13165648 divisible by 11?
(Sum of digits at odd places) – (Sum of digits at even places)
= (8 + 6 + 6 + 3) – (4 + 5 + 1 + 1)
= 23 – 11 = 12
The number is 12, so the number 13165648 is not divisible by 11.

</pre>
#include <bits/stdc++.h>

using namespace std;

int main()
{

string s;
long long i,sum1,sum2,sum3;
while(cin>>s)
{
if(s=="0")
{
break;
}

sum1=0;
for(i=0; i<s.size(); i+=2)
{
sum1=sum1+(s[i]-'0');
}
sum2=0;
for(i=1; i<s.size(); i+=2)
{
sum2=sum2+(s[i]-'0');
}
sum3=abs(sum1-sum2);
if(sum3==0 || sum3%11==0)
{
cout<<s<<" is a multiple of 11."<<endl;
}
else
{
cout<<s<<" is not a multiple of 11."<<endl;
}

}
return 0;
}
<pre>