473,418 Members | 4,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Prime numbers

4 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.
Nov 20 '21 #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. }

5 20990
dev7060
636 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. }
Nov 21 '21 #2
guiromero
4 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.
Nov 21 '21 #3
dev7060
636 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. }
Nov 21 '21 #4
donbock
2,426 Expert 2GB
Look of Sieve of Erastothenes. That may be a more efficient algorithm for finding all the prime numbers in a given range.
Jan 22 '22 #5
james121
1 Bit
Also need this, got many points from comments. Thanks.
Feb 10 '22 #6

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

Similar topics

0
by: AshifToday | last post by:
this was my and my frineds little project in earlier classes, the program seperates the composite and prime numbers in two sections of the screen ===================== /* This program has...
25
by: johnmsimon | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
5
by: trillianx | last post by:
I know there has been lot of discussion on Prime numbers program but I have very specific question. Here is my program: # Find the prime numbers # This is a user input program that lets you...
4
by: Villanmac | last post by:
Hi i have made the following program to make a fucntion that determines if a number is prime. Than i am supposed to use this function to print all prime numbers between 1 and 10000. What am I...
2
by: ujjawaltaleja | last post by:
//simply we are going to use the approach that the numbers that are divisble by prime numbers are not prime and the others are prime. #include<stdio.h> #include<math.h> int main() { int...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.