Problem: Your preferred banking institution is requiring that you change your fi
ID: 3885251 • Letter: P
Question
Problem: Your preferred banking institution is requiring that you change your five digit PIN in the name of security. One requirement of your new PIN is that it cannot share any digit at the same place with your current PIN. Given both the current and new PIN print the total number of invalid digits. Both PIN values will be in the range [00000, 99999]. Example Execution #1 ('3' digit in the hundreds place is common to both PINs): Enter your current PIN: 12345 Enter your new PIN: 98310 Total number of invalid digits: 1 Example Execution #2: Enter your current PIN: 01010 Enter your new PIN: 10101 Total number of invalid digits: 0 Example Execution #3: Enter your current PIN: 12345 Enter your new PIN: 22345 Total number of invalid digits: 4 Example Execution #4: Enter your current PIN: 99999 Enter your new PIN: 00000 Total number of invalid digits: 0 Example Execution #5: Enter your current PIN: 13433 Enter your new PIN: 43133 Total number of invalid digits: 3 Example Execution #6: Enter your current PIN: 19183 Enter your new PIN: 28273 Total number of invalid digits: 1 Example Execution #7: Enter your current PIN: 12244 Enter your new PIN: 44241 Total number of invalid digits: 2 Please review the ASSIGNMENT REQUIREMENTS for this program. The use of selection, including logical and relational operators, is prohibited. The techniques necessary to solve this problem are similar to the examples found on pages 100-101 of your course notes packet.Explanation / Answer
C code:
#include <stdio.h>
int main()
{
while(1)
{
char s1[5]; char s2[5];
printf("%s", "Enter your current pin ");
scanf("%s",&s1);
printf("%s", "Enter new pin ");
scanf("%s",&s2);
int invalid_digits= 0;
for (int i = 0; i < 5 ; ++i)
{
if(s1[i] == s2[i])
{
invalid_digits = invalid_digits + 1;
}
else
{
continue;
}
}
printf("%s %d ","Total number of invalid digits = ", invalid_digits);
}
return 0;
}
Sample Output:
Enter your current pin 11111
Enter new pin 22222
Total number of invalid digits = 0
Enter your current pin 22211
Enter new pin 11111
Total number of invalid digits = 2
C++ code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
while(true)
{
string s1 ,s2;
cout << "Enter your current pin ";
cin >> s1;
cout << "Enter new pin ";
cin >> s2;
int invalid_digits= 0;
if(s1.length() == s2.length())
{
for (int i = 0; i < s1.length(); ++i)
{
if(s1[i] == s2[i])
{
invalid_digits = invalid_digits + 1;;
}
else
{
continue;
}
}
cout << "Total number of invalid digits = " << invalid_digits << endl << endl;
}
else
{
cout << "Error, length of both pins should be same! " << endl ;
}
}
return 0;
}
Sample Output:
Enter your current pin 12345
Enter new pin 54321
Total number of invalid digits = 1
Enter your current pin 11111
Enter new pin 12121
Total number of invalid digits = 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.