C Program to generate prime numbers up to user given Number.

Spread the love

Description :

This program accepts one integer number as input and generates prime numbers up to that number.

This program is one the program in the series of prime number generation programs,  my previous programs are here.

Program :

  1. #include<stdio.h>
  2. int main()
  3. {
  4.         int i,j,cnt,num;
  5.     LABEL:    
  6.         printf(“Enter Value :”);
  7.         scanf(“%d”, &num);
  8.         /* let’s test given number is above 1
  9.         * and number must non Negative number
  10.         */
  11.         if(num < 2)
  12.         {
  13.                 printf(“Please enter valid Number n “);
  14.                 goto LABEL;
  15.         }
  16.         for(i=2; i<= num ; i++)
  17.         { // this loop for iterating from 2 to given number
  18.                 cnt = 0;
  19.                 for(j=2; j<=i/2; j++)
  20.                 { // testing i is prime or not..
  21.                         if(i%== 0)
  22.                         {
  23.                                 cnt++;
  24.                                 break;
  25.                         }
  26.                 }
  27.                 if(cnt == 0)
  28.                 { // i is prime..
  29.                         printf(“%d “,i);
  30.                 }
  31.         }
  32.         printf(n);
  33.         return 0;
  34. }

Output:

Related programs: 

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply