Problem: Write a function to find whether a given no. is prime or not. Use the same to generate the prime numbers less than 100.
Source
Source Code:
/*******************************************/
- #include <stdio.h>
- #include<conio.h>
- int main()
- {
- //variables declaration
- int i, j, n, isPrime;
- // Reads upper limit to print prime.
- printf("Enter number: ");
- scanf("%d", &n);
- printf("\n Prime numbers up to %d are: \n", n);
- // Finds all Prime numbers between 1 to n.
- for(i=2; i<=n; i++)
- {
- // Assume that the current number is Prime.
- isPrime = 1;
- // Check if the current number i is prime or not.
- for(j=2; j<=i/2; j++)
- {
- if(i%j==0)
- {
- isPrime = 0;
- break;
- }
- }
- /* If the number is prime then print */
- if(isPrime==1)
- {
- //display the prime number.
- printf(" %d \t", i);
- }
- }
- getch();
- }
Output:
Enter number: 35
Prime
numbers up to 35 are:
2 3
5 7 11
13 17 19
23 29
31
Enter number: 57
Prime
numbers up to 57 are:
2 3
5 7 11
13 17 19
23 29
31 37
41 43 47
53
0 comments:
Post a Comment