Converting string to int | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Converting string to int

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Code_Coach_Challenge { class Program { static void Main(string[] args) { //your code goes here int a = Console.ReadLine(); int b = 30; int x = a % b; Convert.ToInt32(Console.WriteLine(x)); } } } I don't understand what is wrong with this code. Did I convert to int in the wrong spot?

23rd Mar 2021, 3:57 PM
Skipps
2 Answers
+ 4
Skipps WriteLine used to print values and ReadLine() use to get data. Console.ReadLine() returns String value so to get int value you have to convert using Convert.ToInt32(); So here int n = Convert.ToInt32(Console.ReadLine()); ----To print n ---- Console.WriteLine(n);
23rd Mar 2021, 4:39 PM
A͢J
A͢J - avatar
+ 1
Console.writeline does not create string. It writes argument to standard output stream.
23rd Mar 2021, 4:06 PM
Igor Kostrikin
Igor Kostrikin - avatar