Write a C program to find the sum of individual digits of a positive integer.

Description:

Sum of the individual digits means adding all the digits of a number.

Ex: 123 sum of digits is 1+2+3 = 6

Algorithm:

Step 1: Start
Step 2: Read num as integer
Step 3: while (num > 0)
          begin
            dig ← num MOD 10;
            sumOfDigits ← sumOf Digits + dig;
            num ← num DIV 10;
         end
Step 4: Print sumOfDigits
Step 5: Stop

Flowchart:

Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
 int number = 0, digit = 0, sumOfDigits = 0;
 clrscr();
 printf("Enter any number\n ");
 scanf("%d", &number);
 while (number != 0)
 {
  digit = number % 10;
  sumOfDigits = sumOfDigits + digit;
  number = number / 10;
 }
 printf ("Sum of individual digits of a given number is %d", sumOfDigits);
 getch();
}

Input & Output:

Enter any number
1234
Sum of individual digits of a given number is 10

Viva Questions:

Q: What is the mean of sum of the individual digits?

Ans: Sum of the individual digits means adding each digit in a number.

Q: What is positive integer?

Ans: if the integer value is grater than zero then it is called positive integer.

Q: Define preprocessor ?

Ans: Before compiling a process called preprocessing is done on the source code by a program called the preprocessor.