C Program To Find Sum of Squares of Digits using Recursion


Write a C program to find sum of squares of digits of a positive integer number input by the user, using recursive function.

Example:

If user inputs num value as 123. Then we fetch the individual digits present in 123 i.e., 3, 2 and 1, square it and add it to get the final result.

i.e., (3 x 3) + (2 x 2) + (1 x 1) = 14.

So, sum of squares of digits of 123 is 14.

Video Tutorial: C Program To Find Sum of Squares of Digits using Recursion


[youtube https://www.youtube.com/watch?v=7YaJ2pIkzKc]

YouTube Link: https://www.youtube.com/watch?v=7YaJ2pIkzKc [Watch the Video In Full Screen.]

Source Code: C Program To Find Sum of Squares of Digits using Recursion

#include<stdio.h>

int square(int num)
{
    if(num == 0)
        return 0;
    else
        return( (num%10) * (num%10) + square(num/10) );
}

int main()
{
    int num;

    printf("Enter a positive integer number:\n");
    scanf("%d", &num);

    printf("Sum of squares of digits of %d is %d.\n", num, square(num));

    return 0;
}

Output:
Enter a positive integer number:
123
Sum of squares of digits of 123 is 14.

Source Code: C Program To Find Sum of Squares of Digits using Recursion and pow() method

#include<stdio.h>
#include<math.h>

int square(int num)
{
    if(num == 0)
        return 0;
    else
        return( pow((num%10), 2) + square(num/10) );
}

int main()
{
    int num;

    printf("Enter a positive integer number:\n");
    scanf("%d", &num);

    printf("Sum of squares of digits of %d is %d.\n", num, square(num));

    return 0;
}

Output:
Enter a positive integer number:
123
Sum of squares of digits of 123 is 14.

Here we are making use of pow() method present inside math.h library file. pow() takes base value as first argument and exponent value as its second argument.

Source Code: C Program To Find Sum of Squares of Digits using Recursion, Ternary/Conditional Operator and pow() method

#include<stdio.h>
#include<math.h>

int square(int);

int main()
{
    int num;

    printf("Enter a positive integer number:\n");
    scanf("%d", &num);

    printf("Sum of squares of digits of %d is %d.\n", num, square(num));

    return 0;
}

int square(int num)
{
    return( (num == 0) ? 0 : ( pow((num % 10), 2) + square(num/10) ));
}

Output 1:
Enter a positive integer number:
123
Sum of squares of digits of 123 is 14.

Output 2:
Enter a positive integer number:
2103
Sum of squares of digits of 2103 is 14.

Output 3:
Enter a positive integer number:
456
Sum of squares of digits of 456 is 77.

Output 4:
Enter a positive integer number:
2020
Sum of squares of digits of 2020 is 8.

Output 5:
Enter a positive integer number:
2021
Sum of squares of digits of 2021 is 9.

To know more about Ternary or Conditional Operator visit:
Ternary Operator / Conditional Operator In C.

Dry Run: Example

Lets assume that user has input num value as 123.

num(num%10)2num/10(num%10)2+square(num/10)
123(3)2129+square(12)
12(2)214+square(1)
1(1)201+square(0)

Value Returning – Control Shifting back.

Return ValueToResult
return 0;square(0)1+0=1
1square(1)4+1=5
5square(12)9+5=14

So, sum of squares of digits of 123 is 14.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *