Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Bài 15: Hãy viết chương trình tính tổng các chữ số của một số nguyên bất kỳ
- CODE 1:
- #include <iostream>
- using namespace std;
- int tong(int n)
- {
- return n?n%10+tong(n/10):0;
- }
- int main()
- {
- cout<<tong(2222);
- system("pause");
- }
- CODE 2:
- #include <stdio.h>
- int main () {
- int n, s ;
- printf ("\nEnter an integer : ") ; scanf ("%d", &n) ;
- if (n < 0)
- n = - n ;
- cal:
- s= 0 ;
- while (n) {
- s += n %10 ; n /= 10 ;
- }
- if (s >= 10) {
- n = s ; goto cal ;
- }
- printf ("\nResult = %d\n", s) ;
- return 0 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment