0% found this document useful (0 votes)
14 views2 pages

Calci

codes

Uploaded by

atharvdir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Calci

codes

Uploaded by

atharvdir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <math.h>

void showmenu();
double add(double a, double b);
double subtract(double a, double b);
double multiply(double a, double b);
double divide(double a, double b);
double power(double base, double exponent);
double squareRoot(double a);
double sine(double angle);
double cosine(double angle);
double tangent(double angle);

int main() {
int choice;
double num1, num2;

// Display the menu at the start


showmenu();

do {
printf("Enter your choice: ");
scanf("%d", &choice);

if (choice >= 1 && choice <= 5) {


printf("Enter your first number: ");
scanf("%lf", &num1);
printf("Enter second number: ");
scanf("%lf", &num2);
} else if (choice == 6) {
printf("Enter number: ");
scanf("%lf", &num1);
} else if (choice >= 7 && choice <= 9) {
printf("Enter angle in degrees: ");
scanf("%lf", &num1);
} else if (choice == 10) {
printf("Exiting...\n");
return 0;
} else {
printf("Invalid choice!\n");
continue; // Skip to next iteration for invalid choice
}

switch (choice) {
case 1:
printf("Result: %.2lf\n", add(num1, num2));
break;
case 2:
printf("Result: %.2lf\n", subtract(num1, num2));
break;
case 3:
printf("Result: %.2lf\n", multiply(num1, num2));
break;
case 4:
while (num2 == 0) {
printf("Error: Division by zero. Please enter a non-zero second
number: ");
scanf("%lf", &num2);
}
printf("Result: %.2lf\n", divide(num1, num2));
break;
case 5:
printf("Result: %.2lf\n", power(num1, num2));
break;
case 6:
printf("Result: %.2lf\n", squareRoot(num1));
break;
case 7:
case 8:
case 9: {
printf("Enter angle in degrees: ");
while (scanf("%lf", &num1) != 1) {
printf("Invalid input. Please enter a numeric value: ");
while (getchar() != '\n'); // Clear the input buffer
}
if (choice == 7) {
printf("Result: %.2lf\n", sine(num1 * M_PI / 180));
} else if (choice == 8) {
printf("Result: %.2lf\n", cosine(num1 * M_PI / 180));
} else if (choice == 9) {
printf("Result: %.2lf\n", tangent(num1 * M_PI / 180));
}
break;
}
}

} while (choice != 10);

return 0;
}

void showmenu() {
printf("\n--- Scientific Calculator ---\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Power\n");
printf("6. Square Root\n");
printf("7. Sine\n");
printf("8. Cosine\n");
printf("9. Tangent\n");
printf("10. Exit\n");
}

double add(double a, double b) { return a + b; }


double subtract(double a, double b) { return a - b; }
double multiply(double a, double b) { return a * b; }
double divide(double a, double b) { return a / b; }
double power(double base, double exponent) { return pow(base, exponent); }
double squareRoot(double a) { return sqrt(a); }
double sine(double angle) { return sin(angle); }
double cosine(double angle) { return cos(angle); }
double tangent(double angle) { return tan(angle); }

You might also like