EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that takes the radius of a circle as input from the user and calculates its area using the formula Area = π × r².

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of variables, input/output, and arithmetic operations

📖 Introduction

In this practical, you will write a program that:

  • Asks the user to enter the radius of a circle
  • Reads the input using scanf()
  • Calculates the area using the formula: Area = π × r × r
  • Displays the result using printf()

The value of π (pi) is approximately 3.14159. We will use a constant variable for pi.

💡 Tip: Use float data type for radius and area because they can have decimal values.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named circle_area.c.
  3. Write the following code:
/* Practical 15: Program to calculate area of a circle */ #include <stdio.h> int main() { // Declare variables float radius, area; const float PI = 3.14159; // Ask user for radius printf("Enter the radius of the circle: "); scanf("%f", &radius); // Calculate area: Area = PI * radius * radius area = PI * radius * radius; // Display the result printf("Radius: %.2f\n", radius); printf("Area of the circle: %.2f\n", area); return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. float radius, area; — Declares variables for radius and area.
  3. const float PI = 3.14159; — Declares a constant for pi.
  4. printf("Enter the radius: "); — Prompts the user to enter the radius.
  5. scanf("%f", &radius); — Reads the radius value from the user.
  6. area = PI * radius * radius; — Calculates the area.
  7. printf() — Displays the radius and area.

Step 3: Save, Compile, and Run

  1. Save the file (Ctrl + S).
  2. Open the terminal and compile using the command:
gcc circle_area.c -o circle_area

Run the program:

./circle_area

Sample Input & Output:

Enter the radius of the circle: 5 Radius: 5.00 Area of the circle: 78.54

Step 4: Test with Different Inputs

  1. Test 1: Radius = 7 → Area = 153.94
  2. Test 2: Radius = 10 → Area = 314.16
  3. Test 3: Radius = 2.5 → Area = 19.63

📊 Formula Used

Formula Description Example
Area = π × r² Area of a circle r = 5 → Area = 3.14159 × 5 × 5 = 78.54
π (Pi) Mathematical constant Approximately 3.14159
Radius squared (r × r) 5² = 25

📊 Observations

  • The program successfully takes input from the user.
  • The area is calculated correctly using the formula.
  • The scanf() function is used to read user input.
  • The %f format specifier is used for floating-point numbers.
  • The %.2f format specifier displays the result with 2 decimal places.

✅ Result

You have successfully written a C program that takes the radius of a circle as input and calculates its area using the formula πr². You have learned how to use scanf() to take input and printf() to display output.

📌 Important: Always use & before the variable name in scanf() (e.g., &radius). This tells the program where to store the input value.
💡 Tip: You can use pow(radius, 2) from the math.h library to calculate radius squared, but radius * radius is simpler and faster.

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو صارف سے دائرے کا رداس ان پٹ کے طور پر لے اور فارمولا Area = π × r² کا استعمال کرتے ہوئے اس کا رقبہ حساب کرے۔

🛠️ آپ کو کیا چاہیے

  • Visual Studio Code یا کوئی بھی C/C++ کمپائلر
  • متغیرات، ان پٹ/آؤٹ پٹ، اور ریاضی کے عمل کی معلومات

📖 تعارف

اس عملی میں، آپ ایک پروگرام لکھیں گے جو:

  • صارف سے دائرے کا رداس درج کرنے کو کہتا ہے
  • scanf() کا استعمال کرتے ہوئے ان پٹ پڑھتا ہے
  • فارمولا استعمال کرتے ہوئے رقبہ حساب کرتا ہے: Area = π × r × r
  • printf() کا استعمال کرتے ہوئے نتیجہ ظاہر کرتا ہے

π (pi) کی قیمت تقریباً 3.14159 ہے۔ ہم pi کے لیے ایک مستقل متغیر استعمال کریں گے۔

