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

C Program to Check Year is a Leap Year or Not

How to Check Year is a Leap Year or Not in C:- In this program we have to find the year is a leap year or not. Generally  we assume that year is exactly divisible by 4 is a leap year. But it is not only in this case 1900 is divisible by 4. But it  is not a leap so it that case we follows these conditions    It is exactly divisible by 100 If it is divisible by 100, then it should also exactly divisible by 4 And it is divisible by 400   These all conditions are true year is a leap year. ALGORITHMS:- Step 1 . Initialize variable “year” to find leap year. Step 2 . Take input from User. Step 3 . We use this condition ((year%4==0)&&            (year%100!=0)) || (year%400==0)) to check the year is Leap or not. S tep 4 . It is true display year i...

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      {...