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.
5 21008 dev7060 644
Recognized Expert Contributor
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.
 int main() {


int i, num, numofprimes = 0;


num = 7;


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

if (isPrime(i)) {

numofprimes++;

}

}

printf("%d", numofprimes);


return 0;

}
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(in t 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.
dev7060 644
Recognized Expert Contributor
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.
 int biggestPrime(int num) {

for (int i = num; i >= 2; i)

if (isPrime(i))

return i;

}
donbock 2,426
Recognized Expert Top Contributor
Look of Sieve of Erastothenes. That may be a more efficient algorithm for finding all the prime numbers in a given range.
Also need this, got many points from comments. Thanks . Sign in to post your reply or Sign up for a free account.
Similar topics 
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 been made by A & A Group.
Muhammad Ali: Roll # 1462 Class A2 , B.Sc.(Hons.) in C.S.

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 complicated. we are
on a functions chapter. i am usung the square root to find the prime.
so far i have accomplished this but i cant fgure how to divide this by
ten so i can find the right prime. i am using c.
#include <stdio.h>

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 ending in 5 ..which obviously cannot be prime numbers)
can anyone please help me out with the reason??
/*Generate Prime Numbers*/
#include<stdio.h>

by: newstips6706 
last post by:
1, 2, 3, 5, 7... PRIME Numbers
________________________________
Definitions
What is a PRIME Number ?

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 decide if a number is a prime or not
import math
num = int(raw_input("Please enter a number: "))
#num = int(math.sqrt(num))
 
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 doing wrong as mine dosnt work.
Thank for any help

#include <iostream>

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 n,i,arr,size=0,j,temp; // array is for storing prime numbers
scanf("%d",&n); // we will be finding prime numbers between 2 to n (both inclusive)
arr=2; // we know that 2 is a prime number
size++; // we got...

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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...

by: Oralloy 
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bitfields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++12 std=c++20 Wnarrowing bit_field.cpp
Here is the code in...

by: jinu1996 
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...

by: tracyyun 
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, ZWave, WiFi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
 
by: conductexam 
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...

by: TSSRALBI 
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LANtoLAN VPNs.
The last exercise I practiced was to create a LANtoLAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

by: 6302768590 
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

by: muto222 
last post by:
How can i add a mobile payment intergratation into php mysql website.

by: bsmnconsultancy 
last post by:
In today's digital era, a welldesigned website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
 