Skip to content
Exercise · Loops · lesson 7 of 8

Exercise: is the number prime

Write a function bool is_prime(int n) that returns true if n is a prime number and false otherwise. A prime number is greater than 1 and is divisible only by 1 and itself.

  • is_prime(97) → true
  • is_prime(91) → false
  • is_prime(2) → true

Constraints: n is from 0 to 10000; 0 and 1 are not prime.

is_prime.cpp — cpp
cpp · 4 lines
Ctrl+Enter run · Esc leave editor