Home > Net >  Counting how many dozen in a number and also count its extra amount?
Counting how many dozen in a number and also count its extra amount?

Time:10-01

How do i make this program counts how many dozen in a number and also count its extra amount? This is what only I came up

#include<stdio.h>

int main()

{

float number, dozen;

printf("Please Enter any integer Value : ");

scanf("%f", &number);

dozen = number / 12;

printf("dozen of a given number %.2f is  =  %.2f", number, dozen);

return 0;

}

I dont know how i will get to count the dozen in a number, for example there is 45, i need to get 3 dozen and the extra will be 9.

CodePudding user response:

You prompt for an integer but then use floats. You already had the correct dozen calculation and just miss the modulo operator %. Reformatted code for readability.

#include <stdio.h>

int main() {
    printf("Please Enter any integer Value : ");
    int number;
    scanf("%d", &number);
    printf("dozen of a given number %d is %d with remainder %d\n",
        number,
        number / 12,
        number % 12
    );
    return 0;
}

and example execution:

Please Enter any integer Value : 14
dozen of a given number 14 is 1 with remainder 2

CodePudding user response:

Now that there is an accepted answer,
here's a small lesson in arithmetic (plagiarising @Allan Wind's answer):

#include <stdio.h>

int main() {
    printf("Please Enter any integer Value : ");
    int number;
    scanf("%d", &number);
    printf("dozen of a given number %d is %d with remainder %d\n",
        number,
        number / 12,
        number - number / 12
    );
    return 0;
}
  •  Tags:  
  • c
  • Related