Prime Numbers
A prime number is an integer greater than 1 that has exactly two divisors, 1 and itself.
The first ten prime numbers are
- 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.
Integers that are not prime are called composite numbers.
C++ program to find prime numbers in a given range
#include <iostream>
#include <iomanip> // std::setw
using namespace std;
int main()
{
int num,i,count,n;
cout << "Enter max range: ";
cin >> n;
for(num = 1;num<=n;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
cout << num << setw(3);
}
return 0;
}
/*
OUTPUT:
Enter max range: 30
2 3 5 7 11 13 17 19 23 29
*/
C++ Program to Check If the number is Prime or not
#include <iostream>
using namespace std;
int main()
{
int i,no;
cout<<"Enter any num: ";
cin>>no;
if(no==1)
{
cout<<"Smallest prime num is 2";
}
for(i=2;i<no;i++)
{
if(no%i==0)
{
cout<<"Not prime number";
break;
}
}
if(no==i)
{
cout<<"Yes, Number is Prime";
}
return 0;
}
/*
OUTPUT:
Enter any num: 17
Yes, Number is Prime
*/
C++ Program to Print next Prime number
#include <iostream>
using namespace std;
int main()
{
int i,j=2,number;
cout<<"Enter any number: ";
cin>>number;
cout<<"Next prime number: ";
for(i=number+1;i<3000;i++)
{
for(j=2;j <i;j++)
{
if(i %j==0)
{
break;
} // if
} // for
if(i==j || i==1)
{
cout<<"\t"<<i;
break;
} // if
} // outer for
return 0;
}
/*
OUTPUT:
Enter any number: 17
Next prime number: 19
*/
- C++ Simple Programs And Examples
- C++ – Hello World Program
- C++ – Simple calculator
- C++ – Even and Odd
- C++ – Swap two numbers
- C++ – Prime Number
- C++ – Find Perfect Number
- C++ – Factorial of Number
- C++ – Fibonacci Series
- C++ – Human Resource Management Program
- C++ – Calculate number of characters, words, sentences
- C++ – Subtract two Strings
- C++ – Processing of the Students structure
- C++ – Program with Matrices
- C++ – Calculate Equation
- C++ – Arrange Numbers in Ascending order
- C++ – Check Armstrong Number
- C++ – HCF and LCM
- C++ – Square Root of a Number
- C++ – Cube Root of a Number
- C++ – ASCII Code for Characters and numbers
- C++ – Check Palindrome Number
- C++ – Bubble sort
- C++ – Random Number Generator
- C++ – Sum of ODD Numbers in the Given Range
- C++ – Display current date and time
- Formula Based Programs
- C++ – Leap Year
- C++ – Surface Area and volume of cone
- C++ – Solve Quadratic equation
- String Based Programs
- C++ – String into Upper case or lower case
- C++ – Concatenate Strings
- Array Based Programs
- C++ – Program with Matrices
- Print Any Patterns
- C++ – Print 5 rows of 10 Stars (*)
- C++ – Half Pyramid of Stars (*)
- C++ – Half Pyramid of numbers
- C++ – Print Triangle of Stars
- C++ – Display Reverse pyramid.
- C++ – Print Alphabet Pattern
- C++ – Diamond of Star
- C++ – Pascal Triangle
- C++ – Floyd Triangle
- C++ Conversion
- C++ – Convert decimal to Hexadecimal
- C++ – Decimal to Binary
- C++ Sorting algorithms & Techniques
- C++ – Bubble Sort
- C++ – Insertion Sort
- C++ – Selection Sort
- C++ – Merge Sort
- C++ – Quick Sort
- C++ Handling Files
- C++ – How to get Current Directory in Linux/Windows
- C++ – How Create a Text File and Write in It
Leave a Reply