Tuesday, 17 June 2014

C++ program to find the sum of odd & even positioned digits of a number separately



Here is a C++ program to find the sum of odd & even positioned digits of a number separately


 SOURCE CODE OUTPUT
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int n,osum=0,esum=0,temp;
    cout<<"Enter the number ";
    cin>>n;
    while(n>0)
    {
        temp=n%10;
        if(temp%2==0)
            esum+=temp;
        else
            osum+=temp;
        n/=10;
    }
    cout<<"Sum of odd digits = "<<osum<<endl;
    cout<<"Sum of even digits = "<<esum;
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment