469,612 Members | 1,674 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

Prime numbers

3 2Bits
I'm writing a program to calculate the number of prime numbers in a sequence. There's a variable called num that is given and then the program must calculate how many prime numbers there are from 2 (the smallest prime number) to num.

It should be something like that:

Enter a number greater than 1: 7 There are 4 prime numbers between 2 and 7.

This is because 2,3,5 and 7 are prime numbers, a total of 4 numbers.

The code below shows the variable num which is equal to 7, the main function calls isPrime (which says if a number is prime or not), then the way I thought about was using a for loop from 2 to num and check if each i is prime or not.

I don't know how to proceed from that. Please, could someone help?

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

int isPrime(int num) {
int i;
for (i=2; i<=sqrt(num); i++) {
if (num%i == 0) {
return 0;
}
}
return 1;
}


int main(){

int i, num, numofprimes;

num=7;

for (i=2;i<=num;i++){
if (isPrime(i)){

}

printf("%d", numofprimes);
}

return 0;
}
If someone could help I would be glad.
1 Week Ago #1

✓ answered by dev7060

I'm writing a program to calculate the number of prime numbers in a sequence. There's a variable called num that is given and then the program must calculate how many prime numbers there are from 2 (the smallest prime number) to num.

It should be something like that:

Enter a number greater than 1: 7 There are 4 prime numbers between 2 and 7.

This is because 2,3,5 and 7 are prime numbers, a total of 4 numbers.

The code below shows the variable num which is equal to 7, the main function calls isPrime (which says if a number is prime or not), then the way I thought about was using a for loop from 2 to num and check if each i is prime or not.

I don't know how to proceed from that. Please, could someone help?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int isPrime(int num) {
  5. int i;
  6. for (i=2; i<=sqrt(num); i++) {
  7. if (num%i == 0) {
  8. return 0;
  9. }
  10. }
  11. return 1;
  12. }
  13.  
  14.  
  15. int main(){
  16.  
  17. int i, num, numofprimes;
  18.  
  19. num=7;
  20.  
  21. for (i=2;i<=num;i++){
  22. if (isPrime(i)){
  23.  
  24. }
  25.  
  26. printf("%d", numofprimes);
  27. }
  28.  
  29. return 0;
  30. }
If someone could help I would be glad.
Expand|Select|Wrap|Line Numbers
  1. int main() {
  2.  
  3.   int i, num, numofprimes = 0;
  4.  
  5.   num = 7;
  6.  
  7.   for (i = 2; i <= num; i++) {
  8.     if (isPrime(i)) {
  9.       numofprimes++;
  10.     }
  11.   }
  12.   printf("%d", numofprimes);
  13.  
  14.   return 0;
  15. }

4 5913
dev7060
518 Expert 512MB
I'm writing a program to calculate the number of prime numbers in a sequence. There's a variable called num that is given and then the program must calculate how many prime numbers there are from 2 (the smallest prime number) to num.

It should be something like that:

Enter a number greater than 1: 7 There are 4 prime numbers between 2 and 7.

This is because 2,3,5 and 7 are prime numbers, a total of 4 numbers.

The code below shows the variable num which is equal to 7, the main function calls isPrime (which says if a number is prime or not), then the way I thought about was using a for loop from 2 to num and check if each i is prime or not.

I don't know how to proceed from that. Please, could someone help?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int isPrime(int num) {
  5. int i;
  6. for (i=2; i<=sqrt(num); i++) {
  7. if (num%i == 0) {
  8. return 0;
  9. }
  10. }
  11. return 1;
  12. }
  13.  
  14.  
  15. int main(){
  16.  
  17. int i, num, numofprimes;
  18.  
  19. num=7;
  20.  
  21. for (i=2;i<=num;i++){
  22. if (isPrime(i)){
  23.  
  24. }
  25.  
  26. printf("%d", numofprimes);
  27. }
  28.  
  29. return 0;
  30. }
If someone could help I would be glad.
Expand|Select|Wrap|Line Numbers
  1. int main() {
  2.  
  3.   int i, num, numofprimes = 0;
  4.  
  5.   num = 7;
  6.  
  7.   for (i = 2; i <= num; i++) {
  8.     if (isPrime(i)) {
  9.       numofprimes++;
  10.     }
  11.   }
  12.   printf("%d", numofprimes);
  13.  
  14.   return 0;
  15. }
1 Week Ago #2
guiromero
3 2Bits
Thanks very much for your response.

I'd like go further in this code.
I 've created a function biggestPrime to tell the biggest prime number between 2 and num. The only way I could think about doing this was creating an array with numofprimes as its dimension, storing each prime number in the array and then identifying the biggest number in that array. However, I have no idea how to do that.

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

int isPrime(int num) {
int i;
for (i=2; i<=sqrt(num); i++) {
if (num%i == 0) {
return 0;
}
}
return 1;
}


int biggestPrime(int num){

int i, num, numofprimes;

num=7;


for (i=2;i<=num;i++){
if (isPrime(i)){
numofprimes++;
}
}

int Arr [numofprimes] =

return 0;
}


Does anyone know thow to do that?

Thank you.
1 Week Ago #3
dev7060
518 Expert 512MB
Thanks very much for your response.

I'd like go further in this code.
I 've created a function biggestPrime to tell the biggest prime number between 2 and num. The only way I could think about doing this was creating an array with numofprimes as its dimension, storing each prime number in the array and then identifying the biggest number in that array. However, I have no idea how to do that.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int isPrime(int num) {
  5. int i;
  6. for (i=2; i<=sqrt(num); i++) {
  7. if (num%i == 0) {
  8. return 0;
  9. }
  10. }
  11. return 1;
  12. }
  13.  
  14.  
  15. int biggestPrime(int num){
  16.  
  17. int i, num, numofprimes;
  18.  
  19. num=7;
  20.  
  21.  
  22. for (i=2;i<=num;i++){
  23. if (isPrime(i)){
  24. numofprimes++;
  25. }
  26. }
  27.  
  28. int Arr [numofprimes] =
  29.  
  30. return 0;
  31. }

Does anyone know thow to do that?

Thank you.
Expand|Select|Wrap|Line Numbers
  1. int biggestPrime(int num) {
  2.   for (int i = num; i >= 2; i--)
  3.     if (isPrime(i))
  4.       return i;
  5. }
1 Week Ago #4
ConnorDexter
1 Bit
I recommend https://botany.bio/blog/cbd-relaxation-effects/ , you can read many interesting articles
5 Days Ago #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by AshifToday | last post: by
25 posts views Thread by johnmsimon | last post: by
60 posts views Thread by rhle.freak | last post: by
7 posts views Thread by newstips6706 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.