Q:

How to count iteration on a loop

I am teaching myself looping in C and I wanted to know how can the user try the password only 3 times?
When the first time is correct it returns correct,my current loop doesn't do the intended.
int password = 8795, guessed = 3;

while(1) {

    printf("\n\n");
    printf("Please Enter Your Password");
    scanf ("%d ", &guessed);

    if (guessed == password ) {
        printf("Correct Password");
    } else if(guessed != password) {
        printf("Please try again for the second time");
    } else {
        printf("Please try again for the last time");
        break;
    }
}

A:

To loop n times in C, use a for loop:
for(int i = 0; i < n; i++) {
  /* loop contents */
}

In this case, replace n with 3. In your case, however, it makes more sense while reading the code to do as @Michiel Uit Het Broek suggested and use a while loop:
int guessesLeft = 3;
while(guessesLeft > 0) {
  /* loop contents */
  guessesLeft--;
}

To exit the loop with the password is correct, use break:
if(guessed == password) {
  break;
}

