-3

//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();
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 1
    Is there a problem you're having? Looks like you just need it to write it to the console. Not sure what you're going for with the % 60 though. – Blake Thingstad Oct 27 '17 at 21:10
  • @BlakeThingstad if the user enters 65 seconds, the integer division will give 1 minute. The remainder (5) is assigned back to seconds using modulo division. –  Oct 27 '17 at 21:12
  • I agree, what is the issue here? Just writing two numbers out to the console? –  Oct 27 '17 at 21:13
  • @Amy Ah that makes sense. I was confused trying to figure out what the point of showing the seconds was when that's what the user entered. Thanks for clarifying that for me. – Blake Thingstad Oct 27 '17 at 21:14
  • I'm still a rookie here, this a class question "How write a program to prompt the user to to enter to enter time in seconds. The program then displays it in minutes and seconds" . Which I'm just stuck on figure out how answer this question. – user8432428 Oct 27 '17 at 21:17
  • We asked because it seemed like the question was already answered. You just needed to put in `Console.WriteLine("Minutes {0} Seconds {1}", minutes, seconds)` or something like that (there are lots of ways you could write this line). – Blake Thingstad Oct 27 '17 at 21:18
  • please edit your question to state why you are asking, and state what the problem is and the desired output. – Jon Scott Oct 27 '17 at 22:43

3 Answers3

4

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();
Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

For the future I recommend trying Google for something simple like this.

Console.WriteLine(minutes + " minutes & " + seconds + " seconds");
  • 1
    New & Easier: [`Console.WriteLine($"{minutes} minutes & {seconds} seconds");`](https://stackoverflow.com/questions/31014869/what-does-mean-before-a-string). – Erik Philips Oct 27 '17 at 21:24
  • @ErikPhilips I didn't notice you posted an answer till after I put mine up. I kept it simple since OP is a beginner. When I started I never knew about the "$" & "{var}" practice and I learned using the "+" or ".." to add together for console output. – Athanasios Karagiannis Oct 27 '17 at 22:29
0

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.

SniperLegacy
  • 139
  • 5