Saturday, August 13, 2016

C Code Snippets 1 - Calculating the Sum of Digits



ccode


1



/**
* 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 !
Read more course about C Code:



    No comments:

    Post a Comment