Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Making Calculations

Part of TutorialFundamentals

This program demonstrates getting input from the console window, making a simple calculation, and returing the output.

/* 
    Purpose: Calculate the area of a circle
    Author:  J C Calvarese
    License: Public Domain 
*/

import std.c.stdio; /* for scanf */
import std.stdio;   /* for writef/writefln */

const double pi = 3.141592654;

void main()
{
    writefln("Let's calculate the area of a circle.");

    writef("What is the radius? ");

    double radius;
    scanf("%lf", &radius);

    double area;
    area = pi * radius * radius;

    writefln("Area: %s", area);    
}

When you run it, the console screen should look something like this (if you provide a radius of 5):

Let's calculate the area of a circle.
What is the radius? 5
Area: 78.5398