Prime Number program help.

0 Usama tariq · March 31, 2015
#include <iostream>
#include <stdio.h>
using namespace std;

main(){
int N,x,y;


cout << "Please enter the Integer : "; cin >> N;
x=N+1;
if(N==1)
cout << endl <<"1 is not a Prime Number";    
    
else if(N%1==0 && N%N==0 && N%x!=0){

cout << endl << "The Number you entered : " << N << " is a Prime Number" << endl;

for (int i=N;i>=2;i--){
y=i+1;
if(i%1==0 && i%i==0 && i%y!=0)
cout << endl << i;
}
}
else
cout << endl << "Its not a Prime Number";
}

The desire of this program is to check whether the number entered by user is Prime or not and then print all the prime numbers below that number.Please help.The program is buggy.

Post a Reply

Replies

Oldest  Newest  Rating
0 Usama tariq · April 1, 2015
Actually I did everything wrong.Btw ty for reply.
0 Dol Lod · March 31, 2015
What you say and what you do are incredibly different. 

Approach

1)Write a function to determine if a number is prime or not. Preferably, the function should be a bool type for simplicity since it simply indicates if a number is prime or not. Just use the % operator correctly to check if a number is divisible by something else. If the number is less than 2 it is not prime.

2) Now after N is inputted use the function to determine if the number is prime or not. Get rid of the case only handling one and just say it isn't prime. Then just iterate from two to every number up to N, using the isPrime function you wrote earlier to print out the value.

This is a very simple approach to the problem.
  • 1

C++

107,024 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator