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.
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.
Comment