EASY WAYS TO PRINT SUM OF FIVE NUMBERS IN TYPESCRIPT

Introduction

In this task, we will write a simple TypeScript program to calculate and print the sum of five numbers. TypeScript is a strongly typed superset of JavaScript that adds static typing and other features to help with larger applications. This program will introduce basic TypeScript concepts such as variables, functions, and input/output operations.

// Function to add five numbers
function addFiveNumbers(num1: number, num2: number, num3: number, num4: number, num5: number): number {
    // Sum the five numbers and return the result
    return num1 + num2 + num3 + num4 + num5;
}

// Declare five numbers
let number1: number = 10;
let number2: number = 20;
let number3: number = 30;
let number4: number = 40;
let number5: number = 50;

// Call the function to add the numbers
let sum: number = addFiveNumbers(number1, number2, number3, number4, number5);

// Print the result
console.log("The sum of the five numbers is: " + sum);
TypeScript

Explanation of Each Line:

  1. Function Declaration (function addFiveNumbers...):
    • This line defines a function called addFiveNumbers that takes five parameters (num1, num2, num3, num4, num5). Each parameter is of type number. The function will return a number, which is the sum of the five numbers.
  2. Summing the Numbers (return num1 + num2 + num3 + num4 + num5;):
    • The body of the function contains a simple statement that adds the five numbers together and returns the result. This line does the actual addition.
  3. Declaring Variables (let number1: number = 10;):
    • Here, we declare five variables (number1 through number5) using the let keyword. Each variable is of type number, and we initialize them with values: 10, 20, 30, 40, and 50 respectively.
  4. Calling the Function (let sum: number = addFiveNumbers...):
    • This line calls the addFiveNumbers function, passing the five declared numbers as arguments. The result (sum of the numbers) is stored in a variable called sum.
  5. Printing the Result (console.log(...)):
    • This line prints the result to the console. The console.log() method is used for output. The message includes the text "The sum of the five numbers is: " concatenated with the value of sum.

Explanation of Output:

When the program is run, it will output the following line to the console:

The sum of the five numbers is: 150
TypeScript

Explanation:

The function addFiveNumbers calculates the sum of 10 + 20 + 30 + 40 + 50, which equals 150.

The console.log() function outputs the text "The sum of the five numbers is: " followed by the calculated sum (150).

Conclusion

This TypeScript program demonstrates how to declare variables, write a simple function, and print the result in the console. The program can be easily modified to take user input or change the numbers for a more dynamic calculation. The core idea is that functions in TypeScript allow us to encapsulate logic and reuse it as needed.

LEARN TYPESCRIPT FROM THESE SITES

To learn TypeScript effectively, here are a few recommended resources:

  1. Learn TypeScript Interactive Tutorial: This is an excellent resource for beginners to interactively learn TypeScript. It offers a structured approach with tutorials, exercises, and solutions, making it ideal for hands-on learning. You can explore basic and advanced topics, from variables and types to decorators and abstract classes. Check it out here​(TypeScript Tutor).
  2. FreeCodeCamp’s TypeScript Tutorial: This platform provides a comprehensive introduction to TypeScript with exercises, practical projects, and explanations of essential TypeScript features like types, interfaces, and classes. You can find this course on FreeCodeCamp.
  3. TypeScript Fast Crash Course (Dev.to): This quick course provides a focused introduction to TypeScript, covering core concepts like types, modules, and classes in a digestible format. It’s great for those looking to get started quickly. Learn more on Dev.to​(DEV Community).

These platforms will help you understand TypeScript fundamentals and advanced features, giving you practical experience along the way!

Compile any Type Script Code

Leave a Reply

Your email address will not be published. Required fields are marked *

Resize text
Scroll to Top