How do i write this code in C program? i am stuck on it for the past 3 hrs.Suggestions are welcomed
Posted by Different-Affect-690@reddit | learnprogramming | View on Reddit | 12 comments
Print all prime numbers between a and b using functions
A prime number is a whole number that has exactly two factors which are itself and 1, whereas a composite number has factors in addition to 1 and itself.
Given 2 numbers a and b, you need to write a function which will print all prime numbers between these two given numbers (inclusive).
If there are no prime numbers between the given range, print -1.
Input Format
First line contains two integers a
and
b.
Output Format
Print the prime numbers separated
by
space.
Sample Input:
2 10
Sample Output:
2 3 5 7
Awkward-Chair2047@reddit
Every time you copy/paste someone else's code, you are cheating yourself of a great learning opportunity. Search for a pseudo-code approach to solving problem. This approach works across programming languages.
Classic-Rate-5104@reddit
You ask us doing your homework?
EfficientMongoose317@reddit
Don’t overthink it. The problem is basically:
atobSomething like this:
The important logic is:
sqrt(n)1for prime and0otherwisesmichaele@reddit
This is r/learnprogramming. Writing the program for him doesn't help. Everyone else was guiding OP to an answer.
smichaele@reddit
This is r/learnprogramming. Writing the program for him doesn't help. Everyone else was guiding OP to an answer.
Ormek_II@reddit
Can you write a function to Check if a single number is Prime?
Sad_School828@reddit
You iterate from A to B, inclusive. These are your divisors.
For each divisor you iterate from 1 to (current_divisor - 1) and these are your dividends.
For each dividend, you test divisor modulo dividend. If the modulo is ever non-zero except at 1, the divisor is not prime so you can just continue.
If the modulo result is always zero except at 1, then the number is prime so you add it to the output before continuing.
You could alternately dink around providing an implementation of the Sieve of Eratosthenes, but I'm guessing your professor would take one look at it and accuse you of either using AI or getting somebody else to write your code.
Different-Affect-690@reddit (OP)
yup u are right but here is my final code
void printPrimes(int a, int b)
{
int j;
int foundAny = 0;
for (int i = a; i <= b; i++) {
if (i < 2) {
continue;
}
for (j = 2; j < i; j++) {
if (i % j == 0) {
break;
}
}
if (i == j) {
printf("%d ", i);
foundAny = 1;
}
}
if (foundAny == 0) {
printf("-1");
}
}
int main()
{
int a,b;
scanf("%d%d", &a,&b);
printPrimes(a, b);
return 0;
}
HashDefTrueFalse@reddit
Do some research on how you might write a function to check if a number is prime, as that's the core question. Then do that for each in the range.
Since you'll come across it in the first 30 seconds, I don't think there's any harm in mentioning that you'll probably end up "sieving" them, a method based on the observation that multiples of primes cannot be prime, thus you can eliminate all multiples of a prime within the range as possibilities once you've found one.
The C code could look all kinds of ways depending on how you want to check for eliminations.
peterlinddk@reddit
Can you write it in another language?
Can you desribe step by step what the program is supposed to do?
Do you know how to find the next prime number?
Do you know how to check if a number is a prime number or not?
Start by doing the exercise on paper: if given the input of 5 and 13, which numbers should be output, and how would you calculate which ones?
Write down the rules to follow, and then convert that list of rules to C code.
Mortomes@reddit
What have you done in the past 3 hours?
Different-Affect-690@reddit (OP)
well the only thing i wrote
void printPrimes(int a, int b)
{
for(int i=2;i<=b+1;i++)
printf("%d",b);
}
}
after that i havent wrote anything at all