C language basic programming problems-binary conversion + recursion + ternary conditional operator
Enter a positive integer of type int, and calculate the number of 1 when the int type data is stored in the memory.
#include<stdio.h>
int Num(int n){
if (n==0){
return 0;
}
else {
return n % 2==1? (1 + Num(n / 2)) : Num(n / 2);
}
}
int main(){
int n;
scanf("%d",&n);
printf("%d\n",Num(n));
return 0;
}
Hehe, do you understand?