How many prime numbers are there in this sequence

Discussion in 'C' started by Guilherme Romero, Nov 20, 2021.

Tags:
  1. Guilherme Romero

    Guilherme Romero New Member

    Joined:
    Nov 19, 2021
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    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?
    Code:
    #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.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Set numofprimes to zero and then inside the if statement increase it by one.
     
  3. Guilherme Romero

    Guilherme Romero New Member

    Joined:
    Nov 19, 2021
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Thank you, ir worked!
     
  4. Guilherme Romero

    Guilherme Romero New Member

    Joined:
    Nov 19, 2021
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Hello,

    I want to go further in this code.
    I 've created a function biggerPrime 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.
    Code:
    #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 biggerPrime(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.
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Inside the if statement set the number to i and it is your bigger prime. Each time it finds the new one, that will be bigger than the previous one.
     
  6. Guilherme Romero

    Guilherme Romero New Member

    Joined:
    Nov 19, 2021
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Thanks Shabbir again, now I could complete the whole program:
    Code:
    #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, biggestprime;
    
    for (i=2;i<=num;i++){
     
        if (isPrime(i)){
        biggestprime = i;
    
        }
    }
    
    return biggestprime;
    }
    
    int main(){
      
       int num;
      
       printf("Enter a number bigger than 2: ");
       scanf("%d", &num);
      
        printf("The biggest prime between 2 and %d is %d", num, biggestPrime(num));
    }
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    The pleasure is all mine.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice