Menu Close

Find the super digit of an integer using the rules

Simon is planning to summer vacation trip to Kodaikanal. His friends Tina, Nathan, and Irfan all learned about Samson’s plan. They said, we are also coming too, but Simon has only money to spend for him alone. Simon is very good at programming, So he puts a puzzle to his friends to avoid taking them to Kodaikanal and tells them that those who finish this can come with him. 

the puzzle is to find a super digit of an integer x using the following rules: For Given integers, you need to find the super digit of the integer.

 If x has only 1 digit, then its super digit is x. Otherwise, the super digit of x is equal to the super digit of the sum of the digits of x.

Functional Description
    For finding the digit and value, you have used the following expressions

      sum+=n%10;
      n/=10;

Explanation

For Example 1:
                 super digit of 9875 will be calculated as:
super-digit(9875) = super-digit(9+8+7+5) 
                  = super-digit(29) 
                  = super-digit(2+9)
                  = super-digit(11)
                  = super-digit(1+1)
                  = super-digit(2)
                  = 2.

Example 2 (for two numbers)

You are given two numbers n and k. The number p is created by concatenating the string n k times. Continuing the above example where n=1234, assume your value k=4. 
Your initial p=1234 1234 1234 1234 (spaces added for clarity).

superDigit(p) = superDigit(123412341234234)

1+2+3+4+1+2+3+4+1+2+3+4+4+3+2+1 = 40

superDigit(p) = superDigit(40)
4+0 = 4
superDigit(p) = superDigit(4)
All of the digits of p sum to 40. The digits of 40 sum to 4. 
4 is only one digit, so it’s the super digit.

Input:
The single line represents the integer n and k

Output:
single-line prints the super digit value.
#include <stdio.h>
int sumd(int n)
{ int k, sum=0; scanf("%d", &k);
  while(n) {
    sum+=n%10;
    n/=10;
  }
return sum*k;
}

int superd(int num) 
{
  int n=0;
  return (num%9 == 0) ? n = 9:num%9;
} 

{
  int main()
  int num;
  scanf("%d", &num); num= sumd(num); 
  printf("%d",superd(num));
  return 0;
}


INPUT_1:
1234  5

OUTPUT:
5


INPUT_2:
9875  4

OUTPUT:
8


INPUT_3:
5689  3

OUTPUT:
3


INPUT_4:
2365  2

OUTPUT:
5


ILLUSTRATION

Executed using gcc

Morae Q!