💡 مشورہ: رداس اور رقبہ کے لیے float ڈیٹا ٹائپ استعمال کریں کیونکہ ان میں اعشاریہ کی قیمتیں ہو سکتی ہیں۔

📝 اقدامات

پہلا قدم: پروگرام لکھیں

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. circle_area.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 15: Program to calculate area of a circle */ #include <stdio.h> int main() { // Declare variables float radius, area; const float PI = 3.14159; // Ask user for radius printf("Enter the radius of the circle: "); scanf("%f", &radius); // Calculate area: Area = PI * radius * radius area = PI * radius * radius; // Display the result printf("Radius: %.2f\n", radius); printf("Area of the circle: %.2f\n", area); return 0; }

دوسرا قدم: کوڈ کی وضاحت

  1. #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
  2. float radius, area; — رداس اور رقبہ کے لیے متغیرات کا اعلان کرتا ہے۔
  3. const float PI = 3.14159; — pi کے لیے ایک مستقل کا اعلان کرتا ہے۔
  4. printf("Enter the radius: "); — صارف کو رداس درج کرنے کا کہتا ہے۔
  5. scanf("%f", &radius); — صارف سے رداس کی قیمت پڑھتا ہے۔
  6. area = PI * radius * radius; — رقبہ کا حساب کرتا ہے۔
  7. printf() — رداس اور رقبہ ظاہر کرتا ہے۔

تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں

  1. فائل کو محفوظ کریں (Ctrl + S
  2. ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc circle_area.c -o circle_area

پروگرام چلائیں:

./circle_area

نمونہ ان پٹ اور آؤٹ پٹ:

Enter the radius of the circle: 5 Radius: 5.00 Area of the circle: 78.54

چوتھا قدم: مختلف ان پٹ کے ساتھ ٹیسٹ کریں

  1. ٹیسٹ 1: رداس = 7 → رقبہ = 153.94
  2. ٹیسٹ 2: رداس = 10 → رقبہ = 314.16
  3. ٹیسٹ 3: رداس = 2.5 → رقبہ = 19.63

📊 استعمال شدہ فارمولا

فارمولا وضاحت مثال
Area = π × r² دائرے کا رقبہ r = 5 → Area = 3.14159 × 5 × 5 = 78.54
π (Pi) ریاضیاتی مستقل تقریباً 3.14159
رداس مربع (r × r) 5² = 25

📊 مشاہدات

  • پروگرام کامیابی سے صارف سے ان پٹ لیتا ہے۔
  • رقبہ کا حساب فارمولا کا استعمال کرتے ہوئے درست طریقے سے کیا جاتا ہے۔
  • scanf() فنکشن صارف کے ان پٹ کو پڑھنے کے لیے استعمال ہوتا ہے۔
  • %f فارمیٹ اسپیسیفائر فلوٹنگ پوائنٹ نمبرز کے لیے استعمال ہوتا ہے۔
  • %.2f فارمیٹ اسپیسیفائر نتیجہ کو 2 اعشاریہ جگہوں پر ظاہر کرتا ہے۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو دائرے کا رداس ان پٹ کے طور پر لیتا ہے اور فارمولا πr² کا استعمال کرتے ہوئے اس کا رقبہ حساب کرتا ہے۔ آپ نے scanf() کا استعمال کرتے ہوئے ان پٹ لینا اور printf() کا استعمال کرتے ہوئے آؤٹ پٹ ظاہر کرنا سیکھ لیا ہے۔

📌 اہم: scanf() میں ہمیشہ متغیر کے نام سے پہلے & استعمال کریں (مثال: &radius)۔ یہ پروگرام کو بتاتا ہے کہ ان پٹ ویلیو کو کہاں ذخیرہ کرنا ہے۔
💡 مشورہ: آپ رداس مربع کا حساب کرنے کے لیے math.h لائبریری سے pow(radius, 2) استعمال کر سکتے ہیں، لیکن radius * radius آسان اور تیز ہے۔
⬅ Back to Practical Notebooks