C Program To Print Prime Numbers From 1 To 100

C program to print prime numbers from 1 to 100. If you are looking for C program to find prime numbers, here in this tutorial we will help you to learn how to write a C program for prime numbers.

C Program to Print Prime Numbers

Learn how to write a C program to print all Prime numbers between 1 to 100. Writing a C program to display prime numbers between two intervals can be done using various techniques but here in this program, we show how to write a C program to print prime numbers from 1 to 100 in a proper way.

C Program to Print Prime Numbers Source Code

/* C Program to print prime numbers from 1 to 100 - PrimeNumbers.C */

#include <stdio.h>
#include <stdlib.h>
 
void main()
{
//variable declaration
 int num1, num2, i, j, flag, temp, count = 0;
 
//asking user to enter lower and upper limit numbers and reading
 printf("Enter the value of num1 and num2 \n");
 scanf("%d %d", &num1, &num2);
//logic to finding and printing prime numbers from 1 to 100
 if (num2 < 2)
 {
   printf("There are no primes upto %d\n", num2);
   exit(0);
 }
 
 printf("Prime numbers are \n");
 temp = num1;
 
 if ( num1 % 2 == 0)
 {
   num1++;
 }
 
 for (i = num1; i <= num2; i = i + 2)
 {
 flag = 0;
 for (j = 2; j <= i / 2; j++)
 {
 if ((i % j) == 0)
 {
 flag = 1;
 break;
 }
 }
 if (flag == 0)
 {
 printf("%d\n", i);
 count++;
 }
 }
 printf("Number of primes between %d & %d = %d\n", temp, num2, count);
}

C Program to Print Prime Numbers Output

Enter the value of num1 and num2

1

100

Prime numbers are

2     3    5     7    11    13    17    19     23      29

31      37      41      43      47      53      59      61

67      71     73      79      83      89      97

Number of primes between 1 & 100 = 25

C PROGRAMMING TUTORIALS

  1. C Program to Find LCM and GCD of Two Numbers
  2. C Program to Add Spaces in a String
  3. C Program To Add Digits Of A Number
  4. C Program To Find Leap Year
  5. C Program To Append Data Into A File
  6. C Program to Accept Only Integer Values
  7. C Program To Arrange Numbers In Descending Order
  8. C Program to Add Days to Date
  9. C Program to Add Two Fractions
  10. C Program To Reverse A Number
  11. C Program to Find Maximum and Minimum Numbers
  12. C Program to Read a Text File
  13. C Program to Convert Decimal to Hexadecimal
  14. C Program to Convert Decimal to Binary
  15. C Program to Convert Celsius to Fahrenheit
  16. C Program To Find Absolute Value
  17. Ternary Operator Program in C
  18. C Program For Addition Table Using For Loop
  19. Denomination Program in C
  20. C Program to Print Multiplication Table 

Leave a Comment