Write a program in C++ to calculate the n-th prime
- User
Write a program in C++ to calculate the n-th prime
- Assistant
Sure! Here's a C++ program to calculate the n-th prime number:
#include <iostream> bool isPrime(int num) { if (num <= 1) { return false; } for (int i = 2; i * i <= num; i++) { if (num % i == 0) { return false; } } return true; } int nthPrime(int n) { int count = 0; int num = 2; while (count < n) { if (isPrime(num)) { count++; } num++; } return num - 1; } int main() { int n; std::cout << "Enter the value of n: "; std::cin >> n; int nth_prime = nthPrime(n); std::cout << "The " << n << "-th prime number is: " << nth_prime << std::endl; return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.