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.

Write a code to check whether a number is even or odd

Q1.Write a code to check whether a number is even or odd

Here you will find an algorithm and program to check whether a number is an even number or an odd number. First let us understand what is even and odd number.

Even Number :- Even numbers are the numbers which are divisible by 2. For Example :- 2, 4, 6, 8…

Odd Number :- Odd numbers are the numbers which are not divisible by 2. For Example :- 1, 3, 5, 7…

Now let us understand what logic we need to use in this –
As we know that, to know number is even or odd we used to check its divisibility with 2, so here we will check the remainder we get on dividing the input number with 2 if we get remainder as 0 then it is divisible by 2 and hence it is even number else it is odd number.

For Example :-

Input : 10
Output : Even Number
Explanation : As we can see that the number given in input, which is 10, on dividing with 2 we get remainder 0 therefore 10 is Even Number.

Algorithm to check number is even or odd

\\Algorithm to check number is even or odd.
START
Step 1: [ Take Input ] Read: Number N
Step 2: Check: If N%2 == 0 Then
Print : N is an Even Number.
Else
Print : N is an Odd Number.
STOP

Program to check whether the number is even or odd

Output

The number 10 is Even number

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