C Code Snippets 1 - Calculating the Sum of Digits




Hello everyone, today I am going to post the flow-chart and the source code of a program that calculates the sum of digits of a number.







/**
* Author : Berk Soysal
*/

#include <stdio.h>
void main()
{
    int num, temp, digit, sum ;

    printf("Enter a TWO digit number: \n");
    scanf("%d", &num);

    if(num > 9 && num < 100)
    {

        temp = num;
        digit = num % 10;
        num /= 10;
        sum  = num + digit;

        printf("Given number = %d\n", temp);
        printf("Sum of the digits = %d\n", sum);
    }

    else
    {
        printf("\n\n ERROR !! Please Enter a TWO Digit Number !\n\n");
    }

}




Result





Please leave a comment if you have any questions !

Keywords: CCode Snippets
Author:

Software Developer, Codemio Admin


Disqus Comments Loading..