I am very new to C# and I am having some issues. I have been trying for a while now and I cant seem to get it right. I think I have the idea but I just don't know how to make it work. There aren't any examples in the chapters of my book either. I need to "create an application that reads an integer, then determines and displays whether it's odd or even. Have the user enter an integer and output to the console: The number you have entered is: input value + even or odd" I'm hoping I can get some help here. Not looking for someone to just do the work either. If you can explain it, please do!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Student_Challenge_Lab_2
{
class Program
{
// main method begins the execution of C# program
static void Main(string[] args)
{
int number1; // declares the integer to be added
// following code prompts user to input the two sets of integers
Console.Write("Please enter your integer: ");
number1 = Convert.ToInt32(Console.ReadLine());
int %(number1, );
// the program now tests to see if the integer is even or odd. If the remainder is 0 it is an even integer
if (% == 0)
Console.Write("Your integer is even.", number1);
else Console.Write("Your integer is odd.", number1);
}
} // end main
} // end Student challenge lab 2
Every binary operator should be used in a form:
[argument1] [THE OPERATOR] [argument2]
The %
is also a binary operator, which can be used in the same way as +
and /
. So analogically, if the /
operator produces the result of a division operation:
float result = (float)number1 / number2;
the %
will produce the remainder in the same fashion:
int remainder = number1 % number2;
All what's left is that numbers that are even produce 0
remainder when modulo against 2
is calculated.
I'm not sure how you've come up with the syntax you're using here
int %(number1, );
You've already defined number1 as an int above. You want to define a new variable that contains the value of your mod operation on number1. So something like:
int remainder = number1 % 2;
Then
if (remainder == 0)
Here, I have done your homework...
The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise it returns the right operand.
The % operator computes the remainder after dividing its first operand by its second. All numeric types have predefined remainder operators.
I also added a Console.ReadKey so that you can see the output, press any key to end the app.
using System;
namespace Student_Challenge_Lab_2
{
internal class Program
{
// main method begins the execution of C# program
private static void Main(string[] args)
{
// following code prompts user to input the two sets of integers
Console.Write("Please enter your integer: ");
var number1 = Convert.ToInt32(Console.ReadLine());
// the program now tests to see if the integer is even or odd. If the remainder is 0 it is an even integer
Console.Write(number1 % 2 == 0 ? "Your integer ({0}) is even." : "Your integer ({0}) is odd.", number1);
Console.ReadKey();
}
}
// end main
}
// end Student challenge lab 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.