Slip 9 - E) Write a program that demonstrates the use of primitive data types in C#. The program should also support the type conversion of : ● Integer to String ● String to Integer

Solution:


using System;

class Program {
    static void Main(string[] args) {
        // Demonstrate primitive data types
        bool isTrue = true;
        char letter = 'a';
        int number = 10;
        float decimalNumber = 3.14f;
        double bigDecimalNumber = 99999999.99999999;
        decimal preciseDecimalNumber = 1.23456789m;

        Console.WriteLine("Boolean: " + isTrue);
        Console.WriteLine("Character: " + letter);
        Console.WriteLine("Integer: " + number);
        Console.WriteLine("Float: " + decimalNumber);
        Console.WriteLine("Double: " + bigDecimalNumber);
        Console.WriteLine("Decimal: " + preciseDecimalNumber);

        // Convert integer to string
        int intNumber = 123;
        string strNumber = intNumber.ToString();
        Console.WriteLine("Integer converted to string: " + strNumber);

        // Convert string to integer
        string strNumber2 = "456";
        int intNumber2 = Int32.Parse(strNumber2);
        Console.WriteLine("String converted to integer: " + intNumber2);
    }
}

Post a Comment

0 Comments