Magical Number ICD01-Editorial

#PROBLEM LINK:

[problem][1]

Author: Yasha Jadwani

Tester : Prabhsimran Kaur

Editorialist: Prabhsimran Kaur

DIFFICULTY :

Simple

PREREQUISITES:

Recursions

PROBLEM:

You are given the stall number and you have to determine whether the number is magical or not .
An example to determine a number is magical or not -:
1=>

let a number 7

                  7^2=
                   49
                 /    \
                /      \
           4^2=16   +  9^2=81    (16+81=97)
                   97  
                    /\
                   /  \
             9^2=81 + 7^2=49    (81+49=130)
                   130
                   /| \
                  / |  \
              1^2=1 |  0^2=0
                    |
                  3^2=9

EXPLANATION:

This question requires you to recursively call the sum of square of individual numbers untill the square of number equalizes 1.
bound function of recursive call:
// untill the number passed for checking equality with one is a single digit number.

@ recursive function :

if the number has been called once and if it is a single digit number

#if the digit is 1=>YES

if the number has been called once and if it is a single digit number

# if the digit is other than 1=>NO

@ to check a number(n) is a single digit number
#n/10==0

SETTER’S SOLUTION:

mag( int n,int i)
//n is the number we pass each time to check if it is equal to 1
     {
        int m=n;
        int d,s;
        s=0;
        if(n==1 && i==1)
//here i keeps the track whether the number has been called once (i=1)or not (i=0)
        {
            printf("YES\n");
            return 0;
        }
        else if(n/10==0 && i==1)
        {
            printf("NO\n");
            return 0;
        }
        else
        {
            while(m!=0)
            {
                d=m%10;
//here the individual number is fetched from a non singular number
                s+=d*d;
//here the sum of square of individual number of non singular number is calculated(7^2=49=>4^2+9^2=97=>.....)
                m=m/10;
            }
            mag(s,1);
//calling of funtion with the sum and i 
        }
    }