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.
-
UpdatedNov 23, 2014
-
Views82,495
Write a C program to find the roots of a quadratic equation
.
Write a C program to count the lines, words and characters in a given text.
Write a C program to find the sum of individual digits of a positive integer.
Write a C program, which takes two integer operands and one operator from the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use switch statement
)
Write a C program to generate all the prime numbers
between 1 and n, where n is a value supplied by the user.
Write C programs that use both recursive
and non-recursive functions
- To find the
factorial
of a given integer. - To find the
GCD
(greatest common divisor) of two given integers.