Write a code to check whether the given number is prime number or not

Q1.Write a code to check whether the given number is prime number or not

The article will help you write a algorithm and program to Check Whether a Number is Prime or not.

Prime Number:- A number that is divisible by only and only 1 and itself is known as a Prime Number. For example: – 11 is only divisible by 1, so 11 is prime, while 10 is divisible by 1, 2, and 5 so 10 is not a prime number.

Here you will find the algorithm and program to check whether a number is prime or not with explanation.

Prime Number Checking Algorithm

Here’s a simple algorithm to check whether a given positive integer is prime or not:

 

1.Start with the number n to be checked.

2.If n is less than 2, it is not prime. Return false.

3.If n is 2 or 3, it is prime. Return true.

4.If n is even (i.e., divisible by 2), it is not prime (except for 2 itself). Return false.

5.For all odd integers i from 3 to the square root of n (inclusive), do the following:

6.If n is divisible by i, it is not prime. Return false.

7.If none of the above conditions hold, n is prime. Return true.

Program to check whether the number is Prime or not

Output

Enter a positive integer : 11
it is a prime number.
Enter a positive integer : 24
it is not a prime number.

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Top Coding Questions Asked In interview

Top coding questions asked in interview

Some important coding question for interview

44. Write a code to print half inverted pyramid pattern using stars.

59. Write a code to find square root of a number.

55. Write a code to check automorphic number.

60. Write a code to find the missing number in array.

63. Write a code to print multiplication table of a number.

46. Write a code to print upper triangular elements of an array.

48. Write a code to convert number from decimal to binary number.

51. Write a code to check whether a number is positive or negative.

41. Write a code to print floyds triangle.

36. Write a code to sort array elements in ascending order using Insertion Sort.

34. Write a code to sort array elements in ascending order using selection sort algorithm.

39. Write a code to subtract two matrices.

43. Write a code to print half pyramid pattern using stars.

61. Write a code to check harshad (or niven) number.

64. Write a code to remove all occurrences of a character in a string.

33. Write a code to sort array elements in descending order using bubble sort.

37. Write a code to sort array elements in descending order using Insertion Sort.

47. Write a code to concatenate two string without using library function.

54. Write a code to find square root of a number.

58. Write a code to check strong number.

62. Write a code to print the numbers divisible by 5 or 7.

3. Write a code to check whether the given year is leap year or not.

24. Write a code to print all permutations of a given string.

20. Write a code to check whether the given string is pangram or not.

22. Write a code to count occurrences of a given character in a string.

42. Write a code to print full pyramid pattern using stars.

45. Write a code to print lower triangular elements of an array.

52. Write a code to find greatest of three numbers.

49. Write a code to count total number of vowels and consonants in a string.

40. Write a code to multiply two matrices.

35. Write a code to sort array elements in descending order using selection sort algorithm.

56. Write a code to check abundant number.

13. Write a code to find factorial of a number.

CSS

CSS

A language that doesn’t affect the way you think about programming is not worth knowing.  Alan J. Perlis

C is a general purpose, procedural programming language. C was developed by Dennis M. Ritchie at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

It was mainly developed as a system programming language to write operating system. The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development. C programming is highly efficient. That’s the main reason why it’s very popular despite being more than 40 years old. Standard C programs are portable. The source code written in one system works in another operating system without any change.

C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute (ANSI). Today’s most popular Linux OS and RDBMS MySQL have been written in C.

Practice C MCQ Questions here on alfatechlab which will help you to clear your concepts and also to prepare for technical rounds, interviews, competitive exams etc.

HTML

HTML

A language that doesn’t affect the way you think about programming is not worth knowing.  Alan J. Perlis

C is a general purpose, procedural programming language. C was developed by Dennis M. Ritchie at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

It was mainly developed as a system programming language to write operating system. The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development. C programming is highly efficient. That’s the main reason why it’s very popular despite being more than 40 years old. Standard C programs are portable. The source code written in one system works in another operating system without any change.

C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute (ANSI). Today’s most popular Linux OS and RDBMS MySQL have been written in C.

Practice C MCQ Questions here on alfatechlab which will help you to clear your concepts and also to prepare for technical rounds, interviews, competitive exams etc.

Python

Python

A language that doesn’t affect the way you think about programming is not worth knowing.  Alan J. Perlis

C is a general purpose, procedural programming language. C was developed by Dennis M. Ritchie at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

It was mainly developed as a system programming language to write operating system. The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development. C programming is highly efficient. That’s the main reason why it’s very popular despite being more than 40 years old. Standard C programs are portable. The source code written in one system works in another operating system without any change.

C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute (ANSI). Today’s most popular Linux OS and RDBMS MySQL have been written in C.

Practice C MCQ Questions here on alfatechlab which will help you to clear your concepts and also to prepare for technical rounds, interviews, competitive exams etc.