Saturday, August 18, 2018

PRINT PRIME NUMBERS

  • Problem Description

    Ravi is poor in mathematics; his teacher wants him to improve his problem solving skills. So he gives a number to Ravi and asked him to find the prime numbers between 2 and the given number.
  • CODING ARENA
  • #include <stdio.h>
    int main()
    {
      int n,i,fact,j;
      scanf("%d",&n);
      for(i=3;i<n;i++)
      {
        fact=0;
        for(j=1;j<n;j++)
        {
          if(i%j==0)
            fact++;
        }
        if(fact==2)
          printf("%d ",i);
      }

    return 0;
    }
  • Test Case 1

    Input (stdin)
    11
    
    
    Expected Output
    3 5 7
  • Test Case 2

    Input (stdin)
    17
    
    
    Expected Output
    3 5 7 11 13

No comments:

Post a Comment