• Any number which is divisible by 1 and itself is known as prime number.
  • To check prime number or not in c programming we need to use for loop and iterate from 2 to half of the number.
  • If any number is divisible then it is non prime number, we can exit the loop.
  • Now we need to check prime numbers from 1 to 100 or 1 to n.
  • Logic is same and we are adding one more for loop for 1 to N numbers.
  • Now we will iterate for loop from 1 to n and check prime or nor for each number.
  • Lets see an example program on c to check prime numbers from 1 to N. 
  • Prime number program in c using for loop.
  • prime number program in c using while loop
  • prime number using while loop in c
Note: Buy Udemy course from here if you want to learn c programming from beginning.
Master C programming with Example programs, C programming practices for absolute beginners to excel in the industry 


 Program #1 : Write a c program to check prime numbers from 1 to N by using for loop.
  • prime number using for loop in c 
  • C program to check prime number using while loop

  1. #include <stdio.h>
  2. int main()
  3. {
  4.    int n, i,j, count = 0;
  5.  
  6.     printf("Enter a Number");
  7.     scanf("%d",&n);
  8. for(i=2;i<=n;i++)
  9.  {
  10.   for(j=2;j<i;j++)
  11.   {
  12.    if(i%j==0)
  13.    break;
  14.    else if(i==j+1)
  15.    printf("%d\n",i);
  16. }
  17. }
  18.  
  19.     getch();
  20.     
  21. }


 Output:


prime numbers from 1 to n in c

You might like:
  1. C program to check whether the triangle is right angled triangle
  2. Write a c program to generate fibonacci series without recursion  
  3. Write a c program to generate fibonacci series using recursion
  4. Sum of Two integers in c
  5. Matrix multiplication in c program with explanation
  6. Factorial program in c without recursion
  7. C program to check number is armstrong number or not
  8. Check Armstrong number in c using recursive function  
  9. C program for even or odd using for loop 
  10. C program to check odd or even without using modulus operator and division operator
  11. Prime number program in c using for loop and while loop 
  12. C program to print prime numbers from 1 to n
  13. C program to print multiplication table using while loop and for loop
  14. C program to convert binary to decimal using for loop 
  15. C program to reverse a number using while loop and for loop
  16. C program to find sum of n numbers using for loop 
  17. C program to find leap year using if else  
  18. c program to check leap year using conditional operator  
  19. C program to print patterns of numbers,alphabets and stars in a pyramid shape
  20. C program to swap two numbers without using third variable and using functions 
  21. C Program to swap two numbers without using third variable 
  22. Write a C program to swap two integer arrays  
  23. C program for swapping of two strings  
  24. C program to print given number in words 
  25. C program to print 1 to 100 without using loop
  26. C program to find ASCII value of a string  
  27. Convert string to integer c programming  
  28. C program to display characters from a to z using loop 
  29. C programming power function example program 
  30. C program for addition subtraction multiplication and division using switch case  
  31. C program to delete an element in an array
  32. C program to insert an element in an array
  33. Switch case in c example program
  34. Prime number program in c using for loop
  35. Fibonacci series in c without recursion

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu