Console Class in C#

I am Writing few basic programs in console class C# (C Sharp)

using System;
class Ravi
{
    static void Main()
    {
        Console.Write("Enter Your Name:  ");
        string name = Console.ReadLine();
        Console.WriteLine("Hello: " + name );
    }
}        


#. Read a Single Character
using System;
class Ravi
{
    static void Main(){
        Console.Write("Press any key: ");
        ConsoleKeyInfo key = Console.ReadKey();
        Console.WriteLine("\n you presed key: " + key.Key);
    }
}        
3. Changing Console Text and Background Colors
using System;
class Ravi
{
    static void Main()
    {
        Console.ForegroundColor = ConsoleColor.Green;
        Console.BackgroundColor = ConsoleColor.Black;
        Console.Clear();
        Console.WriteLine("green or black: ");
        Console.ResetColor();
    }
}        
Write a program that asks the user for their age and prints whether they are an adult (18 or older) or not.

using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter Your age:  ");
       
        int age = Convert.ToInt32(Console.ReadLine());
        if(age>=18)
        {
            Console.WriteLine("You are an adult.");
        }
        else
        {
            Console.WriteLine("You are not an Adult");
        }
    }
}        
Create a calculator that asks for two numbers and an operator (+, -, *, /) and performs the operation.

using System;
class RaviCal
{
    static void Main()
    {
        Console.Write("Enter First Number");
        double num1 = Convert.ToDouble(Console.ReadLine());
        
        Console.Write("Enter Operator(+, -, *, /): ");
        char op = Console.ReadKey().KeyChar;
        
        Console.Write("Enter Second Number");
        double num2 = Convert.ToDouble(Console.ReadLine());
        
        double result = 0;
        bool valid = true;
        
        switch(op)
        {
            
            case '+': result = num1 + num2; break;
            
            case '-': result = num1 - num2; break;
            case '*': result = num1 * num2; break;
          case '/':
                if (num2 != 0) result = num1 / num2;
                else 
                {
                    Console.WriteLine("\nError: Division by zero is not allowed.");
                    valid = false;
                }
                break;
          
        default:
        Console.WriteLine("\nInvalid operator!");
            valid = false;
            break;
        }
        if(valid)
        Console.WriteLine("\nresult: " +result);
        
    }
}
        
Write a program that asks for a username and password. If the user enters "admin" as the username and "password123" as the password, print "Login Successful", otherwise print "Invalid credentials".



using System;
class Program
{
    static void Main()
    {
        Console.Write("Enter unser name: ");
        string username = Console.ReadLine();
        
        Console.Write("Enter Password:");
        string password = Console.ReadLine();
        
        if(username=="admin" &&  password == "Ravi123")
        {
             Console.WriteLine("Login Sucess");
        
        }
        else
        {
            Console.WriteLine("invalid credentials");
        }
       
        
    }
}        


Ravi Rajput

Software Engineer | Angular Expert | ReactJs | Javascript | C# | DotNet Core |.net Maui | DevOps | Fullstack Developer

5 天前

might be you will get an 99.99% , please try to solve, else you can check this code: using System; class RaviCal { ??static void Main() ??{ ????double num1, num2; ????? ????// Read first number with validation ????Console.Write("Enter First Number: "); ????while (!double.TryParse(Console.ReadLine(), out num1)) ????{ ??????Console.Write("Invalid input. Enter a valid number: "); ????} ????// Read operator ????Console.Write("Enter Operator (+, -, *, /): "); ????char op = Console.ReadKey().KeyChar; ????Console.WriteLine(); // Move to the next line ????// Read second number with validation ????Console.Write("Enter Second Number: "); ????while (!double.TryParse(Console.ReadLine(), out num2)) ????{ ??????Console.Write("Invalid input. Enter a valid number: "); ????} ????double result = 0; ????bool valid = true; ???

回复

要查看或添加评论,请登录

Ravi Rajput的更多文章

  • SQL Database(MSSQL)

    SQL Database(MSSQL)

    SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSERT…

    4 条评论
  • Reactjs Curd Operation

    Reactjs Curd Operation

    Hi, I am Writing Here curd operation code in ReactJs, if you want you can implement. and and lets start process.

  • What is System Database in SQL Server Microsoft

    What is System Database in SQL Server Microsoft

    In SQL Server, the system databases are a set of databases that are created and maintained by SQL Server itself. These…

  • Dependency Injection In .Net Web Api

    Dependency Injection In .Net Web Api

    Create dependency Injection. Configure dependency Injection.

社区洞察