-3

This is a program to find whether the entered number is a prime number or not. There is no errors but i always get the the answer as not a prime number for any number.

#include<iostream.h>
#include<conio.h>
void main()
 { clrscr();
   int n,count = 0,i; 
   cout<<"enter the number n";
   cin>>n;
   for(i=1;i<=n;i++){
      if(n%i==0)
         count++;
   }
   if(count==2){
     cout<<"It is a prime number";}
   else {
     cout<<"It is not a prime number";}
   getch();
   }  
  • 4
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Aug 02 '16 at 14:00
  • 4
    Please do yourself a favor and use **modern** books/tutorials and compilers. What you have up there smells too much of last century. – Mat Aug 02 '16 at 14:01
  • This code won't even compile – DimChtz Aug 02 '16 at 14:05
  • @Mat This is what our school has taught and school wants. So i can't blame em' cuz they'll tell study for your self, this is what we teach! :( –  Aug 02 '16 at 14:05
  • @DimChtz It compiles properly for me in turbo c++ and runs too! –  Aug 02 '16 at 14:06
  • 5
    @NeerajKarthikeyan: well point them at this comment please: "Hello Neeraj's teachers, what you're doing is a disgrace, a huge disfavor for your students & the IT profession in general." – Mat Aug 02 '16 at 14:07
  • Funny enough, the code as posted works for me. :D ... BTW, Turbo C++ has debugger included IIRC (I may be wrong, it's pure archaeology, I think Turbo C++ was used somewhere around 1676, before Christ). You should get something decent like current gcc/clang under linux, and learn also C++11/14/17 extensions, plus modern C++ style. This hurts my eyes (`int` for everything, while you only work with unsigned numbers, just for start, I'm not even going to continue). – Ped7g Aug 02 '16 at 14:31
  • 3
    After cleaning it up a bit (drop `conio.h`, include `iostream` instead of `iostream.h`, make `main` return `int`, drop calls to `getch()` and `clrscr()`), your program actually works for me for a couple of sample values. – Frerich Raabe Aug 02 '16 at 14:34
  • @NeerajKarthikeyan Well, it doesn't compile for me: [link](https://ideone.com/tz1Grn). By the way, I believe that `conio.h` is platform specific header, which may not be portable.. – Algirdas Preidžius Aug 02 '16 at 14:34
  • 1
    @AlgirdasPreidžius `conio.h` is pretty much dead, http://stackoverflow.com/questions/8792317/why-cant-i-find-conio-h-on-linux. – Bob__ Aug 02 '16 at 14:38

3 Answers3

1

When the compilation of a program fails, the compiler usually presents you with information about what the problem in your code is. You should use this information to track down the issue or at least let those you ask for advice know.

Let's go through the compilation errors step by step:

#include<iostream.h>

use #include <iostream> instead

void main()

main should return int. So change your main entry point to: int main(int argc, char* argv[]) and add a return 0; at the very end of your main function.

‘cout’ was not declared in this scope and error: ‘cin’ was not declared in this scope

cout and cin are functions defined in the iostream header you're including. However, these are defined in the std namespace so they are not available in the global scope. So either use std::cout and std::cin or put a using namespace std; before your main function.

With these changes, the code compiles fine and seems to produce the correct output.

Patrik H
  • 419
  • 2
  • 8
-1

@Neeraj Karthikeyan your for loop and if condition are not correct, that is why you are getting not a prime number every time, try to understand below code

#include <iostream>
#include<conio.h>

void main()
{

  int n, i, flag=0;
  clrscr();
  cout << "Enter a positive integer: ";
  cin >> n;
  for(i=2;i<=n/2;++i) //start i with 2 because if you start with 1 so all number will divisible by 1.
  {
      if(n%i==0)//if this is true it means number is not prime number
      {
          flag=1; //here we assign 1 in flag and breaking this block.
          break;
      }
  }
  if (flag==0) // if n did not divisible by any number that means flag is still 0 
      cout << "This is a prime number";
  else //this means flag!=0 that means it divided by any number above therefore it is not a prime number
      cout << "This is not a prime number";

}
Vishal Solanki
  • 2,145
  • 2
  • 16
  • 35
  • 1
    How is the loop condition not correct? It might not be the most efficient way but the definition of a prime number is that it's divisible by 1 and itself only. This means that if you go through all integers `i` in the range `[1,n]` and increment the count when the remainder of the division is 0 (using the modulo operator here) you know that it's a prime when `count == 2` – Patrik H Aug 02 '16 at 14:20
  • so please compile it and run you will get answer @PatrikH – Vishal Solanki Aug 02 '16 at 14:22
  • i said with respect to this above code that he shown in his question – Vishal Solanki Aug 02 '16 at 14:24
  • i know there are infinite way to do a program – Vishal Solanki Aug 02 '16 at 14:26
-1

Re the original code: I don't recall the operator precedence offhand, but possibly it is evaluating your loop condition as 'n % (i == 0)'. Try parenthesizing, i.e '(n % i) == 0'. Otherwise the code looks correct to me.

@Bunker Boy: The given algorithm attempts to count how many divisors n has, -including 1 and n-. If exactly 2, prime. Your approach classifies 1 as prime, but nowadays mathematicians generally classify 1 as neither prime nor composite, so technically you are incorrect in that one case.

PMar
  • 7
  • 1
  • 1
    Well, ok, we are talking of Turbo C++, but [I don't think so](http://en.cppreference.com/w/cpp/language/operator_precedence). – Bob__ Aug 02 '16 at 14:35
  • This answer is not at all correct - `%` has higher precedence than `==`, so adding parentheses as suggested will make no difference. – Paul R Aug 02 '16 at 14:48
  • @PaulR *"when you have eliminated the impossible, whatever remains, however improbable, must be the truth"* ... let's face it, Turbo C++ was **amazing** for it's time, but it can't stand a sh*t in this era. It's 26 years since it's first release... :D (although a quick look over wiki shows there was lot more resuscitation going on than I expected, but still ... it's not even zombie, or dead, it's just history) – Ped7g Aug 02 '16 at 15:05
  • @Ped7g: no matter how old and creaky Turbo C++ might be, I seriously doubt that the authors of the day got operator precedence completely wrong. – Paul R Aug 02 '16 at 15:08