A Learning Platform

SUCCESS

Success = Discipline + Consistency + Sacrifice.
Discipline - in what you do.
Consistency - in what you do.
Sacrifice - for what you do.

                   -- Manjunatha C. P. --

C Program-Area and Circumference of Circle using Functions

C Program using Functions to calculate Area and Circumference of circle.


In this program we are going to find the area and perimeter or circumference of circle using Functions. Here we are using 2 functions area() and circumference().


Definition:


A Function is a group of statements that together perform a common task. Every C program must have at least one function, which is main().


Description:


PI = 3.14

r = radius

area = PI * r * r

circumference = 2 * PI * r


Algorithm to find area and circumference of circle using Functions:


STEP 1: START

STEP 2: INITIALIZE value of Global variable PI

              PI=3.14

STEP 3: Read the value of radius

              READ r

STEP 4: CALL the function area()

              area(radius)

                RETURN ( a = PI * r * r )

STEP 5: CALL the function circumference()

              circumference(radius)

                RETURN ( c = 2 * PI * r )

STEP 6: Print the values of area and circumference

              PRINT area, circumference

STEP 7: END


C Program to calculate the Area and Circumference of Circle using Functions


#include<stdio.h>

#include<conio.h>

const float PI = 3.14;

//prototype declaration

float area(float r);

float circumference(float r);

void main() {

    float r;

    printf("Enter radius: ");

    scanf("%f", &r);

    printf("Area of circle: %f\n", area(r));

    printf("Circumference of circle: %f\n", circumference(r));

    getch();

}

/* return area of the circle */

float area(float r) {

  return PI * r * r;

}

/* return circumference of the circle */

float circumference(float r) {

  return 2 * PI * r;

}


output:


Enter radius of circle : 3


Area of circle : 28.260000

Circumference of circle : 18.840000


Additional information:


A function definition has following parts:


Return Type − A function may or may not return a value. The return_type represents the type of value the function is going to return.

Function Name − It is any valid identifier. The function name and the parameter list together constitute the function header.

Parameter list− A parameter is a placeholder. The parameter list refers to the type, order and number of parameters.

Function Body − The function body contains group of statements that perform common task.