Double and Char - (C# Basic Programs)
Data types are very important in programming languages as they define the type of data that can be stored and manipulated in a program. In this tutorial, we will discuss the use of Double and Char data types in C# programming language.
Syntax
The syntax for using the Double data type in C# is as follows:
double variableName = value;
The syntax for using the Char data type in C# is as follows:
char variableName = 'value';
Example
Double
using System;
class Program {
static void Main(string[] args) {
double num1, num2, sum;
Console.Write("Enter first number: ");
num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter second number: ");
num2 = Convert.ToDouble(Console.ReadLine());
sum = num1 + num2;
Console.WriteLine("The sum is: " + sum);
}
}
Output:
Enter first number: 3.5
Enter second number: 4.7
The sum is: 8.2
Char
using System;
class Program {
static void Main(string[] args) {
char character = 'A';
Console.WriteLine("The character is: " + character);
}
}
Output:
The character is: A
Explanation
The Double data type is used to store floating-point numbers with a precision of up to 15 digits. It is a 64-bit data type.
The Char data type is used to store a single character. It is a 16-bit data type.
In the examples above, we have demonstrated the use of Double and Char data types. We have shown how to declare and assign values to these data types and how to use them in a program.
Use
Double data type is used when we need to store decimal values with high precision. It is commonly used in mathematical calculations.
Char data type is used to store single characters like alphabets, numbers, and special characters. It is useful when programming text-based applications.
Summary
In this tutorial, we discussed the use of Double and Char data types in C# programming language. We covered the syntax, examples, explanation, and use of these data types. By understanding these data types, programmers can improve their ability to store and manipulate data in a C# program.