c-sharp
  1. c-sharp-program-to-print-an-integer-entered-by-the-user

Program to Print an Integer (Entered by the User) - (C# Basic Programs)

Printing an integer entered by the user is one of the most basic programming exercises in any programming language. In this tutorial, we will discuss a C# program to print an integer entered by the user.

Syntax

The syntax for printing an integer entered by the user in C# is as follows:

Console.WriteLine("Enter an integer: ");
int num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("You entered: " + num);

Example

using System;

namespace PrintInteger
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter an integer: ");
            int num = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("You entered: " + num);
        }
    }
}

Output

Enter an integer:
24
You entered: 24

Explanation

The above program prompts the user to enter an integer and then reads it using the Console.ReadLine() method. The Convert.ToInt32() method is used to convert the string input to an integer, and the result is stored in the num variable. Finally, the integer entered by the user is printed to the console using the Console.WriteLine() method.

Use

Printing an integer entered by the user is a simple and commonly used programming task. It is useful for accepting user input and processing it in various ways.

Summary

In this tutorial, we discussed a simple C# program to print an integer entered by the user. We covered the syntax, example, explanation, use, and importance of printing an integer entered by the user using C#. This basic programming exercise is an essential building block for many more complex programs.

Published on: