computer algorithm for to check whether a given number is prime or not
Answers
Answered by
1
algorithm is a step by step approach to solve a problem
sooo following will be the algorithm to find a number is prime or not
1.read a number
/*PRIME NUMBER MEANS IT IS DIVISIBLE BY ONE AND ITSELF SO IF ANY NUMBER HAS FACTORS OTHER THAN ONE AND ITSELF ARE NOT PRIME */
2. using a loop check repeatedly whether it is divisible by any number in between
3. finally display the number is prime or not
4.ending of the operation
i m mentioning the logic too
read a value in the variable named "n"
loop variable will be i set it to 2 coz every number has one as its factor
take a variable flag to check the status intialize it with zero
if i<n and also if n%i==0 you are checking whether it is divisible by any other number if that is the case set the flag value to one and display its not prime and break the program there itself using break key word or else increment the i value to check with the other numbers tooo if it is equal to n you will come out of the loop then display it as prime thats it
sooo following will be the algorithm to find a number is prime or not
1.read a number
/*PRIME NUMBER MEANS IT IS DIVISIBLE BY ONE AND ITSELF SO IF ANY NUMBER HAS FACTORS OTHER THAN ONE AND ITSELF ARE NOT PRIME */
2. using a loop check repeatedly whether it is divisible by any number in between
3. finally display the number is prime or not
4.ending of the operation
i m mentioning the logic too
read a value in the variable named "n"
loop variable will be i set it to 2 coz every number has one as its factor
take a variable flag to check the status intialize it with zero
if i<n and also if n%i==0 you are checking whether it is divisible by any other number if that is the case set the flag value to one and display its not prime and break the program there itself using break key word or else increment the i value to check with the other numbers tooo if it is equal to n you will come out of the loop then display it as prime thats it
Similar questions