I already completed the previous steps just complete 6 and 7
I already completed the previous steps, just complete 6 and 7. Please use <stdio.h> library only, i have my code at the bottom, go from there. C++ language i used visual studios.
6. Now I want to make sure that nobody uses my calculator without authorization. So a user will have to enter a username and password, which if correct will show the welcome page and menu to the user else it will print the message like “Incorrect password. Authorization denied”. The correct password will be the reverse of the username. So after the user enters the username and password, you will check if the reverse of the password is the username. You will use a while loop to reverse your password. And you will you strcmp() to compare the username the user entered to the reverse of the password.
7. Below are the snapshots of the desired output screen. Figure 1: The above shows the program asking for username and password. We have entered a name in the username and the reverse of that name as the password. Figure 2: If the logging is successful you should print a message “Successful login” and then the menu should be displayed.
#include<stdio.h>
int main() {
char c;
do {
printf(\"Welcome to my calculator\ Please look at the menu below for your calculations\ \");
printf(\"1. Addition\ 2. Subtraction\ 3. Multiplication\ 4. Division\ 5. Modulus \ 6. Factorial \ \");
int n, x, y, f, i;
unsigned long long a = 1;
printf(\"Please press your choice from 1-6: \");
scanf_s(\"%d\", &n);
getchar();
switch (n) {
case 1:
printf(\"Enter first number: \");
scanf_s(\"%d\", &x);
getchar();
printf(\"Enter second number: \");
scanf_s(\"%d\", &y);
getchar();
printf(\"%d + %d = %d \ \", x, y, x + y);
break;
case 2:
printf(\"Enter first number: \");
scanf_s(\"%d\", &x);
getchar();
printf(\"Enter second number: \");
scanf_s(\"%d\", &y);
getchar();
printf(\"%d - %d = %d \ \", x, y, x - y);
break;
case 3:
printf(\"Enter first number: \");
scanf_s(\"%d\", &x);
getchar();
printf(\"Enter second number: \");
scanf_s(\"%d\", &y);
getchar();
printf(\"%d * %d = %d \ \", x, y, x*y);
break;
case 4:
printf(\"Enter first number: \");
scanf_s(\"%d\", &x);
getchar();
printf(\"Enter second number: \");
scanf_s(\"%d\", &y);
getchar();
if (y == 0) {
printf(\"Division by 0 is not allowed\ \");
}
else {
printf(\"%d / %d = %d \ \", x, y, x / y);
}
break;
case 5:
printf(\"Enter first number: \");
scanf_s(\"%d\", &x);
getchar();
printf(\"Enter second number: \");
scanf_s(\"%d\", &y);
getchar();
if (y == 0) {
printf(\"Division by 0 is not allowed\ \");
}
else {
printf(\"The Modulus of %d and %d is: %d \ \", x, y, x%y);
}
break;
case 6:
printf(\"Enter your number: \");
scanf_s(\"%d\", &f);
getchar();
if (f < 0)
printf(\"Error! Factorial of a negative number does not exist\ \");
else {
for (i = 1; i <= f; i++) {
a *= i;
}
printf(\"Factorial of %d = \ \", f, a);
}
break;
default:
printf(\"Error!! Enter a valid option 1-6\ \");
}
printf(\"Do you want to Continue? (Y/N): \");
scanf_s(\" %c\", &c);
getchar();
}while (c != \'N\');
return 0;
}
Solution
// C code
#include <stdio.h>
#include <string.h> // for strcmp
int main()
{
char c;
char password[100], username[100], temp;
int i, j = 0;
printf(\"Enter username: \");
scanf(\"%s\",username);
printf(\"Enter password: \");
scanf(\"%s\",password);
i = 0;
j = strlen(password) - 1;
//while loop to reverse your password
while (i < j)
{
temp = password[i];
password[i] = password[j];
password[j] = temp;
i++;
j--;
}
// If the logging is successful you should print a message
// “Successful login” and then the menu should be displayed.
if(strcmp(username,password) == 0)
{
printf(\"Successful login\ \");
}
// else end program for unsuccessful login
else
{
printf(\"Incorrect password. Authorization denied.\ \");
return 0;
}
do {
printf(\"Welcome to my calculator\ Please look at the menu below for your calculations\ \");
printf(\"1. Addition\ 2. Subtraction\ 3. Multiplication\ 4. Division\ 5. Modulus \ 6. Factorial \ \");
int n, x, y, f, i;
unsigned long long a = 1;
printf(\"Please press your choice from 1-6: \");
scanf(\"%d\", &n);
getchar();
switch (n) {
case 1:
printf(\"Enter first number: \");
scanf(\"%d\", &x);
getchar();
printf(\"Enter second number: \");
scanf(\"%d\", &y);
getchar();
printf(\"%d + %d = %d \ \", x, y, x + y);
break;
case 2:
printf(\"Enter first number: \");
scanf(\"%d\", &x);
getchar();
printf(\"Enter second number: \");
scanf(\"%d\", &y);
getchar();
printf(\"%d - %d = %d \ \", x, y, x - y);
break;
case 3:
printf(\"Enter first number: \");
scanf(\"%d\", &x);
getchar();
printf(\"Enter second number: \");
scanf(\"%d\", &y);
getchar();
printf(\"%d * %d = %d \ \", x, y, x*y);
break;
case 4:
printf(\"Enter first number: \");
scanf(\"%d\", &x);
getchar();
printf(\"Enter second number: \");
scanf(\"%d\", &y);
getchar();
if (y == 0) {
printf(\"Division by 0 is not allowed\ \");
}
else {
printf(\"%d / %d = %d \ \", x, y, x / y);
}
break;
case 5:
printf(\"Enter first number: \");
scanf(\"%d\", &x);
getchar();
printf(\"Enter second number: \");
scanf(\"%d\", &y);
getchar();
if (y == 0) {
printf(\"Division by 0 is not allowed\ \");
}
else {
printf(\"The Modulus of %d and %d is: %d \ \", x, y, x%y);
}
break;
case 6:
printf(\"Enter your number: \");
scanf(\"%d\", &f);
getchar();
if (f < 0)
printf(\"Error! Factorial of a negative number does not exist\ \");
else {
for (i = 1; i <= f; i++) {
a *= i;
}
printf(\"Factorial of %d = %llu\ \", f, a);
}
break;
default:
printf(\"Error!! Enter a valid option 1-6\ \");
}
printf(\"Do you want to Continue? (Y/N): \");
scanf(\" %c\", &c);
getchar();
}while (c != \'N\');
return 0;
}
/*
output1:
Enter username: ayush
Enter password: verma
Incorrect password. Authorization denied.
output2:
Enter username: ayush
Enter password: hsuya
Successful login
Welcome to my calculator
Please look at the menu below for your calculations
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Factorial
Please press your choice from 1-6: 1
Enter first number: 4
Enter second number: 5
4 + 5 = 9
Do you want to Continue? (Y/N): Y
Welcome to my calculator
Please look at the menu below for your calculations
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Factorial
Please press your choice from 1-6: 6
Enter your number: 5
Factorial of 5 = 120
Do you want to Continue? (Y/N): N
*/




