Q:

C# | Input and print an integer number

belongs to collection: C# Basic Programs | basics

0

Since, to read a string value or to print a line, we use Console.ReadLine() - but, we can convert it into an integer value.

Syntax to convert string formatted value to integer:

    integer_variable = Convert.ToInt32(Console.ReadLine());

Here, Convert is a class in C# and ToInt32() is a static member of it – which is used to convert a string value to the 4 bytes integer.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

/*C# program to input and print an integer number*/
using System;
class ReadIntExample {
    static void Main() {
        //declare an integer variable
        int num = 0;
        //prompt message to take input
        Console.Write("Input an integer value: ");
        num = Convert.ToInt32(Console.ReadLine());
        //print the value
        Console.WriteLine("num = " + num);
    }
}

Output

 
Input an integer value: 200
num = 200

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | basics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to demonstrate example of arithmetic op... >>
<< C# program to define various types of constants...