Untitled

 avatar
unknown
plain_text
4 years ago
1.1 kB
2
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
    float num_input;
    char waste;
    int flag, flag_positive;
    int number, int_num;

    printf("Please enter an integer: ");

    while((flag = scanf("%f", &num_input)) != 1 || num_input - (int)num_input != 0){
        // The user has either entered a non-integer input or a non-digit
        if(flag == 0){
            scanf("%s", &waste);
            /* for non-number input, scanf of number fails to load the input, an extra scanf of
             string is needed to remove the previous input */
             ///Take note of the datatype
        }
        printf("You have entered an invalid input. Please try again: ");

    }

    int_num = (int)num_input;
    number = abs(int_num);

    int temp, sum = 0, digits = 0;
    while(number > 0){
        temp = number%10;
        sum += temp;
        digits++;
    }
    printf("The number entered is %d, it has %d digits. The sum of all digits is %d.", int_num, digits, sum);


    return 0;
}
Editor is loading...