Feb 17, 2022

Use of mathematical operations and implementation (C language )

Problem-Finding the sum of digits of five digit number.

 #include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{

    int n;
    scanf("%d", &n);
    // Complete the code to calculate the sum of the five digits on n.
    int num;
    int base = 10000;
    int sum = 0;
    // printf("%d\n",(num-(num%base))/base );
    // printf("%d\n",(num%base));

    for (int i = 1; i <= 5; i++)
    {
        num = (n - (n % base)) / base;
        n = (n % base);
        base /= 10;
        sum += num;
    }
    printf("%d\n", sum);

    return 0;
}

No comments:

Post a Comment

Comparing two files in C language.

Operations on More than one File.    # include < stdio.h > # include < stdlib.h > # include < string.h > int main ( int ...