C program to print prime numbers from 1 to 100

prime numbers:

prime numbers are divisible by only 1 and that number itself. Eg.in this program, as per divisions, integer number 5 can be divided by 1 and integer number 5 which is nothing but original number so it is prime number. 2 is the only even prime number.
prime number:2,3,5,7,11,13,17,19,23,29,…

logic:

Below program to find prime number takes user input and then if condition inside for loop checks whether number is divisible by smaller number than input number which is 1 or number itself.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void printprimes();
int checkprimes(int num);
void main()
{
  int i;
  int flag;
  printf("\n Prime numbers from 1 to 100:");
  printprimes();
  getch(); 
}  
void printprimes()
{
  int i,j;
  for(i=2;i<=100;i++)
  {
    if(checkprimes(i));
    printf("%d\t",i);
  }
} 
int checkprimes(int num)
{
  int j;
  for(j=2;j<=sqrt(num);j++)
  {
    if(num%j==0)
    return 0;
  } 
  return 1; 
}

Ouput:


programoutputSimilar programs:
You might also like to see:

Program to find prime number using function

1 thought on “C program to print prime numbers from 1 to 100”

Leave a Reply

error: Content is protected !!
%d bloggers like this: