Skip to main content

C Program to Check Whether a Number is Prime or Not



What is Prime number?

Prime number is a positive integer greater than 1 that is only divisible by 1 and itself. For example: 2, 3 , 5, 7, 11 are the first five prime numbers.

Logic to print prime numbers between 1 to n

Step by step descriptive logic to print all prime numbers between 1 to n.

  1. Input upper limit to print prime numbers from user. Store it in some variable say end.
  2. Run a loop from 2 to end, increment 1 in each iteration. The loop structure should be like for(i=2; i<=end; i++).
  3. Inside the loop for each iteration print value of i if it is prime number
/** * C program to print all prime numbers between 1 to n */ #include <stdio.h> int main() { int i, j, end, isPrime; // isPrime is used as flag variable /* Input upper limit to print prime */ printf("Find prime numbers between 1 to : "); scanf("%d", &end); printf("All prime numbers between 1 to %d are:\n", end); /* Find all Prime numbers between 1 to end */ for(i=2; i<=end; 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 is divisible by any number other than 1 and self * then it is not prime number */ if(i%j==0) { isPrime = 0; break; } } /* If the number is prime then print */ if(isPrime==1) { printf("%d, ", i); } } return 0; }

Comments

Popular posts from this blog

Discrete Mathematics - Rules of Inference

To deduce new statements from the statements whose truth that we already know,  Rules of Inference  are used. What are Rules of Inference for? Mathematical logic is often used for logical proofs. Proofs are valid arguments that determine the truth values of mathematical statements. An argument is a sequence of statements. The last statement is the conclusion and all its preceding statements are called premises (or hypothesis). The symbol “ ∴ ∴ ”, (read therefore) is placed before the conclusion. A valid argument is one where the conclusion follows from the truth values of the premises. Rules of Inference provide the templates or guidelines for constructing valid arguments from the statements that we already have. Table of Rules of Inference Rule of Inference Name Rule of Inference Name P ∴ P ∨ Q P ∴ P ∨ Q Addition P ∨ Q ¬ P ∴ Q P ∨ Q ¬ P ∴ Q Disjunctive Syllogism P Q ∴ P ∧ Q P Q ∴ P ∧ Q Conjunction P → Q Q → R ∴ P → R P → Q Q → R ∴ P → R ...

discrete mathematics:Introduction to Trees

Tree  is a discrete structure that represents hierarchical relationships between individual elements or nodes. A tree in which a parent has no more than two children is called a binary tree. Tree and its Properties Definition  − A Tree is a connected acyclic undirected graph. There is a unique path between every pair of vertices in  G G . A tree with N number of vertices contains  ( N − 1 ) ( N − 1 )  number of edges. The vertex which is of 0 degree is called root of the tree. The vertex which is of 1 degree is called leaf node of the tree and the degree of an internal node is at least 2. Example  − The following is an example of a tree − Centers and Bi-Centers of a Tree The center of a tree is a vertex with minimal eccentricity. The eccentricity of a vertex  X X  in a tree  G G  is the maximum distance between the vertex  X X  and any other vertex of the tree. The maximum eccentricity is the tree diameter. If a ...

discrete mathematics: Venn Diagrams

Venn Diagrams Venn diagram, invented in 1880 by John Venn, is a schematic diagram that shows all possible logical relations between different mathematical sets. Examples Set Operations Set Operations include Set Union, Set Intersection, Set Difference, Complement of Set, and Cartesian Product. Set Union The union of sets A and B (denoted by  A ∪ B A ∪ B ) is the set of elements which are in A, in B, or in both A and B. Hence,  A ∪ B = { x | x ∈ A   O R   x ∈ B } A ∪ B = { x | x ∈ A   O R   x ∈ B } . Example  − If  A = { 10 , 11 , 12 , 13 } A = { 10 , 11 , 12 , 13 }  and B =  { 13 , 14 , 15 } { 13 , 14 , 15 } , then  A ∪ B = { 10 , 11 , 12 , 13 , 14 , 15 } A ∪ B = { 10 , 11 , 12 , 13 , 14 , 15 } . (The common element occurs only once) Set Intersection The intersection of sets A and B (denoted by  A ∩ B A ∩ B ) is the set of elements which are in both A and B. Hence,  A ∩ B = { x | x ∈ A   A N D...