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 AnsariPosted May 15, 2022, 10:46 AM
Shazma BatoolPosted May 15, 2022, 5:57 PM
Sachin SinghPosted May 15, 2022, 1:37 PM
Muhammad Imran AnsariPosted May 15, 2022, 11:19 AM
Shazma BatoolPosted May 15, 2022, 11:10 AM
@ 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 BatoolPosted May 15, 2022, 10:52 AM
Sachin SinghPosted May 14, 2022, 5:39 PM