Assignment 2 COMPLETED

docx

School

Centennial College *

*We aren’t endorsed by this school

Course

100

Subject

Computer Science

Date

Feb 20, 2024

Type

docx

Pages

4

Uploaded by MegaElectronHornet33

Report
Assignment 2 1. Write a program to prompt the user to enter a single character. The program should display a message like “Your response was y”. For this question, you must use a variable of type char . Console.Write( "Enter your favourite number from 1-9: " ); char userInputChar; while (! char .TryParse(Console.ReadLine(), out userInputChar) || userInputChar == '\0' ) { Console.WriteLine( "Invalid input. Please enter a number from 1-9: " ); } Console.WriteLine( $"\nYour favourite number is {userInputChar} \n" ); 2. Write a program to ask the user about the validity of a simple statement. The program should accept the response then display the statement as well as the response. The response should be true or false. For this question, you must use a variable of type bool. It is useful to know that Convert.ToBoolean() can work with true, True, true, TRUE etc Console.Write( "Is this statement True or False: The Earth is flat. " ); bool isvalid = Convert.ToBoolean(Console.ReadLine()); Console.WriteLine( $"\nThe statement is: { false } \n" ); Console.WriteLine( $"\nThe answer is: False\n" ); 3. Write a program to calculate the area of a circle. The user will enter the radius of the circle and the program will calculate and display the area according to the formula (area = 3.14 * radius * radius). You must accept fractions as the input. If the user enters 1.2 for the radius then the area will be 4.52. (Use the "F" format-specifier for floating point values). using System; class Program { static void Main() { Console.WriteLine( "Enter radius of circle (as a fraction or a number): " ); string userInput = Console.ReadLine(); double radius; string [] fractionParts = userInput.Split( '/' ); if (fractionParts.Length == 2 && double .TryParse(fractionParts[0], out double numerator) && double .TryParse(fractionParts[1], out double denominator)) { radius = numerator / denominator; } else if ( double .TryParse(userInput, out radius)) { } else { Console.WriteLine( "\nInvalid input. Please use numbers or fractions.\n" ); return ;
} double area = Math.PI * radius * radius; Console.WriteLine( $"\nThe area of the circle is: {area: F } \n" ); } } 4. Write a program that prompts the user for a number (that may be a fraction). The program reads in the input and print the following: the input as a double, the input as an int and finally the input as a char. e.g. if the input is 65.790 , then the output will be 65.790 , 65 , and A using System; class Program { static void Main() { Console.Write( "Enter a number or a decimal: " ); string userInput = Console.ReadLine(); double userInputDouble; if (userInput.Contains( "/" )) { string [] fractionParts = userInput.Split( '/' ); if (fractionParts.Length == 2 && double .TryParse(fractionParts[0], out double numerator) && double .TryParse(fractionParts[1], out double denominator)) { userInputDouble = numerator / denominator; } else { Console.WriteLine( "Invalid input." ); userInputDouble = 0.0; } } else { if (! double .TryParse(userInput, out userInputDouble)) { Console.WriteLine( "Invalid input." ); userInputDouble = 0.0; } } int userInputInt = ( int )userInputDouble; char userInputCharConverted = ( char )userInputInt; Console.WriteLine( $"As double: {userInputDouble: F } , As int: {userInputInt} , As char: {userInputCharConverted} \n" ); } } 5. Adult ticket cost $3.75 and child ticket cost $2.25. Write a program to prompt the user for the amount of adult and child ticket that she needs. The program will display a user-friendly message of the number of tickets brought as well as the total cost. (Use the "C" format-string for currency).
using System; class Program { static void Main() { int adultTickets, childTickets; Console.Write( "How many adult tickets are needed: " ); if (! int .TryParse(Console.ReadLine(), out adultTickets) || adultTickets < 0) { Console.WriteLine( "Invalid input. Please enter a valid positive integer for adult tickets." ); return ; } Console.Write( "\nHow many child tickets are needed: " ); if (! int .TryParse(Console.ReadLine(), out childTickets) || childTickets < 0) { Console.WriteLine( "Invalid input. Please enter a valid positive integer for child tickets." ); return ; } decimal adultTicketCost = 3.75M; decimal childTicketCost = 2.25M; decimal totalCost = (adultTickets * adultTicketCost) + (childTickets * childTicketCost); Console.WriteLine( $"\nNumber of adult tickets: {adultTickets} " ); Console.WriteLine( $"Number of child tickets: {childTickets} " ); Console.WriteLine( $"Total cost: {totalCost: C } \n" ); } } 6. Write a program to calculate and display the potential difference between the ends of a wire. The program will prompt the user for the current flowing and the resistance of the wire. Potential difference is the product of the current and the resistance of the wire and may include a fractional part. (Again, use the "F" format-specifier for floating point values). using System; class Program { static void Main() { double current, resistance; Console.Write( "Enter the current: " ); if (! double .TryParse(Console.ReadLine(), out current)) { Console.WriteLine( "Invalid input. Please enter a valid number for current." ); return ; } Console.Write( "Enter the resistance: " ); if (! double .TryParse(Console.ReadLine(), out resistance)) {
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Console.WriteLine( "Invalid input. Please enter a valid number for resistance." ); return ; } double potentialDifference = current * resistance; Console.WriteLine( $"\nYou entered: Current = {current} , Resistance = {resistance} " ); Console.WriteLine( $"Potential difference: {potentialDifference: F } \n" ); } }