C#: Display the value of an integer n is 1,0 and -1 for the value of an integer m
Write a C# Sharp 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.
Sample Solution:-
C# Sharp Code:
using System; // Importing the
public class Exercise6 // Declaration of the Exercise6 class
{
public static void Main() // Entry point of the program
{
int m, n; // Declaration of the integer variables m and n
Console.Write("\n\n"); // Printing new lines
Console.Write("Display the value of n is 1, 0, and -1 for the value of m:\n"); // Displaying the purpose of the program
Console.Write("----------------------------------------------------------"); // Displaying a separator
Console.Write("\n\n"); // Printing new lines
Console.Write("Input the value of m :"); // Prompting user to input the value of m
m = Convert.ToInt32(Console.ReadLine()); // Reading the input value of m from the user
if (m != 0) // Checking if m is not equal to 0
{
if (m > 0) // Checking if m is greater than 0
n = 1; // Assigning 1 to n if m is greater than 0
else
n = -1; // Assigning -1 to n if m is less than 0
}
else
{
n = 0; // Assigning 0 to n if m is equal to 0
}
Console.Write("The value of m = {0} \n", m); // Printing the value of m
Console.Write("The value of n = {0} \n\n", n); // Printing the value of n
}
}
Sample Output:
Display the value of n is 1,0 and -1 for the value of er m: ---------------------------------------------------------- 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 .](https://www.w3resource.com/w3r_images/csharp-conditional-statement-exercise-6.png)
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C# Sharp program to read age of a candidate and determine whether it is eligible for casting his/her own vote.
Next: Write a C# Sharp 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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics