Advertisement
nguyenhappy92

Viết hàm tính tổng các chữ số nguyên tố

Nov 3rd, 2015
933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. // tinh tong cac chu so nguyen to
  2. // Khai bao cac ham thu vien neu co
  3. #include<stdio.h>
  4. #include<conio.h>
  5.  
  6. int NT(int x)
  7. {
  8. int dem=0;
  9. for(int i=1;i<=x;i++)
  10. {
  11. if(x%i==0)
  12. dem++;
  13. }
  14. if(dem==2)
  15. return 1;
  16. return 0;
  17. }
  18. // tinh tong
  19. long tinhNT(int y)
  20. {
  21. long s=0;
  22. int a;
  23. while(y!=0)
  24. {
  25. a=y%10;
  26. if(NT(a)==1)
  27. {
  28. s=s+a;
  29. }
  30. y=y/10;
  31. }
  32. return s;
  33. }
  34. void main()
  35. {
  36. int n;
  37. scanf("%d",&n);
  38. printf("%ld",tinhNT(n));
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement