In: Computer Science
Code:
#include <stdio.h> // for accessing the standard input/output
// implementing the weighted digit function
int weighted_digit_sum(int n){
int digit=0;
int sum=0;
int count=1;
while(n){ //we use a while loop to get and apply added weight to it
digit = n%10; // getting the rightmost digit
if(count%2==0){ // if it falls on 2nd place, multiply by 2 or else 1
sum += digit*2;
}else{
sum += digit*1;
}
n=n/10;
count++;
}
return sum; // returning the weighted value
}
// implementing the reduced sum function
int reduced_sum(int x){
int y=x;
while(y>9){ // condition break the loop when sum<10 as we need only single digit sum
y=weighted_digit_sum(y);
}
return y;
}
// driver code
int main() {
int sm = 0;
int a[10];
for(int i=0; i<=10000; i++){
sm=reduced_sum(i);
a[sm]++;
}
for(int j=0; j<10; j++){
printf("%d\n", a[j]);
}
return 0;
}
Output: