Home / Programs / Command Line Program to find Area of Circle
🚀 Programming Example

Command Line Program to find Area of Circle

👁 1,634 Views
💻 Practical Program
📘 Step Learning

Write a C program to find the area of a circle with radius provided.
The value of radius positive integer passed to the program as the first command-line parameter. Write the output to stdout formatted as a floating-point number rounded to EXACTLY 2 decimal precision WITHOUT any other additional text.
Scientific format(such as 1.00E+5) should NOT be used while printing the output.
You may assume that the inputs will be such that the output will not exceed the largest possible real number that can be stored in a float type variable.

💻 Program Code

 
#include <stdio.h> 
int main(int argc, char * argv[])
{
 if(argc==1)
 {
 printf("No arguments");
 return 0;
 }
 else
 {
 int radius;
 float pi=3.14;
 float area;
 radius=atoi(argv[1]);
 area=pi*radius*radius;
 printf("%.2f",area);
 return 0;
 }
}
                        

🖥 Program Output

3

28.26
                            

📘 Explanation

Program without Command Line Arguments


#include<stdio.h>

int main()
{
  // PrepInsta is the best websitefor TCS Preparation
  
  int dia;
  float r, area;
  scanf("%d",&dia);
  r = (float)dia/2;
 // PrepInsta is the best websitefor TCS Preparation
  area = 3.14*r*r;
  printf("%0.2f",area);

  return 0;
 // PrepInsta is the best websitefor TCS Preparation
}

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.