Found 2745 Articles for Csharp

C# Program to Convert Fahrenheit to Celsius

Samual Sam
Updated on 19-Jun-2020 09:28:03

2K+ Views

Firstly, set the Fahrenheit temperature −double fahrenheit = 97; Console.WriteLine("Fahrenheit: " + fahrenheit);Now convert it into Celsius −celsius = (fahrenheit - 32) * 5 / 9;ExampleYou can try to run the following code to convert Fahrenheit to Celsius.Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class MyApplication {       static void Main(string[] args) {          double celsius;          double fahrenheit = 97;          Console.WriteLine("Fahrenheit: " + fahrenheit);          celsius = (fahrenheit - 32) * 5 / 9;          Console.WriteLine("Celsius: " + celsius);          Console.ReadLine();       }    } }OutputFahrenheit: 97 Celsius: 36.1111111111111

C# Program to Convert Decimal to Binary

karthikeya Boyini
Updated on 19-Jun-2020 09:28:41

856 Views

Let’ s say you have set the decimal to be −decVal = 34; Console.WriteLine("Decimal: {0}", decVal);Use the ToString() method for the values you get as a binary number for the decimal value −while (decVal >= 1) {    val = decVal / 2;    a += (decVal % 2).ToString();    decVal = val; }Now set a new empty variable to display the binary number using a loop −string binValue = "";ExampleYou can try to run the following code to convert decimal to binary in C#.Live Demousing System; using System.Collections.Generic; using System.Text; namespace Demo {    class MyApplication {     ... Read More

C# Program to convert Digits to Words

Samual Sam
Updated on 19-Jun-2020 09:29:32

3K+ Views

Firstly, declare the words from 0 to 9 −// words for every digits from 0 to 9 string[] digits_words = { "zero", "one", "two",    "three", "four", "five",    "six", "seven", "eight",    "nine" };The following are the digits to be converted to words −// number to be converted into words val = 4677; Console.WriteLine("Number: " + val);Use the loop to check for every digit in the given number and convert into words −do {    next = val % 10;    a[num_digits] = next;    num_digits++;    val = val / 10; } while(val > 0);ExampleYou can try to ... Read More

C# Program to Convert Character case

karthikeya Boyini
Updated on 19-Jun-2020 09:01:03

338 Views

Let’s say your string is −str = "AMIT";To convert the above uppercase string in lowercase, use the ToLower() method −Console.WriteLine("Converted to LowerCase : {0}", str.ToLower());ExampleThe following is the code in C# to convert character case.Live Demousing System; using System.Collections.Generic; using System.Text; namespace Demo {    class MyApplication {       static void Main(string[] args) {          string str;          str = "AMIT";          Console.WriteLine("UpperCase : {0}", str);          // convert to lowercase          Console.WriteLine("Converted to LowerCase : {0}", str.ToLower());          Console.ReadLine();       }    } }OutputUpperCase : AMIT Converted to LowerCase : amit

C# Program to Convert Binary to Decimal

Samual Sam
Updated on 19-Jun-2020 09:01:36

3K+ Views

Firstly, set the binary value −int num = 101;Now assign the binary to a new variable −binVal = num;Till the value is greater than 0, loop through the binary number and base value like this, while (num > 0) {    rem = num % 10;    decVal = decVal + rem * baseVal;    num = num / 10;    baseVal = baseVal * 2; }ExampleThe following is the code to convert binary to decimal.Live Demousing System; using System.Collections.Generic; using System.Text; namespace Demo {    class MyApplication {       static void Main(string[] args) {       ... Read More

C# program to convert binary string to Integer

karthikeya Boyini
Updated on 19-Jun-2020 09:02:11

663 Views

Use the Convert.ToInt32 class to fulfill your purpose of converting a binary string to an integer.Let’s say our binary string is −string str = "1001";Now each char is parsed −try {    //Parse each char of the passed string    val = Int32.Parse(str1[i].ToString());    if (val == 1)       result += (int) Math.Pow(2, str1.Length - 1 - i);    else if (val > 1)       throw new Exception("Invalid!"); } catch {    throw new Exception("Invalid!"); }Check the above for each character in the passed string i.e. “100” using a for a loop. Find the length of ... Read More

C# program to count occurrences of a word in string

Samual Sam
Updated on 19-Jun-2020 09:02:48

2K+ Views

Set the string first −string str = "Hello World! Hello!";Now check the string for the occurrences of a word “Hello’ and loop through −while ((a = str1.IndexOf(pattern, a)) != -1) {    a += pattern.Length;    count++; }ExampleYou can try to run the following code to count occurrences of a word in a string.Live Demousing System; class Program {    static void Main() {       string str = "Hello World! Hello!";       Console.WriteLine("Occurrence:"+Check.CheckOccurrences(str, "Hello"));    } } public static class Check {    public static int CheckOccurrences(string str1, string pattern) {       int count ... Read More

C# program to count the number of words in a string

karthikeya Boyini
Updated on 19-Jun-2020 09:03:23

5K+ Views

Let us first declare the string −string str = "Hello World!";Now loop through the complete string and find the whitespace or tab or newline character −while (a

C# program to convert a list of characters into a string

Samual Sam
Updated on 19-Jun-2020 09:04:00

607 Views

Firstly, declare the character array and set the value of each character −char[] ch = new char[5]; ch[0] = 'H'; ch[1] = 'e'; ch[2] = 'l'; ch[3] = 'l'; ch[4] = 'o';Now, use the string class constructor and create a new string from the above array of characters −string myChar = new string(ch);ExampleLet us see the code to convert a list of characters to string in C#.Live Demousing System; namespace Demo {    class MyApplication {       static void Main(string[] args) {          char[] ch = new char[5];          ch[0] = ... Read More

C# Program to Check Whether the Entered Number is an Armstrong Number or Not

karthikeya Boyini
Updated on 19-Jun-2020 09:04:36

738 Views

For an Armstrong number, let us say a number has 3 digits, then the sum of cube of its digits is equal to the number itself.For example, 153 is equal to −1³ + 3³ + 5³To check for it using C#, check the value and find its remainder. Here “val” is the number you want to check for Armstrong −for (int i = val; i > 0; i = i / 10) {    rem = i % 10;    sum = sum + rem*rem*rem; }Now compare the addition with the actual value. If it matches, that would mean the ... Read More

Advertisements