C Program
#include <stdio.h> #include <string.h> int main() { char phone[20]; printf("Enter phone number: "); scanf("%s", phone); int len = strlen(phone); for (int i = 0; i < len - 4; i++) { phone[i] = '*'; } printf("Masked Phone Number: %s\n", phone); return 0; }
C Output
Input: 9876543210 Output: ******3210
C++ Program
#include <iostream> using namespace std; int main() { string phone; cout << "Enter phone number: "; cin >> phone; for (int i = 0; i < phone.size() - 4; i++) { phone[i] = '*'; } cout << "Masked Phone Number: " << phone << endl; return 0; }
C++ Output
Input: 9876543210 Output: ******3210
JAVA Program
import java.util.Scanner; public class MaskPhone { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter phone number: "); String phone = sc.next(); String masked = "*".repeat(phone.length() - 4) + phone.substring(phone.length() - 4); System.out.println("Masked Phone Number: " + masked); } }
JAVA Output
Input: 9876543210 Output: ******3210
Python Program
phone = input("Enter phone number: ") masked = "*" * (len(phone) - 4) + phone[-4:] print("Masked Phone Number:", masked)
Python Output
Input: 9876543210 Output: ******3210
In-Depth Explanation
Example
When we enter a phone number such as 9876543210, the software conceals all numbers except the last four, and it gives us ******3210. It is the common way in apps such as Paytm, Amazon, Google Pay, and nearly every other website where personal information needs to be protected.
Real-Life Analogy
Imagine when you shop using your debit card or credit card at a store. The receipt usually shows something like XXXX-XXXX-1234 instead of the full card number. The reason is simple: security. The system needs to confirm enough of your number for you to recognize it, but it should never reveal the whole thing in case someone else sees it. Masking a phone number functions in the same manner—it conceals your personal information but retains the last digits so that you may verify your identity.
Why It Matters
This tiny program initiates you into string handling, one of the most frequent operations in software development. Mastering how to access parts of strings, swap characters, and slice information serves you in mastering much more complex operations like password encryption, anonymization of user information, or inputting sensitive information into databases.
On interviews, these issues challenge your grasp of loops, string slicing, substrings, and security-minded thinking. Lots of rookie programmers know how to program loops, but few can think through applying them to practical security scenarios.
Learning Insights
This exercise educates you to the fact that safeguarding user data is not only about coding algorithms but also about data privacy thinking. An masked phone number verifies (last digits identical) without misuse (complete number not revealed). You reinforce your understanding of string operations, indexing, and repeat functions across languages by solving this.
Real-World Application
You can spot masking in OTP screens, login mechanisms, online banking, and nearly all e-commerce websites. Whenever you notice something like **Your OTP has been sent to ****4321, the backend is executing similar logic you have written here in these basic programs.
SEO-Optimized Closing
Learning to mask a phone number in C, C++, Java, and Python is a great practice for learners who wish to learn string handling and data security fundamentals. This code is commonly employed in real-life applications like e-commerce, banking, and online registrations where protection of user identity is crucial. Knowing how to conceal sensitive information while exposing data partially assists in building secure systems and sets you apart from the crowd during coding interviews. Solving such problems not only enhances your coding, but also provides you with hands-on experience of real-world situations, so it is a must-know idea for every programmer.
Social Plugin