Circle Area and Circumference in C

CCBeginner
Practice Now

Introduction

In this lab, we will learn how to write a C program to find the area and circumference of a circle. We will use the formula area = PI * radius * radius and circumference = 2 * PI * radius. We will be using the stdio.h library to read and write data.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/constants("`Constants`") c/BasicsGroup -.-> c/operators("`Operators`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123197{{"`Circle Area and Circumference in C`"}} c/comments -.-> lab-123197{{"`Circle Area and Circumference in C`"}} c/variables -.-> lab-123197{{"`Circle Area and Circumference in C`"}} c/data_types -.-> lab-123197{{"`Circle Area and Circumference in C`"}} c/constants -.-> lab-123197{{"`Circle Area and Circumference in C`"}} c/operators -.-> lab-123197{{"`Circle Area and Circumference in C`"}} c/user_input -.-> lab-123197{{"`Circle Area and Circumference in C`"}} c/memory_address -.-> lab-123197{{"`Circle Area and Circumference in C`"}} c/pointers -.-> lab-123197{{"`Circle Area and Circumference in C`"}} c/function_declaration -.-> lab-123197{{"`Circle Area and Circumference in C`"}} end

Initializing the Program

Firstly, we will initiate our program by creating a new file named main.c in the ~/project/ directory. We will begin by including the necessary libraries and initializing the main function.

#include<stdio.h>
int main()
{
  //program code
  return 0;
}

Declaring Variables

Next, we need to declare some variables, namely radius, PI, area, and circumference. PI will be a constant with the value of 3.14.

#include<stdio.h>
int main()
{
  int radius;
  const float PI = 3.14;
  float area, circumference;
  //program code
  return 0;
}

Reading Input

To get input from the user, we will use the scanf function. We will prompt the user to enter the value of radius.

#include<stdio.h>
int main()
{
  int radius;
  const float PI = 3.14;
  float area, circumference;
  printf("Enter the radius of the circle: ");
  scanf("%d", &radius);
  //program code
  return 0;
}

Calculating the Area

To calculate the area of the circle, we will use the formula PI * radius * radius. We will store the result in area variable.

#include<stdio.h>
int main()
{
  int radius;
  const float PI = 3.14;
  float area, circumference;
  printf("Enter the radius of the circle: ");
  scanf("%d", &radius);
  area = PI * radius * radius;
  //program code
  return 0;
}

Calculating the Circumference

To calculate the circumference of the circle, we will use the formula 2 * PI * radius. We will store the result in circumference variable.

#include<stdio.h>
int main()
{
  int radius;
  const float PI = 3.14;
  float area, circumference;
  printf("Enter the radius of the circle: ");
  scanf("%d", &radius);
  area = PI * radius * radius;
  circumference = 2 * PI * radius;
  //program code
  return 0;
}

Displaying Output

Lastly, we will display the result using the printf function. We will display the values of area and circumference.

#include<stdio.h>
int main()
{
  int radius;
  const float PI = 3.14;
  float area, circumference;
  printf("Enter the radius of the circle: ");
  scanf("%d", &radius);
  area = PI * radius * radius;
  circumference = 2 * PI * radius;
  printf("\nArea of Circle: %f", area);
  printf("\nCircumference of Circle: %f", circumference);
  return 0;
}

Complete Program Code

#include<stdio.h>
int main()
{
  int radius;
  const float PI = 3.14;
  float area, circumference;
  printf("Enter the radius of the circle: ");
  scanf("%d", &radius);
  area = PI * radius * radius;
  circumference = 2 * PI * radius;
  printf("\nArea of Circle: %f", area);
  printf("\nCircumference of Circle: %f", circumference);
  return 0;
}

Summary

In this lab, we learned how to write a C program to find the area and circumference of a circle by using the formula area = PI * radius * radius and circumference = 2 * PI * radius. We also learned how to read input, store results in variables, and display output using the printf function.

Other C Tutorials you may like