Learn how to take user input and calculate the area of a circle using the formula πr².
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².
In this practical, you will write a program that:
scanf()printf()The value of π (pi) is approximately 3.14159. We will use a constant variable for pi.
float data type for radius and area because they can have decimal values.
Run the program:
Sample Input & Output:
| Formula | Description | Example |
|---|---|---|
| Area = π × r² | Area of a circle | r = 5 → Area = 3.14159 × 5 × 5 = 78.54 |
| π (Pi) | Mathematical constant | Approximately 3.14159 |
| r² | Radius squared (r × r) | 5² = 25 |
scanf() function is used to read user input.%f format specifier is used for floating-point numbers.%.2f format specifier displays the result with 2 decimal places.
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.
& before the variable name in scanf() (e.g., &radius). This tells the program where to store the input value.
pow(radius, 2) from the math.h library to calculate radius squared, but radius * radius is simpler and faster.
ایک C/C++ پروگرام لکھنا جو صارف سے دائرے کا رداس ان پٹ کے طور پر لے اور فارمولا Area = π × r² کا استعمال کرتے ہوئے اس کا رقبہ حساب کرے۔
اس عملی میں، آپ ایک پروگرام لکھیں گے جو:
scanf() کا استعمال کرتے ہوئے ان پٹ پڑھتا ہےprintf() کا استعمال کرتے ہوئے نتیجہ ظاہر کرتا ہےπ (pi) کی قیمت تقریباً 3.14159 ہے۔ ہم pi کے لیے ایک مستقل متغیر استعمال کریں گے۔
float ڈیٹا ٹائپ استعمال کریں کیونکہ ان میں اعشاریہ کی قیمتیں ہو سکتی ہیں۔
پروگرام چلائیں:
نمونہ ان پٹ اور آؤٹ پٹ:
| فارمولا | وضاحت | مثال |
|---|---|---|
| Area = π × r² | دائرے کا رقبہ | r = 5 → Area = 3.14159 × 5 × 5 = 78.54 |
| π (Pi) | ریاضیاتی مستقل | تقریباً 3.14159 |
| r² | رداس مربع (r × r) | 5² = 25 |
scanf() فنکشن صارف کے ان پٹ کو پڑھنے کے لیے استعمال ہوتا ہے۔%f فارمیٹ اسپیسیفائر فلوٹنگ پوائنٹ نمبرز کے لیے استعمال ہوتا ہے۔%.2f فارمیٹ اسپیسیفائر نتیجہ کو 2 اعشاریہ جگہوں پر ظاہر کرتا ہے۔
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو دائرے کا رداس ان پٹ کے طور پر لیتا ہے اور فارمولا πr² کا استعمال کرتے ہوئے اس کا رقبہ حساب کرتا ہے۔ آپ نے scanf() کا استعمال کرتے ہوئے ان پٹ لینا اور printf() کا استعمال کرتے ہوئے آؤٹ پٹ ظاہر کرنا سیکھ لیا ہے۔
scanf() میں ہمیشہ متغیر کے نام سے پہلے & استعمال کریں (مثال: &radius)۔ یہ پروگرام کو بتاتا ہے کہ ان پٹ ویلیو کو کہاں ذخیرہ کرنا ہے۔
math.h لائبریری سے pow(radius, 2) استعمال کر سکتے ہیں، لیکن radius * radius آسان اور تیز ہے۔