Write Programs in C Plus Plus (C++)

Start Writing Programs in C++ with the help of well documented C++ code examples, algorithms, flowcharts, and sample C++ Programs!

Saturday, March 24, 2018

C++ Program Calculate Sum of Squares of Digits of Given Number

Q: Write a C++ Program to input an integer number. Calculate the sum of squares of digits of          this number.

      For example, if given number is 123 then answer will be Sum squares of  digits = 14
      because 12+22+32 = 1+4+9 = 14.
C++ Program to Calculate sum of squares of digits of given number
C++ Program to Calculate sum of squares of digits of given number


C++ Code of Sum of Squares of Digits of a given number Program


/*
Program to input a number, then display sum of squares of its digits.
(c) Www.EasyCppProgramming.Blogspot.Com
*/
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
     int num, temp, sum=0, r, q;
     clrscr();
     cout<<"Enter a number to add square of its Digits=";
     cin>>num;
     temp = num;
     while ( temp>0 )
     {
       q = temp/10;
       r = temp % 10;
       sum = sum +  ( r * r);

       temp = q;
     }
     cout<<"\n The sum of squares of digits of given number "<<num<<"  =  "<<sum;
     getch();
     }

The Working of  C++ Code of Add Digits of a given number Program

This program is of reverse number logic.

1. We will divide 123 by 10 and get quotient=12, r = 3
2. add r*r=3*3  into sum, so sum=9

3. Now put q =12 into dividend number
4. so divide 12 by 10, quotient=1 and remainder r=2.
5. Add r*r=2*2 into sum, so sum will become 9+4 =13.
6. now put q=1 into dividend number

7. Now divide 1 by 10, quotient=0 and r=1.
8. so add r*r=1*1 into sum, and sum becomes 13+1= 14
9. now put quotient=0 into dividend number called temp variable
10. so that while loop test condition while (temp>0) becomes false and loop terminates.

11. Therefore display the answer sum of squares of digits of number 123 = 14
C++ Program to Calculate sum of squares of digits of given number output of execution
C++ Program to Calculate sum of squares of digits of given number execution

/*
Program to input a number, then display sum squares of its digits.
(c) Www.EasyCppProgramming.Blogspot.Com
*/
#include<iostream.h>     // include header files
#include<conio.h>

void main()
{
     int num, temp, sum=0, r, q;
     clrscr();
     cout<<"Enter a number to add its Digits=";
     cin>>num;                 // input number
     temp = num;             // copy number into temp variable
     while ( temp>0 )      // loop while temp is greater than zero
     {
       q = temp/10;      // calculate quotient = temp number / 10, if num is 123 then q=12
       r = temp % 10;  //  calculate remainder r = 123 % 10, r = 3
       sum = sum + ( r * r );  // sum = 0 + 3x3 =0+9
                    // so square of last digit 3 of 123 is added in sum  variable
                               
       temp = q;      // set next time dividend     temp=12
     }
     cout<<"\n The sum of digits of given number "<<num<<"  =  "<<sum; //sum=14
     getch();
     }

Search C++ Programs and Tutorials