Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Convert an integer to string and a string to an integer

C# Sharp Basic: Exercise-79 with Solution

Write a C# Sharp program to convert an integer to string and a string to an integer.

Sample Solution:

C# Sharp Code:

using System;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string n_str = "50";            
            Console.WriteLine("Original value and type: " + n_str + ",  " + n_str.GetType());
            int result = test_str_to_int(n_str);
            Console.WriteLine("Convert string to integer:");
            Console.WriteLine("Return value and type: "+ result+",  "+result.GetType());
            int n = 122;
            Console.WriteLine("\nOriginal value and type: " + n + ",  " + n.GetType());
            string result1 = test_int_to_str(n);
            Console.WriteLine("Convert integer to string:");
            Console.WriteLine("Return value and type: " + result1 + ",  " + result1.GetType());
        }
        public static int test_str_to_int(string str)
        {
            return int.Parse(str);
        }

        public static string test_int_to_str(int n)
        {
            return n.ToString();
        }
    }
}

Sample Output:

Original value and type: 50,  System.String
Convert string to integer:
Return value and type: 50,  System.Int32

Original value and type: 122,  System.Int32
Convert integer to string:
Return value and type: 122,  System.String

Flowchart:

Flowchart: C# Sharp Exercises - Convert an integer to string and a string to an integer.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to find sum of squares of elements of a given array of integers.
Next: Write a C# Sharp program to convert all the values of a given array of mixed values to string values.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.