C Language

June 2, 2023

How to use sum of digits of any number for loop in C Language

Program to use sum of digits of any number for loop in C Language


#include <stdio.h>
int main()
{
    int n, sum = 0, rem;
    printf("enter a number : ");
    scanf("%d", &n);
    for (; n > 0; n /= 10)
    {
        rem = n % 10;
        sum += rem;
    }
    printf("sum of digits=%d\n", sum);
    return 0;
} 

output


enter a number : 433
sum of digits=10

by : Nadeem Khan

Quick Summary:

Program to use sum of digits of any number for loop in C Language copy #include <stdio.h> int main() { int n, sum = 0, rem; printf(“enter a number : “); scanf(“%d”, &n); for (; n > 0; n /= 10) { rem = n % 10; sum += rem; } printf(“sum of digits=%d\n”, sum); […]