Create an application in C# using if else if statement that lets the user enter
ID: 3590221 • Letter: C
Question
Create an application in C# using if else if statement that lets the user enter a number of seconds and works as follows:
There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds.
There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or equal to 3,600, the program should display the number of hours in that many seconds.
There are 86,400 seconds in a day. If the number of seconds entered by the user is greater than or equal to 86,400, the program should display the number of hours in that many seconds.
Explanation / Answer
Below is your code:-
using System;
namespace ConverTime
{
public class Program
{
public static void Main(string[] args)
{
///variable declarations
String input;
Double seconds, minutesCalculated, hoursCalculated, daysCalculated;
double secondsPerMinute = 60;
double secondsPerHour = 3600;
double secondsPerDay = 86400;
Console.Write("Enter the seconds:");
input = Console.ReadLine();
seconds = Convert.ToDouble(input);
if (seconds < secondsPerMinute)
{
Console.WriteLine("{0} seconds (no conversion was made)",seconds);
}
else if (seconds >= secondsPerMinute && seconds < secondsPerHour )
{
minutesCalculated = seconds/secondsPerMinute;
Console.WriteLine("{0} seconds equals: {1} minutes",seconds,minutesCalculated);
}
else if (seconds >= secondsPerHour && seconds < secondsPerDay)
{
hoursCalculated = seconds/secondsPerHour;
Console.WriteLine("{0} seconds equals: {1} hours",seconds,hoursCalculated);
}
else if (seconds >= secondsPerDay)
{
daysCalculated = seconds/secondsPerDay;
Console.WriteLine("{0} seconds equals: {1} days",seconds,daysCalculated);
}
}
}
}
UPDATED
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ConverTime
{
public class Program
{
public static void Main(string[] args)
{
///variable declarations
String input;
String output;
Double seconds, minutesCalculated, hoursCalculated, daysCalculated;
double secondsPerMinute = 60;
double secondsPerHour = 3600;
double secondsPerDay = 86400;
Console.Write("Enter the seconds:");
input = Console.ReadLine();
seconds = Convert.ToDouble(input);
if (seconds < secondsPerMinute)
{
output = seconds+" seconds (no conversion was made)";
}
else if (seconds >= secondsPerMinute && seconds < secondsPerHour )
{
minutesCalculated = seconds/secondsPerMinute;
output = seconds+" seconds equals: "+minutesCalculated+" minutes";
}
else if (seconds >= secondsPerHour && seconds < secondsPerDay)
{
hoursCalculated = seconds/secondsPerHour;
output = seconds+" seconds equals: "+hoursCalculated+" hours";
}
else if (seconds >= secondsPerDay)
{
daysCalculated = seconds/secondsPerDay;
output = seconds+" seconds equals: "+daysCalculated+" days";
}
MessageBox.show(output);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.