Finding Sum of the digits a 5-digit number entered by user

We are going to find the solution of the given problem in C language syntax :-

In this solution we will use Modulus Operator(%) which returns the remainder after dividing the number.


Algorithm of the Solution

1. Ask for the user to input the 5-digit number.

2. Declare variables for the number , all its digits and sum.

3. Stepwise division of all the digits using Modulus Operator and save it in the variables.

4. Save sum in a variable of all the digits.

5. Display Sum of all the digits


/* Sum of the digits of 5-digit number */

#include <stdio.h>
int main()
{
     int number , a1, a2, a3, a4, a5 , sum;
   
     printf(“Enter the 5-digit number:”);
     scanf(“%d”, &number);
    
      a1 = number %10;
      number = number /10;

a2 = number %10;
      number = number /10;

      a3 = number %10;
      number = number /10;

      a4 = number %10;
      number = number /10;

       a5 = number %10;
      number = number /10;

      sum = a1+a2+a3+a4+a5;

       printf(“Sum of the digits of 5-digit number:%d” , sum);
       
       return 0;

}
    
     


    

Leave a comment

Design a site like this with WordPress.com
Get started