Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
#include<stdio.h>
#include <conio.h>

void main()
{
clrscr();
int a ,i ;
i=2;
printf("please enter a number and I will tell you if the number is prime number or not");
scanf("%d",&a);
if(a%i==0)
{
printf("%d is not a prime number",a);
}
else
{
while(a%i!= 0||a==i)
{
++i;
}
if(i!=a)
{
printf("%d is not a prime number",a);
}
if(i==a)
{
printf("%d is a prime number ",a);
}
}
getch();
}


What I have tried:

i can't understand the problem tha'ts why i don't know what to do
Posted
Updated 9-Jul-20 2:26am
v3
Comments
KarstenK 9-Jul-20 8:34am    
tip: you only need to check the division of all smaller prime numbers less than half of the number.

Prime numbers are those that are exactly divisible by one and themselves only: your method uses the "Sieve of Eratosthenes"[^]

Or at least, it tries to.
Look at your loop and see if you can spot the problem:
C++
while(a%i!= 0||a==i)

Think hard about when that condition is and isn't met ...

And do yourself a favour: indent your code. It's a lot, lot easier to see what is going on when you indent code correctly:
C++
#include<stdio.h>
#include <conio.h>

void main()
    {
    clrscr();
    int a ,i ;
    i=2;
    printf("please enter a number and I will tell you if the number is prime number or not");
    scanf("%d",&a);
    if(a%i==0)
        {
        printf("%d is not a prime number",a);
        }
    else
        {
        while(a%i!= 0||a==i)
            {
            ++i;
            }
        if(i!=a)
            {
            printf("%d is not a prime number",a);
            }
        if(i==a)
            {
            printf("%d is a prime number ",a);
            }
        }
    getch();
    }
Compare that with your code in the question!
 
Share this answer
 
Quote:
while(a%i!= 0||a==i)
is wrong.


It should be:
C
while( a % i != 0 &&  i != a)

Please note, your code is not very efficient.
 
Share this answer
 
v2
Prime numbers:

0) 0 is not prime.

1) 1-3 are prime (the only prime number that is even is 2, btw).

1) You only have to mod from 2 to floor(n*0.5) (where "n" is the value you're checking) to determine if a given number is prime.

2) You only have to check until you get a mod result of 0 (indicates not prime), or until you reached floor(n*0.5) (indicates prime).

I don't bother myself any more with the C language (although i did whip up a quick c# console app as an exercise to prove to myself I was right), so go forth and code.
 
Share this answer
 
v2
Your code is extremely inefficient.
When you work on a problem, you need to understand it as much as possible.
If you look at primes numbers, you will see that 2 is the only even prime, any other even number imply a multiple of 2. So testing 4, 6, 8, 10, 12, 14 ... is useless. Just takinf this into account double the speed of your algorithm.
To check if 97 is a prime, the only primes factors to test are 2, 3, 5 and 7, no more. If you find the reason, you algorithm will be immensely faster.
Have a look there: Integer Factorization: Trial Division Algorithm[^]
Quote:
i can't understand the problem tha'ts why i don't know what to do

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900