Question Write a C program that prompts the user for a five digit lab access cod
ID: 3682889 • Letter: Q
Question
Question Write a C program that prompts the user for a five digit lab access code, read it in as an unsigned integer then print to the screen each of the five digit access codes (with possible leading zeroes) that are equivalent (there are 32). Be aware that 1 and 2 are on the same button, as are 3 and 4, etc. 9 and 0 are on the same button, as well. The key access device has 0 and 1 on the first button, 2 and 3 on the next button and so on.
Program Objectives:
1. Prompt the user to enter a five digit access code (0 -99999). If the integer is outside the range prompt user again.
2. Determine the 32 different access codes that would be the same key pressed combination.
3. Print to the screen the 32 different codes.
Remember. There are 5 buttons. Button 1 = 1 or 2, button 2 = 3 or 4, ... button 5 = 9 or 0. So if someone's password is 11111, they press the first button 5 times. They would press the first button 5 times even if their password is 22222, or 21111, or 21212, etc... There are 32 different possibilities. 2^5 number of possibilities.
Explanation / Answer
#include <stdio.h>
int Numdigit(int num){
int count=0;
for(;num!=0;num=num/10)
count++;
return count;
}
int main(void) {
int num=0,d1,d2,d3,d4,d5,d[2];
int keys[10]={1,2,3,4,5,6,7,8,9,0};
int i=0,j=0,k=0,l=0,m=0;
printf("Enter 5 digit Number: ");
scanf("%d",&num);
if(Numdigit(num)!=5){
while(1){
printf("Try again.Enter 5 digit Number: ");
scanf("%d",&num);
if(Numdigit(num)<=5){
break;
}
}
}else{
d1=(num/10000)%10;
d2=(num/1000)%10;
d3=(num/100)%10;
d4=(num/10)%10;
d5=num%10;
if(d1==d2 &&d1==d3&&d1==d4&&d1==d5){
if(d1%2==0){
d[0]=keys[d1-2];
d[1]=keys[d1-1];
}else{
d[0]=keys[d1-1];
d[1]=keys[d1];
}
printf("Possible combinations ");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
for(k=0;k<2;k++){
for(l=0;l<2;l++){
for(m=0;m<2;m++){
printf("%d ",((d[i]*10000)+(d[j]*1000)+(d[k]*100)+(d[l]*10)+d[m]));
}
}
}
}
}
}
}
// your code goes here
return 0;
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.