//what I have done so far
int seconds, minutes;
Console.Write("Seconds: ");
seconds = int.Parse(Console.ReadLine());
minutes = seconds / 60;
seconds = seconds % 60;
Console.ReadLine();
//what I have done so far
int seconds, minutes;
Console.Write("Seconds: ");
seconds = int.Parse(Console.ReadLine());
minutes = seconds / 60;
seconds = seconds % 60;
Console.ReadLine();
Seems like you just need to output the result to the console, just before the ReadLine
:
Console.Write("Enter the number of seconds: ");
int totalSeconds = int.Parse(Console.ReadLine());
int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60;
// You're missing this line:
Console.WriteLine($"{totalSeconds} seconds = {minutes} minutes and {seconds} seconds");
Console.Write("\nPress any key to exit...");
Console.ReadKey();
Also, just so you know, there is a System.TimeSpan
class that will do these calculations for you. You can create it using the static method FromSeconds()
(there are others, like FromDays
, FromHours
, FromMinutes
, etc), and then you can access properties like TotalSeconds
or Seconds
:
Console.Write("Enter the number of seconds: ");
int totalSeconds = int.Parse(Console.ReadLine());
var result = TimeSpan.FromSeconds(totalSeconds);
Console.WriteLine(
$"{result.TotalSeconds} seconds = {result.Minutes} minutes and {result.Seconds} seconds");
Console.Write("\nPress any key to exit...");
Console.ReadKey();
For the future I recommend trying Google for something simple like this.
Console.WriteLine(minutes + " minutes & " + seconds + " seconds");
Rufus L answer was accurate bu just a little warning when using int totalSeconds = int.Parse(Console.ReadLine());
. The user can enter characters and your console application will crash.
You can add try catch bloc to prevent this like so:
try {
int totalSeconds = int.Parse(Console.ReadLine());
}
catch (FormatException) {
Console.WriteLine("The entered number is invalid.");
}
There are better ways to do that with loops to allow the user to type again. Check out the Int.TryParse(...) which return a boolean based on if the parse was successful or not.