Thread: How many prime numbers are there in this sequence

  1. #1
    Registered User
    Join Date
    Nov 2021

    How many prime numbers are there in this sequence

    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?
    #[COLOR=var(--highlight-keyword)]include[/COLOR] [COLOR=var(--highlight-variable)]<stdio.h>[/COLOR]
    [COLOR=var(--highlight-keyword)]#[COLOR=var(--highlight-keyword)]include[/COLOR] [COLOR=var(--highlight-variable)]<math.h>[/COLOR][/COLOR]

    [COLOR=var(--highlight-namespace)]int[/COLOR] [COLOR=var(--highlight-literal)]isPrime[/COLOR]([COLOR=var(--highlight-namespace)]int[/COLOR] num) {
    [COLOR=var(--highlight-namespace)]int[/COLOR] i;
    [COLOR=var(--highlight-keyword)]for[/COLOR] (i=[COLOR=var(--highlight-namespace)]2[/COLOR]; i<=[COLOR=var(--highlight-literal)]sqrt[/COLOR](num); i++) {
    [COLOR=var(--highlight-keyword)]if[/COLOR] (num%i == [COLOR=var(--highlight-namespace)]0[/COLOR]) {
    [COLOR=var(--highlight-keyword)]return[/COLOR] [COLOR=var(--highlight-namespace)]0[/COLOR];
    [COLOR=var(--highlight-keyword)]return[/COLOR] [COLOR=var(--highlight-namespace)]1[/COLOR];

    [COLOR=var(--highlight-namespace)]int[/COLOR] [COLOR=var(--highlight-literal)]main[/COLOR](){

    [COLOR=var(--highlight-namespace)]int[/COLOR] i, num, numofprimes;


    [COLOR=var(--highlight-keyword)]for[/COLOR] (i=[COLOR=var(--highlight-namespace)]2[/COLOR];i<=num;i++){
    [COLOR=var(--highlight-keyword)]if[/COLOR] (isPrime(i)){


    [COLOR=var(--highlight-literal)]printf[/COLOR]([COLOR=var(--highlight-variable)]"%d"[/COLOR], numofprimes);

    [COLOR=var(--highlight-keyword)]return[/COLOR] [COLOR=var(--highlight-namespace)]0[/COLOR];

    If someone could help I would be glad.
    Last edited by guiromero; 11-19-2021 at 05:20 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Maybe you could post your code WITHOUT all that silly markup.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-02-2014, 10:11 AM
  2. Replies: 18
    Last Post: 03-30-2014, 06:30 AM
  3. Replies: 1
    Last Post: 03-16-2012, 02:07 AM
  4. non prime numbers or composite numbers plz!
    By danishzaidi in forum C Programming
    Replies: 10
    Last Post: 11-15-2011, 11:10 AM
  5. Sequence of numbers
    By emj83 in forum C Programming
    Replies: 3
    Last Post: 02-27-2009, 08:32 AM

Tags for this Thread