Here is the c program to check if the user entered number is prime ,even and odd. These few lines of code solve three problems.
In the above program we used integer type num variable for storing user entered numbers. Then we used the IF condition statement. That's all.
IF condition for even number
In the First IF statement we have a logic. If the number is divided by two then the reminder should be 0 then the number is an even number else not an even number. That simple logic is implemented in the first if statement.
IF condition for odd number
In the second IF statement we Implemented odd number logic. We identify odd numbers just by making little change in even number logic. If the number is divided by two then the reminder should not be a zero. Then the number is odd. That's simple logic used to identify whether a number is odd or not an odd number.
IF condition for prime number
In the third IF condition we implemented the logic of the prime number. In this IF condition we used multiple conditions hence we used and , or conditional operator. First condition checks when the number is divided by two then the reminder should be one. Next condition specifies that one is not a prime number. The third condition specifies 2 as the prime number. Third condition we used because it's not work with the first condition hence the third is an essential condition. That's the logic of prime numbers.
All these three problems of identifying number type using c program implemented in one single code. Hope You understood.
Comments
Post a Comment
Please give us feedback through comments