Sum Calculator for Two Numbers in Console

Simple C# console program to input two integers, calculate their sum, and display the result. Ideal for beginners learning basic I/O and arithmetic.

Brief Introduction

Sum Calculator for Two Numbers in Console (csharp)
DESCRIPTION:
A simple C# program that reads two integers entered by the user via console, calculates their sum, and displays the result. Suitable for beginners to understand basic input/output and arithmetic operations.

ADDITIONAL NOTES:
The program is designed to be simple and educational, ideal for those starting to program in C#. It handles invalid input with an error message and requests re-entry.

EXPLANATIONS:
- The Main() method is the program's entry point.
- Console.WriteLine and Console.ReadLine are used to interact with the user via console.
- Input strings are converted to integers using int.TryParse to avoid exceptions on non-numeric input.
- If conversion fails, the user is informed and the program terminates.
- If conversion succeeds, the sum is calculated and the result displayed.

Example Code: csharp

using System;

namespace CalcolatriceSomma
{
    class Program
    {
        static void Main(string[] args)
        {
            // Variables to store the entered numbers
            int numero1, numero2;

            // Request and read the first number
            Console.WriteLine("Inserisci il primo numero intero:");
            string input1 = Console.ReadLine();

            // Check that the input is a valid number
            if (!int.TryParse(input1, out numero1))
            {
                Console.WriteLine("Errore: il valore inserito non è un numero intero valido.");
                return; // Terminate the program
            }

            // Request and read the second number
            Console.WriteLine("Inserisci il secondo numero intero:");
            string input2 = Console.ReadLine();

            // Check that the input is a valid number
            if (!int.TryParse(input2, out numero2))
            {
                Console.WriteLine("Errore: il valore inserito non è un numero intero valido.");
                return; // Terminate the program
            }

            // Calculate the sum
            int somma = numero1 + numero2;

            // Display the result
            Console.WriteLine($"La somma di {numero1} e {numero2} è: {somma}");
        }
    }
}
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Software: .NET SDK (version 6.0 or higher recommended) to compile and run the program.

Software: Text editor or IDE (Visual Studio, Visual Studio Code, Rider, etc.) to write C# code.

Configuration: no external libraries needed, the program uses only the standard System library.

Hardware: any computer with Windows, Linux, or macOS operating system supporting .NET.

Links/Config: no special configuration required, the program runs from console/terminal.