Цифри на число

Да се напише програма, която позволява да се въведе четирицифрено число и да се отпечата броят на четните и броят на нечетните цифри в него, както и сумата им.

Публикувано в 11а, 11в с етикети . Постоянна връзка.

4 Responses to Цифри на число

  1. Gadget каза:
     
    int x,x1,x2,x3,x4,br1,br2,sum1,sum2;
        cout<<"Vavedete chetiritzifreno chislo:"<<endl;
        cin>>x;
        //Razdelqme chisloto na tzifri
        x4=x/1000;
        x3=(x/100)%10;
        x2=(x/10)%10;
        x1=x%10;
        //Namirame broqt na chetnite
        br2=bool(x1%2==0)+bool(x2%2==0)+bool(x3%2==0)+bool(x4%2==0);
        //Namirame broqt na nechetnite
        br1=bool(x1%2==1)+bool(x2%2==1)+bool(x3%2==1)+bool(x4%2==1);
        //Namirame sumata na chetnite tzifri
        sum2=(bool(x1%2==0)*x1)+(bool(x2%2==0)*x2)+(bool(x3%2==0)*x3)+(bool(x4%2==0)*x4);
        //Namirame sumata na nechetnite tzifri
        sum1=(bool(x1%2==1)*x1)+(bool(x2%2==1)*x2)+(bool(x3%2==1)*x3)+(bool(x4%2==1)*x4);
        
        cout<<"\nBroqt na chetnite tzifri e: "<<br2;
        cout<<"\n\nBroqt na nechetnite tzifri e: "<<br1;
        cout<<"\n\nSumata na chetnite tzifri e: "<<sum2;
        cout<<"\n\nSumata na nechetnite tzifri e: "<<sum1<<endl;
        
        system("PAUSE");
        return 0;
    
  2. dreanor каза:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
       int x,a,b,c,d,ch,suma;
        cout<<"Vavedete chetiricifreno chislo: \n";
        cin>>x;
        if ((x>=1000)&&(x<=9999))
        {
            a=x/1000;
            b=(x/100)%10;
            c=(x/10)%10;
            d=x%10;
            
            suma=a+b+c+d;
            
            ch=a%2+b%2+c%2+d%2;
            
            cout<<"broqt na nechetnite e :"<<ch<<endl;
            cout<<"broqt na chetnite e :"<<4-ch<<endl;
            cout<<"sumata na nechetnite e: "<<a*(a%2)+b*(b%2)+c*(c%2)+d*(d%2)<<endl;
            cout<<"sumata na chetnite e: "<<suma-(a*(a%2)+b*(b%2)+c*(c%2)+d*(d%2))<<endl;
        }
       else cout<<"newalidno chislo\n";
        system("PAUSE");
        return 0;
    }
    
  3. maleboldjia каза:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    int x,a,b,c,d,p,s,ch,br_ch,nech,br_nech; //ch-?????; nech-???????
    cout<<"Vavedi 4-cifreno chislo: ";
    cin>>x;
    cout<<endl;
    if ((x>=1000)&&(x<10000)) {
       a=x/1000;
       b=x/100%10;
       c=x/10%10;
       d=x%10;
       s=a+b+c+d;
       p=a*(a%2)+b*(b%2)+c*(c%2)+d*(d%2);
       
       br_nech=bool(a%2==1)+bool(b%2==1)+bool(c%2==1)+bool(d%2==1);
       br_ch=bool(a%2==0)+bool(b%2==0)+bool(c%2==0)+bool(d%2==0);
       
       cout<<"Broyat na chitnite cifri e: "<<br_ch<<endl;
       cout<<"Sumata na chetnite cifri e: "<<s-p<<endl;
       cout<<endl;
       cout<<"Broyat na nechetnite cifri e: "<<br_nech<<endl;
       cout<<"Sumata na nechetnite cifri e:"<<p<<endl;
    }
    else cout<<"Nevalidno chislo."<<endl;
    
        system("pause");
    	return 0;
    }
    

Вашият коментар