PART A · PRESCRIBED EXERCISE 01

Surface area and volume of a sphere

Understand the algorithm. Run the code.
Explain the result with confidence.

Read the programOpen the playground
Aim & definitionStep-by-step algorithmWorked sampleExtra test cases3 viva answers

LAB A01 · 01

Understand the task.

Aim: Surface area and volume of a sphere.

DEFINITION

Sphere formulas. A sphere is a three-dimensional surface whose points are all at the same distance r from its centre. Its surface area is 4πr²; the volume enclosed is (4/3)πr³.

Review the related theory in Unit 1.

LAB A01 · 02

Know what goes in and what comes out.

One radius, from 0 to 10000. The radius may contain a decimal part. Output area is in square units and volume in cubic units.

Sample input

3

Expected output

Surface area = 113.10
Volume = 113.10

Source programs read the documented input directly, without prompt text, so the same sample works in the embedded compiler’s STDIN field.

LAB A01 · 03

Plan the steps before writing code.

  1. Read radius as double and validate the range.
  2. Calculate area = 4 × π × radius × radius.
  3. Calculate volume = (4.0 / 3.0) × π × radius × radius × radius.
  4. Display both results rounded to two decimal places.

LAB A01 · 04

One complete program. Ready to run.

Surface area and volume of a spheremain.c
#include <stdio.h>
#define PI 3.141592653589793
int main(void)
{
    double radius, area, volume;
    if (scanf("%lf", &radius) != 1 || !(radius >= 0.0 && radius <= 10000.0)) {
        puts("Enter a radius from 0 to 10000.");
        return 1;
    }
    area = 4.0 * PI * radius * radius;
    volume = (4.0 / 3.0) * PI * radius * radius * radius;
    printf("Surface area = %.2f\nVolume = %.2f\n", area, volume);
    return 0;
}
EXPECTED OUTPUTSurface area = 113.10 Volume = 113.10

Try this programDownload A01-sphere.c
Compile on your own computer
gcc A01-sphere.c -o program

Run the generated program and enter the sample input. The examples use standard C90-compatible syntax and explicit function declarations. For these learning programs, enter numeric values representable in the stated C types and within the documented ranges.

LAB A01 · 05

Connect each statement to its purpose.

  • double preserves fractional input and avoids integer-only division.
  • 4.0 / 3.0 gives a fractional quotient; 4 / 3 would be integer division and produce 1.
  • PI is a symbolic constant. The formula multiplies the radius directly, so this program does not need math.h.
  • %.2f controls displayed precision; it does not change the stored double into an exact decimal value.

LAB A01 · 06

Follow the sample calculation.

Step or componentWhat happens
Radius3
Area4 × π × 3² = 36π ≈ 113.10
Volume(4/3) × π × 3³ = 36π ≈ 113.10

LAB A01 · 07

Check more than one input.

Try this input or caseExpected behaviour
0Both results are 0.00.
1Area 12.57; volume 4.19.
-1 or textRejected before calculation.

Time and storage

O(1) time and O(1) extra space: a fixed number of arithmetic operations.

Start with the sample, then test a boundary and a case that changes the result. A correct sample alone does not establish that every input is handled correctly.

LAB A01 · 08

Use the real C compiler.

Choose an example and load it into the embedded C compiler. Select I/O inside the compiler, check the sample STDIN, and press Run. Change the input or edit the source to explore a different case.

Sample input (STDIN)

3

Expected output

Surface area = 113.10
Volume = 113.10

Load the compiler when you’re ready to practise.

Your C workspace

Edit real C code and see real compiler output.

Compiler provided by OneCompiler. Opening the editor connects to that service; code and input you run are processed there. If the embed is unavailable, open the C compiler in a new tab and paste the source below.

View / copy the selected source
#include <stdio.h>
#define PI 3.141592653589793
int main(void)
{
    double radius, area, volume;
    if (scanf("%lf", &radius) != 1 || !(radius >= 0.0 && radius <= 10000.0)) {
        puts("Enter a radius from 0 to 10000.");
        return 1;
    }
    area = 4.0 * PI * radius * radius;
    volume = (4.0 / 3.0) * PI * radius * radius * radius;
    printf("Surface area = %.2f\nVolume = %.2f\n", area, volume);
    return 0;
}

LAB A01 · 09

Explain why the program works.

Why use 4.0 / 3.0?

Both operands are floating-point values, so division produces a fractional quotient. Integer 4 / 3 truncates to 1.

Do area and volume normally have the same numeric value?

No. They happen to match for radius 3 in this sample, and at radius 0. Their units are different.

What does %lf mean in scanf?

It expects the address of a double object. For printf, %f formats a double argument.

LAB A01 · 10

Build the next skill.

Read the viva answers, try the extra cases, and return to the lab index to mark this exercise as practised.