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:
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.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework