🎓 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 most basic operations in programming. This task is often used as a simple demonstration of how functions work. This program will demonstrate how to create a JavaScript function that accepts two numbers, calculates their sum, and returns the result.
Problem Statement
Create a JavaScript function that:
- Accepts two numbers as arguments.
- Calculates the sum of the two numbers.
- Returns and displays the result.
Example:
Input:
5,7Output:
12Input:
10,20Output:
30
Solution Steps
- Create a Function: Define a function that accepts two parameters.
- Calculate the Sum: Add the two parameters inside the function.
- Return the Result: Return the calculated sum.
- Display the Result: Print the sum of the two numbers.
JavaScript Program
// JavaScript Function to Calculate the Sum of Two Numbers
// Author: https://www.javaguides.net/
function sumTwoNumbers(num1, num2) {
// Step 1: Calculate the sum of the two numbers
let sum = num1 + num2;
// Step 2: Return the result
return sum;
}
// Example input
let number1 = 5;
let number2 = 7;
let result = sumTwoNumbers(number1, number2);
console.log(`The sum of ${number1} and ${number2} is: ${result}`);
Output
The sum of 5 and 7 is: 12
Example with Different Input
let number1 = 10;
let number2 = 20;
let result = sumTwoNumbers(number1, number2);
console.log(`The sum of ${number1} and ${number2} is: ${result}`);
Output:
The sum of 10 and 20 is: 30
Explanation
Step 1: Create a Function
- The function
sumTwoNumbers()is defined with two parameters,num1andnum2, which represent the two numbers to be summed.
Step 2: Calculate the Sum
- Inside the function, the sum of
num1andnum2is calculated using the+operator.
Step 3: Return and Display the Result
- The result of the addition is returned by the function and printed using
console.log().
Conclusion
This JavaScript function demonstrates how to calculate the sum of two numbers by defining a simple function with parameters. This solution is basic yet fundamental and can be extended to more complex mathematical operations or integrated into larger applications.
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