tim tong cac chu so va so cac chu so

/*tinh tong cac chu so va so cac chu so*/

#include<stdio.h>

#include<conio.h>

#include<math.h>

int main()

{

int n,ln,a,b;

int i;

i=0;

printf("nhap so nguyen n:");

scanf("%d",&n);

ln=n;

a=0;

while(n!=0)

{

b=n%10;

a=a+b;

i=i+1;/*bien i dung de dem so lan thuc hien phep tinh =>i la so chu so*/

n=n/10;

}

printf("tong cac chu so cua %d la: %d

",ln,a);

printf("so %d co %d chu so",ln,i);

getch();

return 0;

}

/*VD:

n=123 n=12 n=1

=>ln=123

123#0 12#0 1#0

b=3 b=2 b=1%10=1

a=3 a=3+2=5 a=5+1=6

i=1 i=1+1=2 i=2+1=3

n=12 n=12/10=1 n=1/10=0

*/

Bạn đang đọc truyện trên: AzTruyen.Top

Tags: