pages

Monday, 19 April 2021

c-program | program to print all prime number from 1 to n natural number in c-programming

program to print the all prime number form 1 to n natural number.

we have first to know that what is prime number , A number which is only divisible by 1 and itself is called prime number.

In this  program , we will takes input from the user to print all prime numbers from 1 to N. The logic behind implement this program is that - Run loop from 1 to N and check each value in another loop, if the value is divisible by any number between 2 to num-1 (or less than equal to num/2) - Here num is the value to check it is prime of not.


 #include<stdio.h>
 #include<conio.h>
 void main()
 {
      int i,num,j;
      printf("enter the numbre:-");
      scanf("%d",&num);
      for(i=2;i<=num;i++){
         for(j=2;j<=num-1;j++){
                  if(i%j==0){
                           break;
                  }
         }
         if(i==j)
                  printf("%d ",j);
      }
      getch();
 }


output:-  enter the number num=35

prime  number are: 2,3,5,7,11,13,17,19,23,29,31


For more c-programming:-

1.c-program to check which number is prime or not

2.c-program to check plaindrome or not

3.c-program to swap two number without using third variable


No comments:

Post a Comment