How to use ChatGPT to write code

ChatGPT can perform various tasks efficiently; it can answer queries, provide information on various topics, summarize and translate text into different languages, etc. Similarly, it can also write code.
ChatGPT's responses are entirely dependent on the precision of the prompts it is given. We must ensure that the required information is provided in the question statement.

To generate code, please keep the following points in mind.

Specify the language and framework

We must specify the language and framework in our question statement. Otherwise, ChatGPT will not know in which language we want the code. For example, suppose we want a simple web layout using the Angular framework. Instead of typing “Generate code for a simple web layout" in the prompt, we should write “Generate code for a simple web layout in Angular.”

Keep the question statement precise

ChatGPT is a well-trained model. It can understand our prompt with very few words and provide a valid response. For example, suppose we want to write code in Python that adds two numbers. Instead of writing “Generate code in Python that adds any two numbers and displays the output results on the console,” we should write "Generate code in Python that adds two numbers."

Note: We must provide the necessary information, otherwise ChatGPT might not accurately understand what we mean and give a different response from what we asked.

Always specify the approach

We must explicitly specify the approach that ChatGPT should use to generate the solution code. For example, suppose we want to write code in C++ that displays the Fibonacci sequence using the recursive approach. So, instead of writing “Generate a code in C++ that displays the Fibonacci sequence,” we should write “Generate code in C++ using the recursive approach that displays the Fibonacci sequence.”

Note: We should always attempt to write our own code first. As a final option, we can utilize ChatGPT's code to obtain some hints and guidance.

Example

Now, we will generate code in Python that checks whether a given number is prime or not. Let's stick to the points mentioned above and check what ChatGPT's response is:

Code generated by ChatGPT
Code generated by ChatGPT

Let's verify the code generated by ChatGPT:

def is_prime(number):
if number < 2: # check if number is less than 2
return False
for i in range(2, int(number ** 0.5) + 1): # check if number is divisible by any integer from 2 to sqrt(number)
if number % i == 0:
return False
return True
print(is_prime(7)) # True
print(is_prime(12)) # False

Explanation

  • Lines 2–3: First, it checks if the number is less than 2 since 0 and 1 are not prime numbers.

  • Lines 4–7: It loops through all integers from 2 to the number's square root (inclusive) and checks if the number is divisible by any of them. If it is, the method returns False immediately, indicating that the integer is not prime. The function returns True if none of the integers divide the number evenly. This indicates that the number is prime.

Unlock your potential: ChatGPT for everyday tasks, all in one place!

To continue your exploration of ChatGPT for everyday tasks, check out our series of Answers below:

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved