CateDoge CateDoge - 3 months ago 10
C Question

Hello! What is wrong here? I'm a beginner

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define PI 3.14159

/* give you the diameter , circumference and area */

int main()
{

float radius, area, circumference, diameter ;

printf( "\nI will calculate the diameter, area and circumference.\n" );
printf( "\nPlease enter a circle's radius: \n" );
scanf( "%f", &radius );
diameter = 2 * radius ; // diameter is 2 times radius
area = PI * pow ( radius, 2 ) ;
circumference = 2 * PI * radius
printf( "\nThe diameter is %d.\n", diameter );
printf( "\nThe circumference is %f.\n", circumference );
printf( "\nThe area is %f.\n", area );

printf( "\nThanks! See you next time!\n" );

return 0;
}

Answer

The ; at the end of an expression acts as a [ sequence point ] in C, The following statement

circumference = 2 * PI * radius  // Missing semicolon at the end

missed the ; at the end


Also, use %f format specifier for float with some precision information, eg

printf( "\nThe diameter is %d.\n", diameter );

should be

printf( "\nThe diameter is %0.2f.\n", diameter);

The .2 before f sets the precision to 2 places after the decimal point.

Comments