GET EDUCATE

Contents
Basics of C Programming

⯈Basic C Programs

C Program to find area and circumference of circle

We start the program by reading the necessary inputs and then conclude the program with calculating the outputs.

Input: radius (double)

Output: area (double) and circumference (double)

Reading the inputs,

Let ‘r’ be the radius.

scanf("%lf", &r);

No variable should be used unless it is declared, and since we have used scanf function we need stdio.h

#include<stdio.h>
int main()
{
double r;
scanf("%lf",&r);
...
}

It is good to give a user interface before we read the input, therefore, we write,

#include<stdio.h>
int main()
{
double r;
printf("Enter radius r:");
scanf("%lf", &r);
......
}

Calculating the output,

area = 3.142 * r * r;

cir = 2 * 3.142 * r;

No variables should be used unless they are declared. We go up the ladder and declare area and cir.

#include<stdio.h>
int main()
{
double r;
double area, cir;
printf("Enter radius:");
scanf("%lf",&r);
area = 3.142 * r * r;
cir = 2 * 3.142 * r;
.......
}

After we calculate all the outputs, its the time to display them.

Therefore, the final program becomes.

#include<stdio.h>
int main()
{
double r;
double area, cir;
printf("Enter radius:");
scanf("%lf",&r);
area = 3.142 * r * r;
cir = 2 * 3.142 * r;
printf("area = %lf\n",area);
printf("circumference = %lf\n",cir);
return 0;
}

Output window of above code

output area circle

Memory diagram of above program

Scope after declaring all the variables,

scope area circle

Scope after reading the input (radius, r)

scope area circle

Scope after calculating the outputs,

scope area circle

After the main program completes the scope deactivates

scope area circle
Scroll to Top