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.

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