logo
down
shadow

I am making a prime number checker: when I try to run it, it stopped working


I am making a prime number checker: when I try to run it, it stopped working

By : Abanoub Samir
Date : September 23 2020, 04:00 AM
I hope this helps you . ,
Try this Code.
code :
#include <stdio.h>

int main()
{
    int x,i,counter=0;
    printf("Input number!\t");
    scanf("%d",&x);


    for(i=1;i<=x;i++)
    {
        if(x%i==0)
        {
            counter++;
        }

    }

    if(counter<=2)
    {
        printf("%d is a prime number.",x);
    }
    else
    {
        printf("%d is not a prime number.",x);
    }
    return 0;
}


Share : facebook icon twitter icon
Prime number checker returning every number as prime

Prime number checker returning every number as prime


By : J. Silva
Date : March 29 2020, 07:55 AM
wish helps you Because you are not returning after you find a number is not prime. So it checks all the way until i-1 and checks if i-1 Mod i = 0 which it never will be. So it says all numbers are prime.
Also, you only have to check if the prime numbers from 2 to sqrt(i) divide into i to see if i is a prime number or not.
Prime number checker returns any number as a prime. C

Prime number checker returns any number as a prime. C


By : user2170316
Date : March 29 2020, 07:55 AM
hope this fix your issue This program is supposed to print every number that is not prime from 3 to 100. But it acts like it doesn’t interpret well the last if statement. , You forgot to set flag to a non-zero value:
code :
for (i = 3; i < 100; i++){
    flag = 1;
    for(k = 2; k <= i/2; ++k){
        if(i % k == 0) { 
            flag = 0; 
            break;
        }
    }
    if (flag==0)  { printf("not prime : %d\n",i); }
}
Python Prime number checker not working

Python Prime number checker not working


By : Rogan Melo
Date : March 29 2020, 07:55 AM
this one helps. In Python 3.x, you need to convert your variable number to int like this:
code :
number = int(input("Please enter a number: "))
>>> number = input("Please enter a number: ")
Please enter a number: 4
>>> type(number)
<class 'str'>
>>> number = input("Please enter a number: ")
Please enter a number: 4
>>> type(number)
<type 'int'>
Prime Number checker is not working

Prime Number checker is not working


By : jackson bai
Date : March 29 2020, 07:55 AM
I hope this helps you . Remove equal to operator in n <= y. Start your loop from 2. It must be like this. ( long n =2; n < y; n++)
my prime number checker function not working properly

my prime number checker function not working properly


By : user3368996
Date : March 29 2020, 07:55 AM
wish helps you So this function is just supposed to return 0 if not prime and 1 if prime. Am I seeing something wrong? for example, when I give it 39, it says it returns 1 although 39 is not a prime. , In this loop
code :
for(loop_counter ; loop_counter < number ; loop_counter++){
int is_prime( unsigned int n )
{
    int prime = n % 2 == 0 ? n == 2 : n != 1;

    for ( unsigned int i = 3; prime && i <= n / i; i += 2 )
    {
        prime = n % i != 0;
    }

    return prime;
}
int prime = n % 2 == 0 ? n == 2 : n != 1;
    for ( unsigned int i = 3; prime && i <= n / i; i += 2 )
                                                   ^^^^^^
        prime = n % i != 0;
for ( unsigned int i = 3; prime && i <= n / i; i += 2 )
                          ^^^^^
for ( unsigned int i = 3; prime != 0 && i <= n / i; i += 2 )
                          ^^^^^^^^^^
Related Posts Related Posts :
  • How do I know it is OK to free the pointer after passed it to a function?
  • How to get the incoming ssh/telnets clients local IP address from a shell script or C
  • C: What does if(a<b<c) do?
  • What does this mean exactly in C?
  • What is the best way to achieve sscanf-like functionality in Perl?
  • How to create threads and sort correctly in a odd-even sorting program?
  • What's the single best reference on the topic of (POSIX) signals?
  • Avoid Overflow when Calculating π by Evaluating a Series Using 16-bit Arithmetic?
  • How to handle data or char -1 when reading from file, since EOF is also -1
  • Compare every 1kb of contents of two files instead of character by character
  • Understanding functions in stm8s disassembly?
  • Recursive and no-recursive get different answer
  • Can anyone explain the following output of the code?
  • How to resync time from NTP server in esp-idf?
  • Output for the following C code is confusing me
  • Determine if window is visible with High CPU
  • Writing a file by taking input from user only saves value for "x" only
  • How should I fill an array of ints from a file using command line args? The size of the file and the no. of elements may
  • Segmentation Fault While Reading File in C
  • Fill an array at index n with m times data without bit-fields
  • Why don't the values I type in the terminal match the values I've already stored in the array?
  • MPLABX XC16 unable to resolve built-in identifier __builtin_dmaoffset
  • How call and compile function from elf to my binary?
  • Modifying non-const char array that is referred to by const char array
  • Does anyone see any mistakes here? I am trying to get a message via a pipe, from my parent to child
  • Memory Leak - Singly linked list in C
  • Trouble with translation of typedef between C to D
  • Why does SIGINT stop sleep more than one time?
  • How to pause a timer created with CreateTimerQueueTimer
  • How can I read data from file ? I can't fix it
  • Scanf in visual studio not accepting multiple cases of characters
  • How can I determine if a char* is a windows line ending?
  • I am trying to store value in long long int type but giving wrong return
  • How to manage different kinds of data in a linked list?
  • uint8_t not rollover to 0 after reaching 255 not working properly
  • Why do these two execvp produce different results?
  • Pass uintmax_t or size_t to custom printf conversion specifier
  • Why does free() leaves stuff in memory?
  • Why pointers can't be used to index arrays?
  • memory allocation eror in C
  • C custom datatypes mapped to C datatypes grouped under a single struct
  • pipe() data is not transferred to child process
  • Getting a core dump from a simple C program
  • Fatal error on makefile, need to understand the problem
  • How can I add a delay of 90 minutes when a port has gone from 0 to 1?
  • To use strcpy or not
  • the usage of strtok() in c shows warnings and returns segmentation fault(core dumped)
  • Trouble allocating array of structs
  • Only first char of user input used in array
  • Why does "int x = 5; printf("%d %d %d", x==5, x=10, x==5);" in C print "0 10 0"?
  • How to scan specific string format in C?
  • sscanf skipping the final value when reading multiple values from a line
  • How can I access full memory space in FreeDOS with C application
  • Semantics of sem_getvalue() in POSIX
  • What does the [x,y] symbol mean in a multidimensional array access?
  • compilation error: cast from pointer to integer of different size
  • why am i getting compilation error "error: conflicting types for ‘ptr’ " for the following code? static int va
  • Why does getw return -1 when trying to read a character?
  • Why C program in whch two functions call each other recursively gives segmentation fault on linux?
  • ssize_t is undefined
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk