读书人

求大神们帮忙解决方法

发布时间: 2013-11-01 14:43:02 作者: rapoo

求大神们帮忙
EXERCISE 1 (3 POINTS OUT OF 10)
Write a C program that calculates the sum of the odd digits and the even digits of an integer. For example, the sum of the odd digits of the number 21554 is 1+5+5 or 11, and the sum of the even digits of the number 21554 is 2+4 or 6. The program should accept any arbitrary integer typed by the user.
A sample run of the program is illustrated below:
Please enter an integer: 21554
Sum of the odd digits: 11
Sum of the even digits: 6
[解决办法]


#include "stdio.h"

int main()
{
int m = 0;
int odd_sum = 0,even_sum = 0;
int quotient = 0,remainder = 0;
printf("please enter an integer:");
scanf("%d",&m);
do
{
remainder = m%10;
if(remainder & 0x01)
{
odd_sum += remainder;
}
else
{
even_sum += remainder;
}
m=m/10;
}while(m);
printf("Sum of the odd digits: %d\nSum of the even digits:%d",odd_sum,even_sum);
getchar();
return 0;
}

希望可以帮到贴主

读书人网 >C++

热点推荐