Search code examples
cfunctionprogram-entry-point

C++ main function and program


I am trying to get this code to run, i have tried these 2 ways but to no avail. The aim is to receive an input using scanf and print out the statement using printf. Error message i get is the Compiler just hangs.

Also, i am not to use any math functions such as square root or power

Test Cases and Expected answers

Input1: 1.41421356237

Output1: Area of the circle is 3.141590

Input2: 5.65

Output2: Area of the circle is 50.143703

Trial 1

#include <stdio.h>
double function(double a){
    double area;
    area = 3.14159*((a/2)*(a/2)*2);
    return area;
}

int main(void){

    double a;
    scanf("%f",a);
    double result = function(a);
    printf("Area of the circle is %f\n",result);
    return 0;
}

Trial 2

#include <stdio.h>
int main(void){


    scanf("%f",a);
    area = 3.14159*((a/2)*(a/2)*2);
    printf("Area of the circle is %f\n",area);
    return 0;
}

would appreciate any help, not sure why the function is not working. Thank you for your time.


Solution

  • In your first trial you need to change your scanf("%f",a); line to:

    scanf("%lf",&a);
    

    because correct type specifier for double is %lf which stands for "long float". Also you need to send &a as argument because scanf expects an address and not a value. Same thing with second trial.