C Exercises: Check the password until it is correct

C Basic Declarations and Expressions: Exercise-36 with Solution

Write a C program to read a password until it is valid. For wrong password print "Incorrect password" and for correct password print, "Correct password" and quit the program. The correct password is 1234.

C Code:

#include <stdio.h>
int main() {
    int pass, x = 10; // Initialize variables for password and loop control

    while (x != 0) {
        printf("\nInput the password: ");
        scanf("%d", &pass); // Read the password input

        if (pass == 1234) {
            printf("Correct password"); // If the password is correct, print a success message
            x = 0; // Set x to 0 to exit the loop
        } else {
            printf("Wrong password, try another"); // If the password is incorrect, prompt for another attempt


    return 0;

Sample Output:

Input the password: 1234                                                                             
Correct password


C Programming Flowchart: Check the password until it is correct

C Programming Code Editor:

Previous: Write a C program to check if two numbers in a pair is in ascending order or descending order.
Next: Write a C program to read the coordinate(x, y) (in Cartesian system) and find the quadrant to which it belongs (Quadrant -I, Quadrant -II, Quadrant -III, Quadrant -IV).

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.