Bài 47: Hãy tính tổng các chữ số chẵn của số nguyên dương n
//========================================//Cách 1
#include <stdio.h>
#include <conio.h>
void main()
{
int n;
scanf("%d", &n);
int S = 0;
int t = n;
while (t != 0)
{
int dv = t%10;
if (dv%2 == 0)
{
S = S + dv;
}
t = t/10;
}
printf("%d", S);
}
//========================================
//Cách 2
/*
#include <stdio.h>
#include <conio.h>
int Tong(int);
void main()
{
int n;
scanf("%d", &n);
int kq = Tong(n);
printf("%d", kq);
}
int Tong(int k)
{
int S = 0;
for (int t=k; t!=0; t=t/10)
{
int dv = t%10;
if (dv%2 == 0)
{
S = S + dv;
}
}
return S;
}
*/
No comments:
Post a Comment