Write a C program to take a 5-digit number as input and calculate the sum of its digits.
Program Output
/* * C Program to find sum of digits of a number */ #include <stdio.h> #include <conio.h> int main(){ int number, digitSum = 0; printf("Enter a number : "); scanf("%d", &number); while(number != 0){ /* get the least significant digit(last digit) of number and add it to digitSum */ digitSum += number % 10; /* remove least significant digit(last digit) form number */ number = number/10; } printf("Sum of digits : %d\n", digitSum); getch(); return 0; }
Program Output
Enter a number : 12345 Sum of digits : 15
No comments:
Post a Comment