Program is saying every number is prime number - c

I have created this C program that is supposed to tell me how many prime numbers are between 1-25, but it is printing that every number is prime number. Please Help
#include<stdio.h>
int main(void) {
int n = 1, counter = 0;
int i, flag = 0;
while ( n <= 25 )
{
for ( i = 2; i <= (n/2); i++ )
{
if (n%i == 0)
{
flag = 0;
}
}
if (flag == 0)
{
counter++;
printf("%d is Prime Number.\n", n);
}
else
{
printf("%d is not Prime Number.\n", n);
}
n++;
}
return 0;
}

flag = 0;
Your flag is always 0. You have to set it to 1, while entering the while loop
while ( n <= 25 ) {
flag = 1; //<-- here
for ( i = 2; i <= (n/2); i++ ) {
You can as well break in the segment, just for efficiency
if (n%i == 0)
{
flag = 0;
break;
}

you can use the code as,
for ( i = 2; i <= sqrt(n); i++ )
{
if (n%i == 0)
{
flag = 0;
}
}
if (flag == 0)
{
counter++;
printf("%d is Prime Number.\n", n);
flag=1;
}
else
{
printf("%d is not Prime Number.\n", n);
}
n++;
use sqrt() instead of n/2 program will be more efficient.

Related

writing a prime factorization program C

I am trying to write a program that shows the results of prime factorization as below:
prompt: Input a positive integer
result examples:
100 = 2^2*5^2
It is a composite integer !
13 = 13
It is a prime number !
I tried to write it only with the basic loops, not using sophisticated techniques because it was for beginner's class. The problem is that when I enter 100 as an input, the result is just
100 =
being printed and the program stops there. Other than that, it gives me results I intended. Can anyone help me find what part of this code is the problem?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int inputNumber (int* input);
int primeFactors (int input);
int main (void) {
int input;
while ( 1 ) {
inputNumber (&input);
primeFactors (input);
}
return 0;
}
int inputNumber (int* input){
printf("\nInput a positive integer : ");
scanf("%d", input);
if(*input == 0){
printf("\n End of program");
exit(0);
}
return;
}
int primeFactors (int input){
int cnt = 0, i = 3, cnt_sum;
printf("%d = ", input);
if(input > 0){
while ( input % 2 == 0){
cnt++;}
if(cnt == 1){
printf("%d",2);}
else if( cnt > 0 && cnt != 1){
printf("%d^%d",2,cnt);
}
cnt_sum += cnt;
for ( ; i <= sqrt(input); i = i+2){
cnt = 0;
while(input % i == 0){
cnt++;
input /= i;
}
if(cnt == 1){
printf("%d",i);
}
else if( cnt > 0 && cnt != 1){
printf("%d^%d",i,cnt);
}
cnt_sum += cnt;
}
if(cnt_sum > 1){
printf("\nIt is a composite number !\n");
}
else{
printf("%d\nIt is a prime number !\n",input);
}
}
else{
printf("\nIt is an invalid number !\n");
}
}
There were few bugs.
while ( input % 2 == 0){cnt++;} loops infinitely if input is even( #Marian )
if(input > 1){ instead of if(input > 0){. since 1 is neither prime nor composite.
Corrected function:
int primeFactors (int input){
int cnt = 0, i = 3, cnt_sum;
printf("%d = ", input);
if(input > 1){
while ( input % 2 == 0){
input/=2;
cnt++;
}
if(cnt == 1){
printf("%d",2);}
else if( cnt > 0 && cnt != 1){
printf("%d^%d",2,cnt);
}
cnt_sum += cnt;
for ( ; i <= sqrt(input); i = i+2){
cnt = 0;
while(input % i == 0){
cnt++;
input /= i;
}
if(cnt == 1){
printf("%d",i);
}
else if( cnt > 0 && cnt != 1){
printf("%d^%d",i,cnt);
}
cnt_sum += cnt;
}
if(cnt_sum > 1){
printf("\nIt is a composite number !\n");
}
else{
printf("%d\nIt is a prime number !\n",input);
}
}
else{
printf("\nIt is an invalid number !\n");
}
}
ideone
You seem to be missing some steps in your algorithm, as well as checking redundant conditions in your primeFactors function.
int cnt = 0, i = 3, cnt_sum;
Because these won't ever be negative, you can change the type to unsigned.
while ( input % 2 == 0){
cnt++;
}
This while statement will loop infinitely if input is odd, because nothing is done to change input. You can add input = input / 2; to stop this.
if(cnt == 1){
printf("%d",2);}
else if( cnt > 0 && cnt != 1){
printf("%d^%d",2,cnt);
}
The else if expression can be reduced to an else statement, because unsigned variables are always greater than 0 and we already know cnt != 1, otherwise the first statement would be triggered.
while(input % i == 0){
cnt++;
input /= i;
}
The while statement is all good here, how peculiar. :P
if(cnt == 1){
printf("%d",i);
}
else if( cnt > 0 && cnt != 1){
printf("%d^%d",i,cnt);
}
Same deal here as with the if / else if statements before: change the else if to else.
With these corrections applied (and some style inconsistencies fixed), the function now looks like this:
int primeFactors (int input) {
unsigned cnt = 0, i = 3, cnt_sum;
printf("%d = ", input);
if (input > 0) {
while (input % 2 == 0) {
cnt++;
input /= 2;
}
if (cnt == 1) {
printf("%d", 2);
} else {
printf("%d^%d", 2, cnt);
}
cnt_sum += cnt;
for ( ; i <= sqrt(input); i += 2){
cnt = 0;
while (input % i == 0) {
cnt++;
input /= i;
}
if (cnt == 1) {
printf("%d", i);
} else {
printf("%d^%d", i, cnt);
}
cnt_sum += cnt;
}
if (cnt_sum > 1) {
printf("%d\nIt is a composite number !\n");
} else {
printf("%d\nIt is a prime number !\n",input);
}
}
else{
printf("\nIt is an invalid number !\n");
}
}

Need 10 outputs per line

I am having trouble refining some code. My code takes a number "n" and calculates that many prime numbers. I need to display 10 primes per line of output data. Any tips would be appreciated.
#include <stdio.h>
int main()
{
int n, i = 3, count, c;
printf("How many primes would you like?");
scanf("%d",&n);
if ( n >= 1 )
{
printf("2");
}
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf(" %d",i);
count++;
}
i++;
}
return 0;
}
Just try
printf(" %5d", i);
/* ^ to help align the numbers
and
if ((count + 1) % 10 == 0)
fputc(stdout, '\n');
fix for the first time when you already print 2.
bool is_prime(int anyNum) //takes an integer array returns, is_prime
{
bool is_prime = true;
for (int c = 2; c <= anyNum - 1; c++)
{
if (anyNum % c == 0)
{
//printf("%d is not prime\r\n" , anyNum);
is_prime = false;
}
}
return is_prime;
}
int main()
{
int num_primes;
printf("How many primes would you like: ");
std::cin >> num_primes;
printf("\r\nScanned Primes Are---\r\n");
int foundPrimes = 0;
int x = 0;
for (; x <= num_primes; x++)
{
bool gotLuckyFindingPrime = is_prime( x );
if (gotLuckyFindingPrime)
{
if (foundPrimes % 10 == 0)
{
printf("\r\n");
}
printf(" %d", x);
foundPrimes = (foundPrimes + 1) % 10;
}
}
}
Does handle ten digit showing on cmd too, you can experiment with formatting

Breaking out of a loop without a break statement [C]

How do you break out of a loop without a break statement? My professor HATES break statements and tells us not to use it. I'm just curious how would I break out of the while-loop if the number I got WAS NOT a prime number?
Here's my code:
#include <stdio.h>
#include <stdlib.h>
/* Prototypes */
void primeChecker(int num1);
int main() {
int num1 = 5;
primeChecker(num1);
return 0;
}
void primeChecker(int num1) {
int i, flag = 0;
printf("Enter a number to check for prime numbers: ");
scanf("%d", &num1);
/* Number to start with */
i = 2;
while (i <= num1/2) {
if (num1 % i == 0) {
flag = 1;
} else {
i++;
}
}
if (flag == 0) {
printf("The number is a prime number!");
} else {
printf("The number is NOT a prime number!");
}
}
Or
int prime = 1;
while (i <= num1/2 && prime) {
if (num1 % i == 0){
prime = 0;
} else {
i++;
}
}
if(prime){
printf("The number is a prime number!");
}else{
printf("The number is not prime.");
}
I mean, you almost had it:
while (i <= num1/2 && !flag){
would have done the trick as well
You can do
while (i <= num1/2) {
if (num1 % i == 0) {
i = num1;
} else {
i++;
}
}
This makes i larger then num1/2 and the while loop exits.
You probably need some more changes to make this work.
In your case, you can use the value of flag as condition:
while (flag == 0 && i <= num1/2) {
if (num1 % i == 0) {
flag = 1;
} else {
i++;
}
}
But that looks like Pascal rather than C. A better solution might be to refactor the loop so that it is in a separate function:
int is_prime(int num1)
{
int i = 2;
while (i <= num1/2) {
if (num1 % i == 0) return 0;
i++;
}
return 1;
}
This makes the code simpler and separates the input stuff in primeChecker from the actual prime checking.

Detect if the number in the array has been already input (C program)

I am kind of confused. I'd like to make a program where if the number in the array has been already input, then it will detect it and say it was repeated, so the program would tell the user to put another non-repeated integer.
#include <stdio.h>
#define SIZE 5
int main()
{
int array[SIZE];
int i;
int j;
for (i = 0; i < SIZE; i++)
{
printf("[%d] Insert a number: ", i + 1);
scanf("%d", &array[i]);
j = i - 1; // This is the closest that I've gotten guys. But I need to create a loop to make j be -1 until it finds a repeated number in the array.
if (array[i] == array[j])
{
printf("The number is repeated");
i--;
}
if (array[i] > 1000)
{
printf("Sorry, the number you entered cannot be bigger than 1000\n");
i--;
}
if (array[i] < 0)
{
printf("Sorry, the number you entered cannot be less than 0\n");
i--;
}
}
for (i = 0; i < SIZE; i++)
{
printf("The array inside is %d\n", array[i]);
}
return 0;
}
As you can see, I did something similar. I just put j = i - 1 so basically it will tell the program that it was repeated. However, I suppose that I should create a loop that will subtract -1 to j until it finds the repeated value (if there is one). I just not have any idea how to create that loop and make it work.
Thank you very much!
The checks can be done the following way (without testing)
int array[SIZE];
int i;
for (i = 0; i < SIZE; i++)
{
int valid = 1;
int num;
do
{
printf("[%d] Insert a number: ", i + 1);
scanf("%d", &num );
if ( !( valid = !( num > 1000 ) ) )
{
printf("Sorry, the number you entered cannot be bigger than 1000\n");
}
else if ( !( valid = !( num < 0 ) ) )
{
printf("Sorry, the number you entered cannot be less than 0\n");
}
else
{
int j = 0;
while ( j < i && num != array[j] ) j++;
if ( !( valid = j == i ) )
{
printf("The number is repeated");
}
}
} while ( !valid );
array[i] = num;
}
This should work for you:
#include <stdio.h>
#define SIZE 5
int main() {
int array[SIZE];
int numberCount, repeatCount;
for(numberCount = 0; numberCount < SIZE; numberCount++) {
printf("[%d] Insert a number:\n>", numberCount + 1);
scanf("%d", &array[numberCount]);
for(repeatCount = 0; repeatCount < numberCount; repeatCount++) {
if (array[numberCount] == array[repeatCount]) {
printf("\nThe numbe is repeated\n");
numberCount--;
break;
}
}
if (array[numberCount] < 0) {
printf("\nSorry, the number you entered cannot be less than 0\n");
numberCount--;
}
if (array[numberCount] > 1000) {
printf("\nSorry, the number you entered cannot be bigger than 1000\n");
numberCount--;
}
}
printf("\n\n");
for(numberCount = 0; numberCount < SIZE; numberCount++)
printf("The array inside is %d\n", array[numberCount]);
return 0;
}

Primes less than an integer

I wrote a program to find all primes less that a user-input integer. However, it just hangs. I assume using all these continues and whatnot is a mess and I have made spaghetti code...can anyone help me?
/*takes integer input, displays all primes less than that integer*/
#include <stdio.h>
int main(void) {
unsigned int num_in, test_num = 0, divisor = 0;
_Bool primestate = 0;
printf("Please enter an integer.\n");
scanf("%d", &num_in);
while(test_num < num_in) {
while(divisor < test_num) {
if(test_num % divisor == 0) {
primestate = 1;
}
test_num++;
}
if(primestate == 1) {
printf("%d is prime and less than %d.\n", test_num, num_in);
} else {
continue;
}
}
return 0;
}
You never increment test_num or divisor, so it gets stuck in one of the loops.
while(test_num < num_in) {
while(divisor < test_num) {
if(test_num % divisor == 0) {
primestate = 1;
}
divisor++; // NEW LINE
}
if(primestate == 1) {
printf("%d is prime and less than %d.\n", test_num, num_in);
} else {
continue;
}
test_num++; // NEW LINE
}
You also have a possible division by 0 (when divisor equals 0) on this line:
if(test_num % divisor == 0) {
#include <stdio.h>
#include <stdbool.h>
int main(void) {
unsigned int num_in, test_num, divisor;
bool primestate;
printf("Please enter an integer.\n");
scanf("%u", &num_in);
if(2 < num_in)
printf("\n%u\n", 2);
for(test_num = 3; test_num < num_in; test_num += 2){
primestate = true;
for(divisor=3; divisor * divisor <= test_num ; divisor += 2) {
if(test_num % divisor == 0) {
primestate = false;
break;
}
}
if(primestate) {
printf("%u\n", test_num);
}
}
return 0;
}

Resources