How to print prime numbers without using modulus (%)? - c

How to print prime numbers without using modulus(%)? I tried making an array of the prime numbers and checking whether the remainder was equal to 1. But later I realized that that was not possible:
int main()
{
for (int i = 2; i < num; i++)
{
if (num % i == 0) // without using % I am suppposed find prime no
c++;
}
if (c == 0)
printf("prime");
else
printf("not prime");
}

int main()
{
for(int i=2;i<num;i++)
{
if((num-(num/i)*i)==0) // without using %
c++;
}
if(c==0)
printf("prime");
else
printf("not prime");
}
This should work.

a - (n * (a/n)) is equivalent to a % n

// Java Program
class PrimeNumber {
public static void main(String[] args) {
int num = 30;
for (int j = 2; j < n; j++) {
if ((num - (num / j) * j) == 0) {
count++;
}
}
if (count == 0) {
System.out.println(num + "is Prime");
}
}
}

Related

Unusual Floating point exception (core dumped) Error with C

I am currently a student, trying to get factorials to print out as prime numbers multiplied to certain exponents like so:
5! = (2^3)(3^1)(5^1)
However, I keep getting an unusual error, which occurs right after using scanf to retrieve my input (By the way, I would really appreciate someone showing me how to retrieve multiple inputs from an exterior file to do this using input redirection, since that's how we were supposed to retrieve our inputs for this).
Anyway, I'm assuming this error is somewhere in the specification for my while loop. I would greatly appreciate any help/tips/pointers. Thank you!
#include <stdio.h> //headers
#include <stdbool.h>
//function prototypes - I will be using functions inside of each other
int find_prime_count (int prime, int num);
int find_next_prime (int prime);
bool is_prime (int num);
int main(void) //main function
{
int primeCount[100] = {0}, prime = 2, fact, i = 2, temp = 2, currentPrimeCount, printCount = 0;
printf ("Enter number: ");
scanf ("%d", &fact);
while (i <= fact)
{
printf ("i is less than factorial");
while (temp != 1)
{
printf ("Temp is not equal to one");
currentPrimeCount = find_prime_count (prime, temp);
printf ("currentPrimeCount calculated");
temp = temp / (currentPrimeCount * prime);
printf ("Temp updated");
primeCount[prime + 1] += currentPrimeCount;
printf ("primeCount[prime + 1] updated");
prime = find_next_prime (prime);
printf ("Next prime found");
}
i += 1;
temp = i;
}
printf ("%3d! = ", fact);
i = 0;
while (i < 100)
{
if (primeCount[i] != 0)
{
if (printCount == 0)
{
printf ("(%d^%d)", i, primeCount[i]);
}
else if (printCount != 0)
{
printf (" * (%d^%d)", i, primeCount[i]);
}
printCount += 1;
if ((printCount % 9) == 0)
{
printf ("/n");
}
if ((printCount > 9) && ((printCount % 9) == 0))
{
printf (" ");
}
}
}
return 0;
}
bool is_prime (int num)
{
bool check = true; //sets check variable to true
int i = 2; //starts counter variable at 2 (will test all numbers >=2 && <num)
while (i < num && check == true)
{
if ((num % i) == 0) //if it is divisible by any number other than 1 and itself
{
check = false; //it is not a prime number and the check becomes false
}
i += 1; //increasing counter
}
return check; //returns boolean value
}
int find_next_prime (int prime)
{
int i = prime;
bool check = false;
printf ("find_next_prime starts.");
while (check == false)
{
i += 1;
check = is_prime (i);
}
printf ("find_next_prime ends.");
return i;
}
int find_prime_count (int prime, int num)
{
int count = 0;
printf ("find_prime_count starts.");
while ((prime % num) == 0)
{
count += 1;
num = num / prime;
}
printf ("find_prime_count ends.");
return count;
}
Using gdb, I can tell that it is a divide by zero error in prim % num.
Hints:
Compile with the -g flag
Run using gdb
Set a breakpoint ...

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.

Code duplication in buzzfizz program including negative numbers

Hello i want to avoid code duplication in my program (buzzfizz including negative numbers)
#include <stdio.h>
int myseries(int n) {
int i, cpt = 0;
if (n < 0) {
for (i = 0; i >= n; i--) {
// if the number is multiple of both three and five
if (i % 15 == 0) {
printf("lancelot\n");
}
// if the number is multiple of 3
else if(i % 3 == 0) {
printf("Fizz\n");
}
// if the number is multiple of 5
else if(i % 5 == 0) {
printf("Buzz\n");
cpt++;
}
else {
printf("%d\n", i);
}
}
return cpt;
}
else {
for (i = 0; i <= n; i++) {
// if the number is multiple of both three and five
if (i % 15 == 0) {
printf("lancelot\n");
}
// if the number is multiple of 3
else if(i % 3 == 0) {
printf("Fizz\n");
}
//if the number is multiple of 5
else if(i % 5 == 0) {
printf("Buzz\n");
cpt++;
}
else {
printf("%d\n",i);
}
}
return cpt;
}
}
//example
main() {
printf("the number of buzz is : %d", myseries(-16));
}
You can use the absolute value of n (i.e. abs(n)) as i's upper bound, and record n's sign (i.e. bool sgn = ( n > 0 ) ? 1 : 0;) for output.

Why does my program return the same character?

This is a program that checks whether the input number is a product of two prime numbers ('Y') or not ('N').
#include <stdio.h>
// the function checks if the number is prime
int is_prime(int z) {
int i;
for(i=2; i<z; i++){
if(z%i == 0){
return 0;
}
return 1;
}
}
/* the function checks if the given number is a
product of two prime numbers bigger than 2*/
int is_prime_product(int x) {
int j;
for(j=2; j<x; j++){
if(x%j == 0){
if(is_prime(x/j) && is_prime(j)){
return 1;
break;
}else return 0;
}
}
}
int main() {
int n=0;
int c;
do{
c=getchar();
if((c>='0') && (c<='9')){
n= n*10+(c-'0');
}
} while (('0'<=c) && (c<='9'));
if(is_prime_product(c)){
putchar('Y');
}else{
putchar('N');
}
return 0;
}
I don't know why this program always returns 'Y' even when it should return 'N'. I just don't see where the mistake is.
Partial answer: Better version of is_prime:
int is_prime(int z)
{
if(z <= 1) return 0;
if(z == 2) return 1;
int i;
for(i=3; i<sqrt(z); i+=2)
{
if(z%i == 0) return 0;
}
return 1;
}
After the test for 2 it is enough to test for odd factors up to the square root of your test number. (Also fixed the curly braces)
Cause of your error:
if(is_prime_product(n)) ...
tests the input number nnot the last character c
Edit
Some hints for better (more readable, more reliable, and so on) code:
Use types matching the problem (bool instead of int)
Use good variable names (i only for loops, z only for floats)
use meaningful variable names (number instead of n)
consistent braces, spacing around operators
These things make a difference!
Have a look:
bool is_prime(unsigned int number)
{
if(number <= 1) return false;
if(number == 2) return true;
for(unsigned int factor = 3; factor < sqrt(number); factor += 2)
{
if(number % factor == 0) return false;
}
return true;
}
Please cheak your is_prime() function. The code return 1; should be after the for loop.You can try the following:
int is_prime (int z)
{
int i;
for (i = 2; i < z; i++)
{
if (z % i == 0)
{
return 0;
}
}
return 1;
}
and your function is_prime_product () should be written as follow:
int is_prime_product (int x)
{
int j;
for (j = 2; j < x; j++)
{
if (x%j==0&&is_prime (x / j) && is_prime (j))
{
return 1;
}
}
return 0;
}
also you should use if (is_prime_product (n)) instead of if (is_prime_product (c)).
I have modified some of your code from main function.
Please try with below code once,
Instead of this main function code:
int main() {
int n=0;
int c;
do{
c=getchar();
if((c>='0') && (c<='9')){
n= n*10+(c-'0');
}
} while (('0'<=c) && (c<='9'));
if(is_prime_product(c)){
putchar('Y');
}else{
putchar('N');
}
return 0;
}
Use this code:
int main() {
int n=0;
int c;
scanf("%d",&c);
do{
if((c>='0') && (c<='9')){
n= n*10+(c-'0');
}
} while (('0'<=c) && (c<='9'));
if(is_prime_product(c)){
putchar('Y');
}else{
putchar('N');
}
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