UVa – 11332 – Summing Digits

#include <bits/stdc++.h>

#define ms(a,b)         memset(a,b,sizeof(a))
#define pb(a)           push_back(a)
#define db              double
#define ft              float
#define ll              long long
#define ull             unsigned long long
#define ff              first
#define ss              second
#define sz(x)           x.size()
#define qu              queue
#define pqu             priority_queue
#define vc              vector
#define vi              vector<int>
#define vll             vector<long long>
#define pii             pair<int,int>
#define pis             pair<int,string>
#define psi             pair<string,int>
#define all(x)          x.begin(),x.end()
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define loop0(i,n)      for(int i=0;i<n;i++)
#define loop1(i,n)      for(int i=1;i<=n;i++)
#define stlloop(x)     for(__typeof(x.begin()) it=x.begin();it!=x.end();it++)
#define gcd(a, b)       __gcd(a, b)
#define lcm(a, b)       ((a)*((b)/gcd(a,b)))
#define case(z,x)       cout<<"Case "<<i<<": "<<x<<endl
#define case(z)         cout<<"Case "<<z<<": "
#define PI              3.14159265358979323846264338328
#define valid(tx,ty)    tx>=0 && tx<r && ty>=0 && ty<c
#define MAX             2000

/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

using namespace std;

int main()
{
    CIN;
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    ll n,x;
    while(cin>>n && n!=0)
    {
        while(1)
        {
            ll c=0,sum=0;
            while(n!=0)
            {
                c++;
                x=n%10;
                n=n/10;
                sum=sum+x;
            }
            if(c==1)
            {
                cout<<sum<<endl;
                break;
            }
            else
            {
                n=sum;
            }

        }
    }

    return 0;
}

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;
}