number of digits in a number in c code example

Example 1: print digits of a number in c

#include <stdio.h>

int main(){
    int num = 1024;

    while(num != 0){
        int digit = num % 10;
        num = num / 10;
        printf("%d\n", digit);
    }
  
    return 0;
}

Example 2: c how many digits has a number

digits = (number == 0) ? 1 : (log10(number) + 1);
//or
while (number > 0)
{
	number /= 10;
	digits++;
}
//see: https://ideone.com/P1h8Ne

Example 3: number in c

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

int main()
{
	int a, b;
    float c, d;
    scanf("%i %i", &a, &b);
    scanf("%f %f", &c, &d);
    int sum1 = a + b;
    int sum2 = a - b;
    float sum3 = c + d;
    float sum4 = c - d;
    printf("%i %i\n", sum1, sum2);
    printf("%.1f %.1f", sum3, sum4);
    return 0;
}

Tags:

C Example