🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Adding two numbers is one of the simplest and most common operations in programming. This task can be easily accomplished in JavaScript using different approaches. This guide will walk you through writing a JavaScript program to add two numbers.
Problem Statement
Create a JavaScript program that:
- Accepts two numbers.
- Adds the two numbers.
- Returns the sum.
Example:
Input:
5and10Output:
15Input:
7and3Output:
10
Solution Steps
- Read the Input Numbers: Provide two numbers either as part of user input or directly in the code.
- Add the Numbers: Use the
+operator to sum the two numbers. - Display the Result: Print the sum of the two numbers.
JavaScript Program
// JavaScript Program to Add Two Numbers
// Author: https://www.javaguides.net/
function addTwoNumbers(num1, num2) {
// Step 1: Add the two numbers
const sum = num1 + num2;
// Step 2: Display the result
console.log(`The sum of ${num1} and ${num2} is: ${sum}`);
}
// Example input
let number1 = 5;
let number2 = 10;
addTwoNumbers(number1, number2);
Output Example
The sum of 5 and 10 is: 15
Example with Different Input
If you modify the input to:
let number1 = 7;
let number2 = 3;
The output will be:
The sum of 7 and 3 is: 10
Explanation
Step 1: Add the Two Numbers
- The numbers are passed as arguments to the
addTwoNumbers()function, where they are added using the+operator.
Step 2: Display the Result
- The result of the addition is stored in the variable
sumand displayed usingconsole.log().
Conclusion
This JavaScript program demonstrates a simple way to add two numbers using the + operator. It is useful for learning basic arithmetic operations and how to handle input and output in JavaScript. This program can be extended to handle more complex arithmetic operations as needed.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment