Vivekananda College of Engineering and Technology
Department of Electronics & Communication Engineering
Back to Experiments
Experiment 12
C Programming Lab
Aim
To find the sum of digits of a given number.
Algorithm
1. Read number. 2. Extract digits and sum.
Use or Edit the given C template
#include <stdio.h> int main() { int n, sum = 0; printf("Enter a number: "); scanf("%d", &n); while(n > 0) { sum += n % 10; n /= 10; } printf("Sum of digits = %d", sum); return 0; }
Input data
Compile & Run
Output
Output will appear here after compilation.
C Programming Virtual Lab - Experiment 12