Wednesday, January 4, 2023

C Programming. Prime number program.

Prime number program.

// This code will tell if a number if prime number or not.

#include<stdio.h>
int main(){
       int i, j, n, c=0;
       printf("Enter a number : ");
       scanf("%d",&n);
       for (i=1; i<=n; i++){
              if (n%i == 0){
                     c++;
              }
       }
       if (c==2){
              printf("Prime Number.\n");
       }
       else {
              printf("Not prime number.\n");
       }


// This code will print the print the prime number between 1 to 100.
       
       printf("Prime nos between 1 to 100 is :\n");

       for (i=1; i<=100; i++){
              c=0;
              for (j=1; j<=i; j++){
                     if (i%j == 0){
                            c++;
                     }
              }
              if (c==2){
                     printf("%d  ",i);
              }      
       }
       return 0;
}



 

No comments:

Post a Comment

Selection Sort Using C programming.

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the un...