Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS101 / KCS201 Programming for Problem Solving - Using C Language
Lab Exercises
17. Write a program to find the sum of digits of the entered number.
/*
File: Prgrm17.c
Author: Aditya Saini
Date: Sept 30, 2019
Description: Program to find the sum of digits of the entered number.
*/
#include <stdio.h>
int main (void)
{
int number;
int sum_of_digit;
int temp;
//Input number.
printf ("Input number: ");
scanf ("%d", &number);
int sum_of_digit;
int temp;
//Input number.
printf ("Input number: ");
scanf ("%d", &number);
//Finding the sum of digits.
temp = number;
sum_of_digit = 0;
while (temp != 0)
{
sum_of_digit = sum_of_digit + temp % 10;
temp = temp / 10;
}
//Output the result.
printf ("Sum of digits: %d", sum_of_digit);
sum_of_digit = 0;
while (temp != 0)
{
sum_of_digit = sum_of_digit + temp % 10;
temp = temp / 10;
}
//Output the result.
printf ("Sum of digits: %d", sum_of_digit);
};
Output
Input number: 12345
Sum of digits: 15
No comments:
Post a Comment
Please do not post spam links.