C Program
#include <stdio.h> #include <string.h> int main() { char u[20], p[20]; printf("User: "); scanf("%s", u); printf("Pass: "); scanf("%s", p); // masking not native if (!strcmp(u, "admin") && !strcmp(p, "1234")) printf("Login Successful"); else printf("Access Denied"); }
C Output
User: admin
Pass: 1234Output:
Login Successful
C++ Program
#include <iostream> using namespace std; int main() { string u, p; cout << "User: "; cin >> u; cout << "Pass: "; cin >> p; // no mask here cout << ((u == "user" && p == "pass") ? "Login Successful" : "Access Denied"); }
C++ Output
User: user
Pass: passOutput:
Login Successful
JAVA Program
import java.util.*; public class Main { public static void main(String[] a) { Scanner s = new Scanner(System.in); System.out.print("User: "); String u = s.next(); System.out.print("Pass: "); String p = s.next(); // masking not available in console if (u.equals("root") && p.equals("toor")) System.out.println("Login Successful"); else System.out.println("Access Denied"); } }
JAVA Output
User: root Pass: toor
Output: Login Successful
Python Program
import getpass u = input("User: ") p = getpass.getpass("Pass: ") print("Login Successful" if u == "admin" and p == "4321" else "Access Denied")
Python Output
User: admin
Pass: (hidden while typing)Output:
Login Successful
In-Depth Explanation
Example
When the user enters admin for username and 4321 for password in the Python example, the getpass module prevents the password from being displayed on the screen. If the credentials are the same as predefined values, "Login Successful" is displayed. If they are different, "Access Denied" is displayed.
Real-Life Analogy
Picture logging into any application — Facebook, your bank, or a student portal. You type in a username and password, and the password is always masked behind *****. This is what getpass does in Python and what most apps emulate.
Why It Matters
Password masking is crucial to security. If the password is displayed on screen while being typed (such as in plain scanf or cin), it can be easily seen by someone else. Safe systems mask the input and only match behind the scenes. This program shows authentication logic, comparing the input, and in the case of Python — safe handling of passwords.
Learning Insights
With this program, you are practicing:
Processing multiple string inputs
Matching credentials
Safe password input (in Python with getpass)
Writing succinct but meaningful logic
Employing conditional checks to mimic authentication
Native password masking isn't implemented in C and C++ — you'd require platform-specific libraries or OS-level hacks to mask input (such as <conio.h> and getch() within Turbo C). But for the sake of simplicity, we mimic logic, with minimal code and portability.
Interview & Project Relevance
Login authentication logic pervades:
Web login forms
ATM PIN checks
User-based access systems
Admin panels and dashboards
In interviews, this question is traditionally posed to assess:
Handling of input
Conditional logic
Security practices awareness
In projects, it acts as the authentication gateway prior to providing access to any resource or action protected.
A password-masking user login system is perhaps the most useful and security-focused programming project. It teaches the fundamentals of credential authentication, and Python's getpass module demonstrates how actual software handles user input securely. These implementations in the shortest and most straightforward code possible in C, C++, Java, and Python facilitate novice programmers' comprehension of logic and significance of creating secure, user-sensitive systems for login and authentication processes.
Social Plugin