Hello everyone! Today we will explore an interesting and practical C++ programming topic: how to write a program to output all prime numbers between 1 and 100. Prime numbers are a very important concept in mathematics; understanding them enhances our programming skills and deepens our understanding of mathematics. Are you ready? Let’s start this journey!
1. What are Prime Numbers?
Before we start writing code, we need to clarify what a prime number is. Simply put, a prime number is a natural number that can only be divided by 1 and itself. For example, 2, 3, 5, and 7 are prime numbers because they cannot be evenly divided by any other numbers. However, 4 is not a prime number because it can be divided by 2.
Tip
-
1 is not a prime number, and 2 is the only even prime number. -
Prime numbers are very important in many fields of mathematics and computer science, such as encryption algorithms.
2. Basic Idea to Determine Prime Numbers
To determine whether a number is prime, we can use the following method:
-
If the number is less than 2, return “not a prime number” directly. -
Start from 2 and check if the number can be divided by any integer from 2 to its square root. -
If no number can divide it, then the number is prime.
With this, we have laid the groundwork for our coding.
3. Writing the C++ Program
Next, let’s write a C++ program together that outputs all prime numbers between 1 and 100. The code is as follows:
#include <iostream> // Include input-output library
#include <cmath> // Include math library for square root calculation
bool isPrime(int num) {
if (num < 2) return false; // Numbers less than 2 are not prime
for (int i = 2; i <= sqrt(num); i++) { // Check from 2 to square root
if (num % i == 0) return false; // If divisible, not prime
}
return true; // Otherwise, it is prime
}
int main() {
std::cout << "Prime numbers between 1 and 100 are:" << std::endl;
for (int num = 1; num <= 100; num++) { // Loop through numbers from 1 to 100
if (isPrime(num)) { // Check if it is prime
std::cout << num << " "; // Output prime number
}
}
std::cout << std::endl; // New line
return 0; // End of program
}
Code Explanation
-
The isPrime
function: This function is used to determine whether a number is prime. We use a loop to check all numbers from 2 to the square root of that number. -
The main
function: We loop through all numbers from 1 to 100 and call theisPrime
function to check if each number is prime. If it is, we print it.
4. Practical Applications
A program that determines prime numbers can be useful in many practical applications. For example, in cryptography, prime numbers are used to generate keys; in data analysis, prime numbers can help us better understand the structure of data. Mastering this basic concept will lay a solid foundation for your future studies.
5. Practice Time
Now it’s time to practice! Please try to extend this program so that it can output prime numbers in any range, not just from 1 to 100. For example, you can allow the user to input two numbers and then output all prime numbers between those two numbers.
Example Code Framework
#include <iostream>
#include <cmath>
bool isPrime(int num) {
if (num < 2) return false;
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
int main() {
int start, end;
std::cout << "Please enter the range (two numbers):";
std::cin >> start >> end;
std::cout << start << " to " << end << " prime numbers are:" << std::endl;
for (int num = start; num <= end; num++) {
if (isPrime(num)) {
std::cout << num << " ";
}
}
std::cout << std::endl;
return 0;
}
Notes
-
When entering the range, ensure the starting number is less than the ending number. -
When handling user input, remember to perform appropriate input validation.
Conclusion
Today, we learned how to use C++ to write a program that outputs all prime numbers between 1 and 100. Through this exercise, you not only mastered the use of functions but also deepened your understanding of loops and conditional statements.
Friends, today’s C++ learning journey ends here! Remember to code and feel free to ask me questions in the comments. Wishing everyone a happy learning experience, and may your C++ skills improve continuously!