Quiz on factoring quadratic expressions I am a beginner, and it is my first program in C. It works well, but I was wondering if there is a shorter way to do it, and how I could have done it better.

It is supposed to make a quadratic expression and ask the user to factor it. If the user factors it correctly (s)he proceeds to the next level, which is a slightly harder question. If (s)he cannot solve it correctly, (s)he stays on the same level, unless (s)he wants to exit.

# include &lt;stdio.h&gt;
# include &lt;stdlib.h&gt;
# include &lt;ctype.h&gt;
# include &lt;string.h&gt;
# include &lt;math.h&gt;
# include &lt;time.h&gt;

int main() {

  srand(time(0));
  int a, b, c, level=1, randMax=level*5;
  char ch;

  // generate the problem to ask
  int ask(){

    // generate non-zero random number for first coefficient
    // that is less than the level of the game, but not zero
    int nonZeroCoefficient(){
      int n = (rand()%(level+2) - (level));
      if ( n == 0 ){
      n++;
      }
      return n;
    }

    // generate non-zero random number

    int nonZeroRand(){
      int n=(rand()%randMax - (randMax));
      if (n == 0){
        n++;
      }
      return n;
    }

    int f = nonZeroCoefficient();
    int h = nonZeroCoefficient();
    int k = nonZeroRand();
    int g = nonZeroRand();

    a = f*h;
    b = (f*k + g*h);
    c = g*k;


    // shows the Level
    printf("\n\n\n\n --- Level %d ---", level);

    // generate the question
    printf("\nfactor the following expression\n%dx^2 %+dx %+d",a ,b, c);
    puts("\n\nEnter your answer in the form of (fX+g)(hX+k)");
    int inputF, inputG, inputH, inputK;
    printf("\nEnter f:");
    scanf("%d", &amp;inputF);
    printf("Enter g:");
    scanf("%d", &amp;inputG);
    printf("Enter h:");
    scanf("%d", &amp;inputH);
    printf("Enter k:");
    scanf("%d", &amp;inputK);

      if (inputF*inputH == a &amp;&amp; ((inputF*inputK+inputG*inputH== b) &amp;&amp; (inputG*inputK == c))){
        printf("\nRight on!\n");
        level++;

      }else{
        printf("\nSorry! your answer is not currect!\nthe currect answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \ntry again",f, g, h, k);
      }

    }


    do{
      ask();
      printf("\nDo you want to continue? (Y/N)");
      scanf (" %c", &amp;ch);
    } while(ch == 'y'|| ch == 'Y');

}
Where you have your own bounds for rand, calculate them directly. Instead of 

int nonZeroCoefficient(){
  int n = (rand()%(level+2) - (level));
  if ( n == 0 ){
  n++;
  }
  return n;
}


try 

int nonZeroCoefficient(){
  int n = (rand()%(level+2) - (level)) + 1;
  return n;
    }


(modify the first part of the equation to suit). This also reduces a skew or bias in your randomness where anything &lt;1 becomes one (i.e. the number 1 becomes over-represented in the random samples).

You also define your functions within your main(). This makes the code harder to read/understand and will make it harder to maintain. As a result, main() will only be:

int main() {
    do{
      ask();
      printf("\nDo you want to continue? (Y/N)");
      scanf (" %c", &amp;ch);
    } while(ch == 'y'|| ch == 'Y');

}


As you don't return anything from main() or ask(), void will suffice. The following are the functions that should be defined separately from your main code

int nonZeroCoefficient() // noting the comment I made about about generating the random number
int nonZeroRand() // noting the comment I made about about generating the random number
int ask()


Both nonZeroCoefficient() and nonZeroRand() functions will be single line functions that could be replaced by a single function

int newRand(int f1, int f2){ //f1 could be level+2 or rand Max, F2 could be level or randMax
      int n = (rand()%(f1) - (f2)) + 1;
      return n;
}