Need Helping adding Square root code to an existing calculator. (Absolute begginer?)

Joined
Jan 13, 2025
Messages
1
Reaction score
0
#include <stdio.h>
#include <time.h>
#include <math.h>


int main() {
printf("Basic 'C LANGUAGE CALCULATOR!!!!'\n\n");
time_t t = time(NULL);
struct tm *currentTime = localtime(&t);
// Format 3: Month DD, YYYY HH:MM:SS
char months[12][10] = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
printf("%s %02d, %04d %02d:%02d:%02d\n\n",
months[currentTime->tm_mon], currentTime->tm_mday, currentTime->tm_year + 1900,
currentTime->tm_hour, currentTime->tm_min, currentTime->tm_sec);

float num1, num2, res;
int choice;
do
{
printf("1. Addition '+' \n\n");
printf("2. Subtraction '-' \n\n");
printf("3. Multiplication '*' \n\n");
printf("4. Division '/' \n\n");
printf("5. Square Root '-v-'\n\n");
printf("6. Exit/Cancel \n\n");

printf("Please Enter Numbers(1-6): \n\n");
scanf("%d", &choice);
if(choice>=1 && choice<=4)
{
printf("\nEnter: ");
scanf("%f %f", &num1, &num2);
}
switch(choice)
{
case 1:
res = num1+num2;
printf("\nResult = %0.2f", res);
break;
case 2:
res = num1-num2;
printf("\nResult = %0.2f", res);
break;
case 3:
res = num1*num2;
printf("\nResult = %0.2f", res);
break;
case 4:
res = num1/num2;
printf("\nResult = %0.2f", res);
break;
case 5: // This Is the option for Sqaure Root find a way to enter It In!
double number, result; // [It faults here, line 54!]
// Input a number
printf("Enter a number: ");
n scanf("%lf", &number);

// Calculate the square root
result = sqrt(number);

// Display the result
printf("The square root of %.2lf is %.2lf\n", number, result);

return 0; // This Is where It ends
default:
printf("\nWrong Choice!");
break;
}
printf("\n------------------------\n");
}while(choice!=5);
return 0;
}


Codeblocks IDE Is used, and yes It may not be the prettiest SRC code but can anyone tell me how or why or where I can add this feature.

Decided to jump In rather than watch basic C tutorials and apps.....

Most of this works except for line 54 or the Square Root bit!
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,249
Messages
2,571,244
Members
47,876
Latest member
Kiptechie

Latest Threads

Top