I have a very simple program actually want to grab the concept; for methods with non-void return type as in my case its int, I am obliged to use a return statement but using that return statement am unable to print a number that I have taken from the user. What I need to do? Thanks
I can use the Console.WriteLine() method in this if-else to let the user know that his/her entered number is even or not But this return statement is then for which purpose?
namespace x
{
class CheckEvenNum
{
static int Check(int num)
{
if (num % 2 == 0)
{
Console.WriteLine("It is a even number");
return num; // Want to print the number on console with this return
}
else
{
Console.WriteLine("Not an even number");
return -1; // Want to print the number on console with this return
}
}
static void Main(string[] args)
{
Console.WriteLine("Enter a positive number");
int number=Convert.ToInt32(Console.ReadLine());
CheckEvenNum.Check(number);
}
}
}

Muhammad Imran AnsariRank #326 · 7.6K reputationPosted May 15, 2022, 6:46 AM EDT
Sachin SinghRank #66 · 26.2K reputationPosted May 14, 2022, 1:39 PM EDT
Sachin SinghRank #66 · 26.2K reputationPosted May 15, 2022, 9:37 AM EDT
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted May 15, 2022, 7:19 AM EDT
Shazma Batool91 reputationPosted May 15, 2022, 6:52 AM EDT
Shazma Batool91 reputationPosted May 15, 2022, 7:10 AM EDT
@ Muhammad Imran Ansari okay am going with point 3 and it is concluded that return statement returns any xyz thing but that xyz thing cannot be displayed or atleast cannot be shown without using Console.WriteLine() method. Thanks @ Muhammad Imran Ansari and Console.WriteLine() method!
Shazma Batool91 reputationPosted May 15, 2022, 1:57 PM EDT