Write a complete C program consisting of the main function and a void function. The program uses the void function to display the following menu: 1. Square root of sum 2. Logarithm to base e of sum of squares Please enter your choice The program then prompts for and reads two values x and y each of type double. It then uses a switch statement to do the following, based on the selected choice: • Choice 1: The program displays the value of sqrt(x + y) if x + y is greater than or equal to zero; otherwise it displays an appropriate error message. • Choice 2: The program displays the value of log(x * x + y * y) if x2 + y2 is greater than zero; otherwise it displays an appropriate error message.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <Windows.h>
#include <math.h>
void print_menu()
{
printf("1. Square root of sum \n2. Logarithm to base e of sum of squares \nPlease enter your choice : ");
}
int main()
{
int res = 0;
print_menu();
while (1)
{
res = 0;
scanf("%i", &res);
if (res == 1 || res == 2)
{
break;
}
printf("Please enter your choice (1 or 2) :");
}
double x=0, y=0;
printf("Please enter x : ");
scanf("%lf", &x);
printf("Please enter y : ");
scanf("%lf", &y);
if (res == 1)
{
if (x + y >= 0)
{
double tmp = sqrt(x + y);
printf("Result : %lf\n", tmp);
}
else
{
printf("X + Y is less than 0\n");
}
}
else if (res == 2)
{
if ((pow(x, 2) + pow(y, 2)) > 0)
{
double tmp = log(x * x + y * y);
printf("Result : %lf\n", tmp);
}
else
{
printf("X^2 + Y^2 is less or equal than 0\n");
}
}
system("Pause");
return 0;
}
Comments
Leave a comment