Write a Program in Python to find sum of square of digits of a given number.

i=int(input("Enter Number to find sum of square of digits: "))
sum=0
while(i>0):
    sum=sum+(i%10)*(i%10)
    i=i//10
print("Sum of Square of each digits=",sum)

Output:

Enter Number to find sum of square of digits: 123
Sum of Square of each digits= 14

Explanation:

What does this program do?
It finds the sum of square of each digits of a number entered by the user.

Example:
If the number is 123, the sum is 1 + 4 + 9 = 14

Line-by-Line Explanation
1. Input from user
i = int(input(“Enter Number to find sum of digits: “))

  • Takes a number from the user
  • Converts it to an integer
  • Stores it in variable i

Example input:
Enter Number to find sum of square of digits: 123

2. Initialize sum
sum = 0

  • This variable sum will store the sum of square of digits.

3. While loop
while(i > 0):

  • Loop runs until the number becomes 0

4. Get last digit
sum = sum + (i % 10)*(i % 10)

  • i % 10 gives the last digit of the number

Example:

  • (123 % 10) * (123 % 10) = 3 * 3 = 9
  • (12 % 10) * (12 % 10) = 2 * 2 = 4
  • (1 % 10) * (1 % 10) = 1 * 1 = 1

5. Remove last digit
i = i // 10

  • Removes the last digit using integer division

Example:

  • 123 // 10 = 12
  • 12 // 10 = 1
  • 1 // 10 = 0
i value(i % 10) * (i % 10) sum value
12390 + 9 = 9
1249 + 4 = 13
1113 + 1 = 14
0loop ends


6. Output
print(“Sum of Square of each digits =”, sum)

Output:

Sum of Square of each digits = 14

Final Answer (for input 123)

12+22+32=1+4+9=141^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14

Leave a Comment

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

Scroll to Top