w3resource

C Exercises: Display the value of an integer n is 1,0 and -1 for the value of an integer m

C Conditional Statement: Exercise-6 with Solution

Write a C program to read the value of an integer m and display the value of n is 1 when m is larger than 0, 0 when m is 0 and -1 when m is less than 0.

Pictorial Presentation:

Display the value of an integer n is 1,0 and -1 for the value of an integer m

Sample Solution:

C Code:

#include <stdio.h>
void main()
{
   int m,n;
   printf("Input the  value of m :");
   scanf("%d",&m);
   if(m!=0)
     if(m>0)
	n=1;
     else
	n=-1;
   else
     n=0;
   printf("The value of m = %d \n",m);
   printf("The value of n = %d \n",n);
}

Sample Output:

Input the  value of m :-5                                                                                     
The value of m = -5                                                                                           
The value of n = -1

Flowchart:

Flowchart: Display the value of an integer n is 1,0 and -1 for the value of an integer m is less than 0.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a C program to read the age of a candiadte and detrermine whether it is eligible for casting his/her own vote.
Next: Write a C program to accept the height of a person in centimeter and categorize the person according to their height.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

What does (x ^ 0x1) != 0 mean?

The XOR operation (x ^ 0x1) inverts bit 0. So the expression effectively means: if bit 0 of x is 0, or any other bit of x is 1, then the expression is true.

Conversely the expression is false if x == 1.

So the test is the same as:

if (x != 1)

and is therefore (arguably) unnecessarily obfuscated.

Ref :https://bit.ly/2NIisQM





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook