100% found this document useful (1 vote)
69 views

Js Exercise Programs

The document provides code examples and outputs for 20 different JavaScript programs that demonstrate basic programming concepts like printing text, performing mathematical operations, conditional logic, loops, functions and more. The programs cover topics such as Hello World, addition, square root, area of a triangle, swapping variables, unit conversion, checking number properties, prime numbers, factorials, and Fibonacci sequences.

Uploaded by

bhagyasree0409
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
69 views

Js Exercise Programs

The document provides code examples and outputs for 20 different JavaScript programs that demonstrate basic programming concepts like printing text, performing mathematical operations, conditional logic, loops, functions and more. The programs cover topics such as Hello World, addition, square root, area of a triangle, swapping variables, unit conversion, checking number properties, prime numbers, factorials, and Fibonacci sequences.

Uploaded by

bhagyasree0409
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

JavaScript Program To Print Hello World

Code:
<html>
<head></head>
<body>
<script>
document.write("Hello World")
</script>
</body>
</html>
Output:
Hello World

JavaScript Program to Add Two Numbers


Code:
<html>
<head></head>
<body>
<script>
var a=15;
var b=20;
var sum=a+b;
document.write("Sum=" + sum);
</script>
</body>
</html>
Output:
Sum=35

JavaScript Program to Find the Square Root


Code:
<html>
<head></head>
<body>
<script>
var SR = Math.sqrt(121);
document.write("The square root of given number is: " + SR);
</script>
</body>
</html>
Output:
The square root of given number is: 11

JavaScript Program to Calculate the Area of a Triangle


Code:
<!DOCTYPE html>
<html>
<body>
<script>
var b,h,area;
b= parseFloat(prompt("Enter Base"));
h =parseFloat(prompt("Enter Height"));
area = 0.5*b*h;
document.write('Base=' +b+ '<br>');
document.write('Height=' +h+ '<br>');
document.write('Area of this triangle is:' +area);
</script>
</body>
</html>
Output:
Base=2
Height=4
Area of this triangle is:4

JavaScript Program to Swap Two Variables


Code:
<!DOCTYPE html>
<html>
<body>
<script>
let a = 3;
let b = 7;
let temp;
temp = a;
a = b;
b = temp;
document.write("The value of a after swapping: " + a + " </br> ");
document.write("The value of b after swapping: " + b + " </br> ");
</script>
</body>
</html>
Output:
The value of a after swapping: 7
The value of b after swapping: 3

JavaScript Program to Convert Kilometers to Miles


Code:
<html>
<head></head>
<body>
<script>
var kms = 5
var miles = kms / 1.609;
document.write(miles);
</script>
</body>
</html>
Output:
3.107520198881293

Javascript Program to Convert Celsius to Fahrenheit


Code:
<html>
<head></head>
<body>
<script>
let c = 50;
let f = 0;
f = (c * (9 / 5)) + 32;
document.write("The value of the temperature in Fahrenheit is " + f);
</script>
</body>
</html>
Output:
The value of the temperature in Fahrenheit is 122

Javascript Program to Check if a number is Positive, Negative, or Zero


Code:
<html>
<head></head>
<body>
<script>
let n = parseInt(prompt("Enter a number: "));
if (n > 0) {
document.write("The number is positive");
}
else if (n == 0) {
document.write("The number is zero");
}
else {
document.write("The number is negative");
}
</script>
</body>
</html>
Output:
The number is zero

Javascript Program to Check if a Number is Odd or Even


Code:
<html>
<head></head>
<body>
<script>
let n = prompt("Enter a number: ");
if(n % 2 == 0) {
document.write("The number is even.");
}
else {
document.write("The number is odd.");
}
</script>
</body>
</html>
Output:
The number is odd.

JavaScript Program to Find the Largest Among Three Numbers


Code:
<html>
<head></head>
<body>
<script>
var x=5;
var y=15;
var z=20;
document.write(Math.max(x,y,z)+" is the largest");
</script>
</body>
</html>
Output:
20 is the largest

JavaScript Program to Check Prime Number


Code:
<!doctype html>
<html>
<body>
<script>
var num, i, count=0;
num=19;
for(i=2; i<num; i++){
if(num%2==0){
count++;
break;
}
}
if(count==0)
document.write(num + " is a Prime Number");
else
document.write(num + " is not a Prime Number");
</script>
</body>
</html>
Output:
19 is a Prime Number

JavaScript Program to Print All Prime Numbers in an Interval


Code:
<html>
<head></head>
<body>
<script>
const lowerNumber = parseInt(prompt('Enter lower number: '));
const higherNumber = parseInt(prompt('Enter higher number: '));
console.log("The prime numbers between lowerNumber and higherNumber
are:");

for (let i = lowerNumber; i <= higherNumber; i++) {


let flag = 0;
for (let j = 2; j < i; j++) {
if (i % j == 0) {
flag = 1;
break;
}
}
if (i > 1 && flag == 0) {
console.log(i);
}
}
</script>
</body>
</html>

JavaScript Program to Find the Factorial of a Number


Code:
<html>
<head></head>
<body>
<script>
function factorial(n){
if(n < 0){
return "number has to be positive."
}
if(n == 0 || n == 1){
return 1;
}
else{
return n * factorial(n-1);
}
}
let n = 5;
answer = factorial(n)
document.write("Factorial of " + n + " : " + answer);
</script>
</body>
</html>
Output:
Factorial of 5 : 120

JavaScript Program to Display the Multiplication Table


Code:
<html>
<head></head>
<body>
<script>
var num = parseInt(prompt('Enter an integer: '));
for(let i = 1; i <= 10; i++) {
var res = i * num;
console.log(num +" * "+ i + " = " + res);
}
</script>
</body>
</html>

JavaScript Program to Print the Fibonacci Sequence


Code:
<html>
<head></head>
<body>
<script>
const number = parseInt(prompt('Enter the number of terms: '));
let n1 = 0, n2 = 1, nextTerm;
document.write('Fibonacci Series:');
for (let i = 1; i <= number; i++) {
document.write(n1);
nextTerm = n1 + n2;
n1 = n2;
n2 = nextTerm;
}
</script>
</body>
</html>
Output:
Fibonacci Series:01123

JavaScript Program to Check Armstrong Number


Code:
<html>
<head></head>
<body>
<script>
const number = prompt("Enter a positive integer");
const numberOfDigits = number.length;
let sum = 0;
let temp = number;
while (temp > 0) {
let remainder = temp % 10;
sum += remainder ** numberOfDigits;
temp = parseInt(temp / 10);
}
if (sum == number) {
document.write("The number is an Armstrong number");
}
else {
document.write("The number is not an Armstrong number.");
}
</script>
</body>
</html>
Output:
The number is not an Armstrong number.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy