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.
Visual Presentation:
Sample Solution:
C Code:
#include <stdio.h> // Include the standard input/output header file.
void main()
{
int m,n; // Declare two integer variables 'm' and 'n'.
printf("Input the value of m :"); // Prompt the user to input the value of 'm'.
scanf("%d",&m); // Read and store the user's input in 'm'.
if(m!=0) // Check if 'm' is not equal to 0.
{
if(m>0)
n=1; // If 'm' is greater than 0, set 'n' to 1.
else
n=-1; // If 'm' is less than 0, set 'n' to -1.
}
else
n=0; // If 'm' is equal to 0, set 'n' to 0.
printf("The value of m = %d \n",m); // Print the value of 'm'.
printf("The value of n = %d \n",n); // Print the value of 'n'.
}
Sample Output:
Input the value of m :-5 The value of m = -5 The value of n = -1
Flowchart:
C Programming Code Editor:
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming-exercises/conditional-statement/c-conditional-statement-exercises-6.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics