This program finds and prints all the prime numbers between a given range of starting and ending numbers using a nested loop. Here is a brief explanation of the program:
This program uses a simple but inefficient algorithm to check for prime numbers. Instead of checking only odd numbers after 2, it checks all numbers between 2 and the current number. This leads to a lot of redundant checks, making the program slower than it needs to be.
Additionally, the program does not handle input validation, so it may fail or produce incorrect results if the user enters invalid input (such as non-numeric characters or negative numbers).
Overall, while this program works correctly and prints all prime numbers within a given range, there are some improvements that could be made to make it faster and more robust.
#include<iostream> using namespace std; int main() { int i,n=2,a,b,prime=0; cout<<"\nEnter the starting number:"; cin>>i; cout<<"\nEnter the ending number:"; cin>>b; while(i<=b) { prime=0; n=2; while(n<i) { if(i%n==0) { prime++; } n++; } if(prime==0) { cout<<"\n"<<i; } i++; } return 0; }To download raw file Click Here
Enter the starting number:1 Enter the ending number:20 1 2 3 5 7 11 13 17 19
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions