Exercise
Date And Time Continuous
Objective
Create a program that shows the current time in the top-right corner of the screen with the format:
12:52:03
The program should pause for one second and then display the time again in the same location.
Write Your C# Exercise
C# Exercise Example
// Import the necessary namespaces for working with date, time, and console operations
using System;
class Program
{
// Main method where the program execution starts
static void Main()
{
// Infinite loop to continuously display the time
while (true)
{
// Get the current time
string currentTime = DateTime.Now.ToString("HH:mm:ss"); // Formats the current time as HH:mm:ss (24-hour format)
// Move the cursor to the top-right corner of the console window
Console.SetCursorPosition(Console.WindowWidth - currentTime.Length - 1, 0); // Position at top-right corner
// Display the current time at the cursor position
Console.Write(currentTime); // Writes the current time to the console at the specified location
// Pause for 1 second before updating the time again
System.Threading.Thread.Sleep(1000); // Pauses the program for 1000 milliseconds (1 second)
// Clear the previous time by moving the cursor back to the top-right corner and overwriting it
Console.SetCursorPosition(Console.WindowWidth - currentTime.Length - 1, 0); // Move to the same position
Console.Write(" "); // Clear the previous time by overwriting it with blank spaces
}
}
}
Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.
In this exercise, you need to create a program that displays the contents of a preliminary "sitemap" on the screen. The sitemap should be generated from the list of "...
In this exercise, you need to create a program that generates an HTML file listing all the images (PNG and JPG files) in the current folder. The program...
In this exercise, you need to create a program that displays specific system details, including the computer name, domain name, and the username of the current...
In this exercise, you need to create a program that takes three parameters: the name of a text file containing the URLs, the modification date, and the change ...
In this exercise, you need to create a program that displays the files and folders in the current folder and allows the user to move up and down the list. If the user...
In this exercise, you need to create a program that stores the files located in a specific folder and its subfolders. Then, the program should ask the user for a sear...