Objective:

Turtle Graphics is a popular way to introduce programming to beginners. In this approach, a ‘turtle’ is moved around the screen to create shapes and patterns. The turtle can be controlled using commands like forward, backward, turn, etc. This program demonstrates the use of Turtle Graphics in C, which can be used to draw simple geometric shapes and patterns.

Code:

#include 
#include 
#include 

void drawShapes()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

    // Drawing a Square
    for (int i = 0; i < 4; i++) {
        forward(100);
        right(90);
    }

    // Moving to new position
    penup();
    forward(150);
    pendown();

    // Drawing a Triangle
    for (int i = 0; i < 3; i++) {
        forward(100);
        right(120);
    }

    // Moving to new position
    penup();
    forward(150);
    pendown();

    // Drawing a Circle
    circle(50);

    getch();
    closegraph();
}

int main()
{
    drawShapes();
    return 0;
}

Explanation of the Program Structure:

The program uses the C language to create graphical shapes using the graphics.h library. Below is a breakdown of the code:

  • Libraries Included:
    • #include <graphics.h>: This library is required to use the graphics functions for drawing.
    • #include <conio.h>: This library provides console input/output functions (like getch()).
  • Initializing Graphics Mode: The initgraph() function initializes the graphics mode.
  • Drawing Shapes:
    • The forward() function moves the turtle forward by a specified distance.
    • The right() function turns the turtle to the right by a specified angle.
    • Shapes such as a square, triangle, and circle are drawn by repeating the forward() and right() functions in different patterns.
  • Graphics Closing: After drawing the shapes, the getch() function is used to wait for user input, and closegraph() is called to close the graphics window.

How to Run the Program:

To run this C program using Turtle Graphics, follow these steps:

  1. Install a C compiler that supports graphics, such as Turbo C++ or any other compatible IDE that supports graphics.h.
  2. Ensure that you have the correct path to the BGI directory for graphics.h.
  3. Compile the program in your IDE and run it. The graphics window will open, displaying the shapes.
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)