DEV Community

Cover image for How to write a simple C program to check if a number is prime using Visual Studio code.
Abdulsalam Yusuf
Abdulsalam Yusuf

Posted on

How to write a simple C program to check if a number is prime using Visual Studio code.

Introduction

C is a procedural language. it is renowned for its portability, efficiency, and simple syntax.

Visual Studio Code is a development tool. Inbuilt with a code editor that supports development processes and more.

In this article, you’ll learn to write a simple C program to check if a number is prime using Visual Studio Code.

Prerequisite

  • Basic knowledge of C programming and its syntax.

  • Visual Studio Code is installed on your machine. Go to the official website to install it.

Terminologies to understand to aid with this lesson

  • Prime number - This is a number that can only be divided by one and itself, for example, 2, 3, 5, 7… etc.

  • Composite number - is a positive integer divided by 1 and other numbers apart from itself, for example, 4, 6, 8, 9… etc.

Steps

Let’s dive in by breaking down the processes involved in this article.

Step 1 - Download Extensions

Extensions are software that extends and enhances the functionality of the editor.

Launch Visual Studio Code and click on Extensions. It can also be accessed using the shortcut keys ctrl+shift+X or the command key for MAC. Install the following extensions:

  1. C/C++ Extension Pack:

The C/C++ Extension Pack provides a comprehensive development environment for C and C++ programming in Visual Studio Code. This extension pack includes essential tools, language support, debugging capabilities, and extra features tailored for C and C++ development.

An image of C/C++ Extension Pack installation

  1. C/C++ Extension:

The C/C++ extension is a core component within the extension pack. It focuses on providing language support for C and C++ in Visual Studio Code. This includes features such as syntax highlighting, code suggestions, debugging support, etc.

An image of C/C++ extension installation

  1. Code Runner:

The Code Runner extension is a tool that allows the execution of code snippets or entire files

An image of Code Runner extension installation

Step 1.1 - Change Settings

Click the gear icon on the bottom-left corner of your screen and click on Settings. Or, use the shortcut keys ctrl+,, the command key for Mac.

An image of the gear icon and Settings

Next, enable the Code-runner: Run In Terminal feature.

Input “run code in terminal” in the search bar and check the Code-runner: Run In Terminal checkbox.

An image of the input “run code in terminal” in the search bar
The Code-runner: Run In Terminal feature allows user input in the terminal. In this lesson, user input handling is required. i.e. if your code prompts for user input, you can provide it within the terminal.

Step 2 - Open a folder

A folder has many uses such as file organization, namespace management, etc.

Navigate the Explorer tab or use the shortcut keys ctrl+shift+E, the command key on Mac.

Click Open Folder and proceed to create a new folder on your file explorer/manager.

An image of Open Folder

Name the folder whatever you want or you can name it “C”. Click Select Folder after.

An image of New Folder and Select Folder

Step 2.1 - Create a file

A file serves many functions, in this lesson, you’ll be storing your source code in a file.

Click on your folder, and beside it, three icons will appear, click the New File icon.

An image of the New File icon

Name your file with the .c extension to specify the C source code. Click enter to save the file.

An image of a file with the .c extension

Step 3 - Code

Your workspace is set up, Next, you’ll write your code to check if a number is prime. In your editor, follow the steps below:

Step 3.1 - Header file

Include the standard input/output and math libraries.



#include <stdio.h>
#include <math.h>


Enter fullscreen mode Exit fullscreen mode

#include <stdio.h> includes the standard input/output library. This library is used for input/output operations.

#include <math.h> includes the math library. This library is used for mathematical functions.

Step 3.2 - Entry point

Include the int main() function.



#include <stdio.h>
#include <math.h>

int main()
{

}


Enter fullscreen mode Exit fullscreen mode

The int main function is the entry point, it returns an integer value(often 0) to state success. An entry point is where program execution starts.

Step 3.3 - Declare variables and user input

Declare Variables:



#include <stdio.h>
#include <math.h>

int main()
{
    int number;
    int i, value1, value2, count = 0;
}


Enter fullscreen mode Exit fullscreen mode

int number stores the user input. While int i, value1, value2, count = 0; declare variables for loop control, intermediate values, and a counter.

Next, add code to accept user input:



#include <stdio.h>
#include <math.h>

int main()
{
    int number;
    int i, value1, value2, count = 0;
    printf("Enter a number: ");
    scanf("%d", &number);
}


Enter fullscreen mode Exit fullscreen mode

printf("Enter a number: ") prompts the user to enter a number. scanf("%d", &number); reads the user input and stores it.

Step 3.4 - Find the square root



#include <stdio.h>
#include <math.h>

int main()
{
    int number;
    int i, value1, value2, count = 0;
    printf("Enter a number: ");
    scanf("%d", &number);

    value1 = ceil(sqrt(number));
    value2 = number;
}


Enter fullscreen mode Exit fullscreen mode

value1 = ceil(sqrt(number)); calculates the square root of number using the sqrt function. The ceil function approximates the square root to the nearest integer.

value2 = number is initialized to the value of number. This will be used in later calculations or conditions. i.e. the prime number checking logic.

Step 3.5 - Loop iteration



#include <stdio.h>
#include <math.h>

int main()
{
    int number;
    int i, value1, value2, count = 0;
    printf("Enter a number: ");
    scanf("%d", &number);

    value1 = ceil(sqrt(number));
    value2 = number;

    for(i = 2; i <= value1; i++)
    {
        if(value2 % i == 0)
            count = 1;
    }
}


Enter fullscreen mode Exit fullscreen mode

The loop checks the divisibility of value2 by i. If true, count is set to 1, else it remains 0.

Step 3.6 - Check if the number is prime



#include <stdio.h>
#include <math.h>

int main()
{
    int number;
    int i, value1, value2, count = 0;
    printf("Enter a number: ");
    scanf("%d", &number);

    value1 = ceil(sqrt(number));
    value2 = number;

    for(i = 2; i <= value1; i++)
    {
        if(value2 % i == 0)
            count = 1;
    }
    if((count == 0 && value2 != 1) || value2 == 2 || value2 == 3)
    {
        printf("%d is a prime number", value2);
    }
    else
    { 
        printf("%d is not prime number", value2);
    }
    return 0;
}


Enter fullscreen mode Exit fullscreen mode

The if statement checks if the number is prime. If the conditions are true, it prints the number is a prime number, if false, it prints it is not a prime number. return 0 indicates the program has been executed successfully.

Step 4 - Run Code

To run your code, click on the Run Code icon on the top-left corner of your screen(first icon). Or use the shortcut keys ctrl+alt+N, the command key on Mac.

Step 5 - Terminal Input

After clicking the Run Code icon, the terminal will appear at the bottom of your screen. You will see a prompt that says “Enter a number” on the terminal. Please input a number in the terminal when prompted.

If the number is prime, the output below is displayed;

An image of the terminal

Otherwise, this output below is displayed;

An image of the terminal

Conclusion

In this tutorial article, you wrote a C program to check if a number is prime. Also leveraging the capabilities of Visual Studio code as a powerful integrated development environment(IDE). You covered inputs, loops, and math functions. All basics for a solid foundation in C programming.

If you enjoyed this tutorial and would like to learn more on this blog, feel free to follow, comment, and leave claps. Happy Coding!

Let’s connect on LinkedIn.

Top comments (0)