0

Can anyone type c program for checking whether the enter no. is prime number or not .

6th Aug 2018, 1:51 PM
Sriram Sekar
Sriram Sekar - avatar
6 Respuestas
+ 1
Here's a code I wrote months ago https://code.sololearn.com/clMqdiMr8Thp/?ref=app Although it's C++
6th Aug 2018, 2:16 PM
Dlite
Dlite - avatar
+ 1
Yahiko why didn't you just use a boolean and only one output based on said boolean
6th Aug 2018, 3:14 PM
hinanawi
hinanawi - avatar
+ 1
Sriram Sekar try to implement below logic on your own... you will definately get it done.. if you face any difficulty, feel free to ask: you can check whether any number is divisible by anything starting from 2 to number (infact square root of number) or not... if it is divisible, number is not prime. For example , you want to check number 6 as prime, try to divide it from 2 to 5 and as it's divisible by 2 , it's not prime.. as 7 is not divisible by 2 to 6, it's prime... note that 1 is not prime number
6th Aug 2018, 3:58 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Yahiko , your code does work perfectly... what hinanawi is trying to suggest is code readability and removal of duplicate lines.. you have mentioned cout in each condition... you can set bool IsPrime as true in top and set it to false/true in each if or elseif condition.. At the end of entire logic block of if else, you can do write cout as prime or not based on bool value
6th Aug 2018, 4:02 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Alright then
6th Aug 2018, 4:18 PM
Dlite
Dlite - avatar
0
Hinanawi I don't understand
6th Aug 2018, 3:33 PM
Dlite
Dlite - avatar