Write a program in C that prints out the prime numbers betwe
Write a program in C++ that prints out the prime numbers between 1 and 100. A prime number (or a prime ) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Solution
Okay So, to write a Basic C++ program to print the Prime numbers between 1 to 100 we will write a code as follows...
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int i,count, n;
for (n = 1; n <= 100; n++) /*Defining the Range from 1 to 100*/
{
count = 0;
for (int i = 2; i <= n/2; i++)
/*Taking a Variable i starting from 2, to check if the number is divisible by 2 or any number after that, and incrementing i */
{
if(n%i==0) /*If the number has a remainder 0 after dividing by i, then break or we can say Skip the Number */
{
count++;
break;
}
}
if(count==0 && n!=1) /*If n!=1 and Count is 0 so the number is only divisible by itself */
{
cout << \"Prime Numbers are:\"<<n << endl; /* Print Prime Numbers*/
}
}
return 0;
}
The Comments are also mentioned after each code, for your better understanding...
Thank You for using Chegg...
