Skip to main content

Sum of Digits of a Five Digit Number

 Sum of Digits of a Five Digit Number



Objective

The modulo operator,%, returns the remainder of a division. For example, 4 % 3 = 1 and 12 % 10 = 2. The ordinary division operator, /, returns a truncated integer value when performed on integers. For example, 5 / 3 = 1. To get the last digit of a number in base 10, use as the modulo divisor.

Task

Given a five-digit integer, print the sum of its digits.

Input Format

The input contains a single five digit number, .

code-

#include <stdio.h>
int main() 
{
  int num;
  int digit;
  int sum=0;
  scanf("%d", &num);
  
  while(num>0)
  {
    digit = num % 10;
    sum = sum+digit;
    num = num / 10;
  }
  printf("%d",sum);
return 0; 
}

Comments

Popular posts from this blog

Calculate the sum of elements in an array.

  HOW TO CALCULATE THE SUM OF THE ELEMENT IN A GIVEN ARRAY: - We will learn how to find the sum of elements in an array is an easy task when you know how to iterate through array elements. In this problem, we will explain your C approach to find the sum of array elements inputted by the user. Here is the solution to this query in C Languageguage.   ALGORITHMS: STEP 1:-   Input the size of the array and store the value in m and arr[m]. STEP 2 : - To store the sum of array elements, initialize a variable sum = 0. STEP 3: -To find the sum of all elements, iterate through each element  and add the current element to the sum. STEP 4: - Inside the loop add the current array element to sum i.e.                 sum = sum + arr[i] or sum += arr[i]. CODE:- #include <stdio.h> int  main() {      int  i,num,a[ 100 ],sum= 0 ;     printf( "Enter the value of number:...

How to find the Tringle is Valid or Not in C

 How to find the Tringle is Valid or Not in C:- Code:- #include <stdio.h> int  main() {        int  side_1,side_2,side_3;        int  a,b,c;       printf( "enter the value of sides:" ); scanf( "%d\n %d\n %d" ,&side_1,&side_2,&side_3);       a= ((side_1*side_1)==((side_2*side_2)+(side_3*side_3)));       b= ((side_2*side_2)==((side_1*side_1)+(side_3*side_3)));       c= ((side_3*side_3)==((side_1*side_1)+(side_2*side_2)));       if  (a || b || c)      {         printf( "The triangle is valid" );      }       else      {...

How to find Power of a number in C

  How to find Power of a number in C: - In this program we will calculate the power of a number using C programming. We want to calculate the power of any given number so you need to multiply that particular number power of time. Ex:- Let suppose number is 2 4  so we need to multiply with 4 times of 2. That is 2*2*2*2=16. ALGORITHMS:- Step 1 – Enter the base number, the number in which you just want to find the power of the number. Step 2 – Enter the exponential, the power of the number. Step 3 – Initialize while loop, while the exponential is not equal to zero.                  (i)  do temp*number and store it in the temp.                   (ii)  now reduce the exponential with -1.                   Here you got the power of the number. Step 4 – Now print the temp variable. Step 5-  Stop. CODE: - #include <stdio.h> int ...