Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Tuesday, July 21, 2020

C program to check number is positive, negative or zero

C Basic programming exercises and solutions:


Write a C program to input any number from user and check whether the given number is positive, negative or Zero using simple if and if...else...if. Let's see the below step by step logic check whether the given is positive, negative or Zero using simple if and if...else...if 

Example:

  1. INPUT
  2. Enter any number to check number is positive, negative or zero: -15

  OUTPUT
-15 is negative number.

Required knowledge for this exercise

Remember: To check whether a number is positive or negative you should be aware with the basic number properties as given below:

1) If a number is greater than zero i.e 
if(num > 0)
then number is positive. 
2) If a number is less than zero i.e 
if(num < 0)
then number is negative. 
Positive and negative numbers| Protec
Logic to check whether number is positive, negative or zero:
1) Input any number from user and store it in variable num using scanf()
2) Now let's apply the below condition using simple if and if...else...if:
Check if(num > 0)then number is positive.
Check if(num < 0) then number is negative.
Check if(num == 0)then number is zero (neither positive nor negative).

Program to check whether a number is positive, negative or zero using simple if: 

  1. /* C program to check whether a number is positive or negative */ #include <stdio.h> int main() { int num; /* Input number from user */ printf("Enter any number: "); scanf("%d", &num); if(num > 0) { printf("%d is positive.", num); }
  2. if(num < 0) { printf("%d is negative.", num); } if(num == 0) { printf("Number is zero"); } return 0; }

The above program is easiest way but it has three conditions we can do this program with two condition using if...else...if see the below program.  

Program to check whether a number positive, negative or zero using if...else...if: 

  1. /* C program to check whether a number is positive or negative */ #include <stdio.h> int main() { int num; /* Input number from user */ printf("Enter any number: "); scanf("%d", &num); if(num > 0) { printf("%d is positive.", num); }
  2. else if(num < 0) { printf("%d is negative.", num); } else { printf("Number is zero"); } return 0; }

Output:

  1. Enter any number: -15
  2. -15 is negative number








Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us