For Pset2: Readability, I'm writing a program in C that prompts the user for text input, analyzes how many letters, words, and sentences are in the text entered by the user, and calculates the grade level using the Coleman Liau index rounded to the nearest integer.

index = 0.0588 * L - 0.296 * S - 15.8

L = average number of letters per 100 words

S = average number of sentences per 100 words

Here is what my code reads as currently (I've even included lines of code I used for debugging purposes).

 // Initialize Coleman-Liau index formula variables
float L = (letters / words * 100);
float S = (sentences / words * 100);
float index = round(0.0588 * L - 0.296 * S - 15.8);
// Debug calculation
float index2 = (0.0588 * (96 / 23 * 100) - 0.296 * (1 / 23 * 100) - 15.8);
int i = (int)index;

// Print Grade level output calculated by index
if (i >= 16)
    printf("Grade 16+\n");
if (i < 1)
    printf("Before Grade 1\n");
    printf("Grade %i\n", i);
    // Print calculation debug
    printf("Grade %f\n", index2);
// Debug correct letter, word, and sentence count
printf("%i letters, %i words, %i sentences\n", letters, words, sentences);

I used floats so that the values don't truncate and I get a more accurate final answer. As for the debugging, I printed the count for letters, words, and sentences to see what was going on there, as well as initialized index2 to plug in exact values instead of variables without the round function to see the exact float answer before rounding to the nearest integer.

Here is the string I was testing:

In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.

According to the assignment, the text is supposed to calculate as Grade 7. I am getting "Grade 8" as the output.

The text consists of 96 letters, 23 words, and 1 sentence, thus the respective values were plugged in manually when finding the value for index2, which calculates as 7.720000. I did the math on Google Calculator, and I got an answer of 7.45565217391, which sounds like the more accurate value as it would round out to Grade 7 as it should according to the assignment.


Is there something wrong with how I'm getting the final answer? I've even tried doubles in hopes that the answer would read more accurately, but I still got the same answer. Why is it a different answer than what Google Calculator provided?


3 Answers 3


This is integer division. When you divide two integers, the result is an integer again, truncating the result, ignoring its fractional part.

You can turn it into floating point division by making at least one operand a floating point number, e.g. any of those:

float L = (float)letters / words * 100; // using a typecast
float L = 100f * letters / words;       // explicitly making 100 a float
float L = 100.0 * letters / words;      // 100.0 is always floating point

Multiplication/division (and also % modulo/remainder operator) is of same precedence and evaluated left to right, so any of the operands up to and including the division would have to be made floating point.

  • i just want to add that it's actually important to have the float number such as 100.0 at the beginning of the operation, because if you leave it at the end the first operation of letters/words is still an integer result
    – xunux
    Jan 13, 2020 at 21:58

Oh, I fought this battle too. Since letters is an int and words is an int, letters / words is an int, and the result is truncated. There are myriad ways to solve it, one way might be to "turn" the values into floats before the division, as with letters * 1.0.


After having a fight between ints and floats, this kind of worked for me:

float L = ((float)letters * 100 / words);
float S = ((float)sentences * 100 / words);

float index = 0.0588 * L - 0.296 * S - 15.8;

if (index >= 16)
    printf("Grade 16+\n");
else if (index < 1)
    printf("Before Grade 1\n");
    printf("Grade %.0f\n", roundf(index));

But the math is still not good for grades 2, 7 and 9.. Is it maybe because of my sentences?

int sentences = 0;
for (int i = 0; i < n; i++)
    if (text[i] == '!' || text[i] == '?' || text[i] == '.')

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .