首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求大神们帮忙解决方法

2013-11-01 
求大神们帮忙EXERCISE 1 (3 POINTS OUT OF 10)Write a C program that calculates the sum of the odd dig

求大神们帮忙
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;
}

希望可以帮到贴主

热点排行