C Program to find prime number

Sam Liyanage

Hey guys so I need to make a program which asks the user to enter a number as a argument and then let them know if it is a prime number or 0 otherwise. So the code I have so far is as follows but I am a little confused on how to make it run through all the possible values of the and make sure that it isn't a non-prime number. Right now what happens is that the program opens, I enter a value and nothing happens. Note: I have math in the header as I am unsure if it is needed or not at this stage.

EDIT: SO I MADE THE CHANGES SUGGESTED AND ALSO ADDED A FOR LOOP HOWEVER WHEN I GO TO COMPILE MY PROGRAM I GET AN WARNING SOMETHING ALONG THE LINES OF 'CONTROL MAY REACH END OF NON-VOID FUNCTION'. HOWEVER THE PROGRAM DOES COMPILE WHEN I GO TO ENTER A NUMBER AND HIT ENTER IRRELEVANT OT WHETHER OR NOT IT IS A PRIME NUMBER I GET AN ERROR BACK SAYING 'FLOATING POINT EXCEPTION: 8'.

EDIT 2: THE FLOATING POINT ERROR HAS BEEN FIXED HOWEVER NOW THE PROGRAM SEEMS TO THINK THAT EVERY NUMBER IS NON - PRIME AND OUTPUTS IT THIS WAY. I CAN'T SEEM TO SEE WHY IT WOULD DO THIS. I AM ALSO STILL GETTING THE 'CONTROL MAY REACH END OF NON-VOID FUNCTION' WARNING

#include <stdio.h>
#include <math.h>

int prime(int a){
    int b;

    for(b=1; b<=a; b++){
        if (a%b==0)
        return(0);
    }
    if(b==a){
        return(1);
    }
}
int main(void){

    int c, answer;

    printf("Please enter the number you would like to find is prime or not= ");
    scanf("%d",&c);

    answer = prime(c);

    if(answer==1){
        printf("%d is a prime number \n",c);
    }
    else
        printf("%d is not a prime number\n",c);
    }
zwarrior

I just modified your function a little. Here is the code

#include <stdio.h>
#include <math.h>

int prime(int a)
{
    int b=2,n=0;

    for(b=2; b<a; b++)
    {
        if (a%b==0)
        {
            n++;
            break;
        }
    }

    return(n);
}
int main(void)
{

    int c, answer;

    printf("Please enter the number you would like to find is prime or not= ");
    scanf("%d",&c);

    answer = prime(c);

    if(answer==1)
    {
        printf("%d is not a prime number \n",c);
    }
    else
    {
        printf("%d is a prime number\n",c);
    }

    return 0;
}

Explanation-

  1. In the for loop, I am starting from 2 because, I want to see if the given number is divisible by 2 or the number higher than 2. And I have used break, because once the number is divisible, I don't want to check anymore. So, it will exit the loop.
  2. In your main function, you had not assigned properly for the printf() statement. If answer==1, it is not a prime number. (Because this implies that a number is divisible by some other number). You had written, it is a prime number(which was wrong).

If you have any doubts, let me hear them.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Prime number program in C

From Dev

TSQL program to find the largest prime number

From Dev

Find prime number in C#

From Dev

C++: Program to find the largest prime factor of a number, what is wrong in my code?

From Dev

Problems with program which find prime number from 1 to 100

From Dev

Prime number program in haskell

From Dev

Prime Number program

From Dev

Ruby Prime number program

From Dev

Find if given number is prime or not

From Dev

Cant find the prime number

From Dev

this is a program to find prime numbers from 2 to 100 in C

From Dev

Prolog Program To Check If A Number Is Prime

From Dev

Prolog Program To Check If A Number Is Prime

From Dev

Java: Prime Number Program Not Working

From Dev

Prime number program does not work

From Dev

Can't seem to find why my Prime Number Program doesn't work properly after reprompt for number

From Dev

Is this program to find prime numbers wrong?

From Dev

C++ : using prime number and not a prime number

From Dev

Prime Number Generator in C

From Dev

Prime Number Generator in C

From Dev

Find the biggest prime of a number [Python]

From Dev

Find next prime number algorithm

From Dev

Find next prime number algorithm

From Dev

Python: find the nth prime number

From Dev

what is wrong in my program ? i am trying to find the list of prime number from n to m

From Dev

Is there any other algorithms to find that a number is a prime number or not

From Dev

C# - Need help creating program that simulates Prime Number behavior by drawing lines

From Dev

Function used to define whether a number is prime or not causes c++ program to crash

From Dev

Check if a number is prime in C#

Related Related

HotTag

Archive