Experiment 4-1-4 Find the digits of an integer and the sum of the digits (15 points)

For a given positive integer N, find the sum of its digits and its digits.

Input format:

Enter a positive integer N that does not exceed 1 0 ​ 9 ​ ​ in one line Enter a positive integer N that does not exceed 10^(​9)​​ in one line Input into in a row are given an a th not ultra over- . 1 09 isa positiveintegernumberN

Output format:

Output the number of digits of N and the sum of their digits in one line, separated by a space.

Input sample:

321

Sample output:

3 6

Code:

# include <stdio.h>
# include <stdlib.h>

typedef long long int long_int;
int main() {
    
    
	long_int n,temp;
	int poi = 0,value = 0;
	scanf("%lld",&n);
	temp = n;
	// 321——321 % 10 = 1——321 / 10 = 32——32 % 10 = 2 
	while (1) {
    
    
		value += (temp % 10);
		if (temp < 10) {
    
    
			poi += 1;
			break;
		} else {
    
    
			temp /= 10;
			poi += 1;
		}
	}
	printf("%d %d",poi,value);
	return 0;
} 

Submit screenshot:

Insert picture description here

Problem-solving ideas:

The idea of ​​solving the problem here has already been encountered in the previous problem. The comment in the code gives an example to facilitate everyone's understanding!

  • typedef longlong int long_int;The meaning is to make long long intthe alias for the long_intconvenience of the later call, which is purely a mess, you can use the previous one directly!

Guess you like

Origin blog.csdn.net/weixin_43862765/article/details/114477057