TCS Coding Practice Question | Checking Prime Number
Input: N = 7 Output: Yes Input: N = 15 Output: No
- Since the number is entered as Command line Argument, there is no need for a dedicated input line
- Extract the input number from the command line argument
- This extracted number will be in String type.
- Convert this number into integer type and store it in a variable, say N
- Now loop through the numbers from 2 to N/2+1, using a variable say i.
In each iteration,
- Check if i divide N completely (i.e. if it is a factor of N).
- If yes, then N is not a prime number.
- If no, then N is a prime number.
- After the loop has ended, it is found out that N is prime or not.
Note: Please note that 1 is not checked in this scenarios because 1 is niether prime nor composite.
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up