0% found this document useful (0 votes)
95 views

Message Display Using Java Script

The document contains 11 code examples demonstrating the use of JavaScript for various tasks like displaying messages, adding numbers, checking if a number is odd or even, swapping variables, calculating factorials, checking for Armstrong, palindrome and Fibonacci numbers, and printing star patterns in a webpage. The code snippets show how to use basic JavaScript functions, operators, loops and DOM manipulation to perform calculations and display output in the browser.

Uploaded by

Pavithra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

Message Display Using Java Script

The document contains 11 code examples demonstrating the use of JavaScript for various tasks like displaying messages, adding numbers, checking if a number is odd or even, swapping variables, calculating factorials, checking for Armstrong, palindrome and Fibonacci numbers, and printing star patterns in a webpage. The code snippets show how to use basic JavaScript functions, operators, loops and DOM manipulation to perform calculations and display output in the browser.

Uploaded by

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

1.

Message display using Java script


<!doctype html>
<html>
<head>
<script>
function hello()
{
("Hello Welcome To AVCCE II MCA Studetns in JS World");
}
</script>
</head>
<body>
<button onclick="hello()">Click Me</button></br></br>
</body>
</html>

Output;

2. Addition program using Java Script

<!doctype html>
<html>
<head>
<script>
function add()
{
var a,b,c;
a=Number(document.getElementById("first").value);
b=Number(document.getElementById("second").value);
c= a + b;
document.getElementById("answer").value= c;
}
</script>
</head>
<body>

AVCCE/MCA/SS Java Script Programs Page 1


<h1>Addtion Using JS</h1>
Enter the First number : <input id="first">
Enter the Second number: <input id="second">
<button onclick="add()">Add</button>
<input id="answer">
</body>
</html>

Output:-

3. JS- Odd Even Number

<!doctype html>
<html>
<head>
<script>
function odd_even()
{
var no;
no=Number(document.getElementById("no_input").value);
if(no%2==0)
{
alert("Even Number");
}
else
{
alert("Odd Number");
}
}
</script>
</head>
<body>

<h2>JS- Odd Even Number></h2>


Enter Any Number:<input id="no_input"><br />

AVCCE/MCA/SS Java Script Programs Page 2


<button onclick="odd_even()">Click me</button>
</body>
</html>

Output:-

4. JS- Swap Two Number

<!doctype html>
<html>
<head>
<script>
function swap()
{
var a,b,c;
a=Number(document.getElementById("first").value);
b=Number(document.getElementById("second").value);
c=a;
a=b;
b=c;
document.getElementById("answer1").value= a;
document.getElementById("answer2").value= b;
}
</script>
</head>
<body>
<h1>JS- Swap Two Number<br> </h1>
Value of a: <input id="first">
Value of b: <input id="second"></br></br>
<button onclick="swap()">Swap</button></br></br>
Value of a: <input id="answer1">
Value of b: <input id="answer2">
</body>
</html>

AVCCE/MCA/SS Java Script Programs Page 3


Output:-

5. Factorial of Number

<!doctype html>
<html>
<head>
<script>
function show()
{
var i, no, fact;
fact=1;
no=Number(document.getElementById("num").value);
for(i=1; i<=no; i++)
{
fact= fact*i;
}
document.getElementById("answer").value= fact;
}
</script>
</head>
<body>
JS- Factorial of Number<br>
Enter Num: <input id="num">
<button onclick="show()">Factorial</button>
<input id="answer">
</body>
</html>
Output:-

AVCCE/MCA/SS Java Script Programs Page 4


6. JS-Armstrong number

<!doctype html>
<html>
<head>
<script>
function armstr()
{
var arm=0,a,b,c,d,num;
num=Number(document.getElementById("no_input").value);
temp=num;
while(temp>0)
{
a=temp%10;
temp=parseInt(temp/10); // convert float into Integer
arm=arm+a*a*a;
}
if(arm==num)
{
alert("Armstrong number");
}
else
{
alert("Not Armstrong number");
}
}
</script>
</head>
<body>
JS-Armstrong number <br>
Enter any Number: <input id="no_input">
<button onclick="armstr()">Check</button></br></br>
</body>
</html>
Output:-

AVCCE/MCA/SS Java Script Programs Page 5


7. Palindrome Number Program in JavaScript

<!doctype html>
<html>
<head>
<script>
function palin()
{
var a,no,b,temp=0;
no=Number(document.getElementById("no_input").value);
b=no;
while(no>0)
{
a=no%10;
no=parseInt(no/10);
temp=temp*10+a;
}
if(temp==b)
{
alert("Palindrome number"); }
else
{
alert("Not Palindrome number"); }
}
</script>
</head>
<body>Palindrome Number Program in JavaScript<br>
Enter any Number: <input id="no_input">
<button onclick="palin()">Check</button></br></br>
</body>
</html>
Output:-

AVCCE/MCA/SS Java Script Programs Page 6


8. Print Triangle of Stars in JavaScript

<html>
<head>Print Triangle of Stars in JavaScript </head>
<br>
<script type="text/javascript">
var i, j;
//outer loop
for(i=5;i>=1;i--)
{
//inner loop
for(j=1;j<=i;j++)
{
document.write('*');
}
document.write('<br/>');
}

</script>
<body>
</body>
</html>
Output:-

9. Print Star Pattern in JavaScript

<html>
<head>Print Star Pattern in JavaScript <br>
<script type="text/javascript">
var i, j;
//outer loop
for(i=1; i <= 5; i++)
{
//inner loop
for(j=1; j<=i; j++)
{
document.write('*');

AVCCE/MCA/SS Java Script Programs Page 7


}
document.write('<br/>');
}

</script>
</head>
<body> </body>
</html>
Output:-

10. Multiplication Table using Java Script

<html>
<head>
<title>Multiplication Table</title>
<script type="text/javascript">
var rows = prompt("How many rows for your multiplication table?");
var cols = prompt("How many columns for your multiplication table?");
if(rows == "" || rows == null)
rows = 10;
if(cols== "" || cols== null)
cols = 10;
createTable(rows, cols);
function createTable(rows, cols)
{
var j=1;
var output = "<table border='1' width='500' cellspacing='0'cellpadding='5'>";
for(i=1;i<=rows;i++)
{
output = output + "<tr>";
while(j<=cols)
{
output = output + "<td>" + i*j + "</td>";
j = j+1;

AVCCE/MCA/SS Java Script Programs Page 8


}
output = output + "</tr>";
j = 1;
}
output = output + "</table>";
document.write(output);
}
</script>
</head>
<body>
</body>
</html>

AVCCE/MCA/SS Java Script Programs Page 9


11. Java Script program for generating Fibonacci Series

Coding: - feb.html
<html>
<head>
<title>Fibonacci series</title>
<script>
function display()
{
var n=parseInt(frm.t1.value)
var a=0,b=1,c=0,s=" ";
while((a+b)<=n)
{
c=a+b;
s=s+c+ " , ";
b=a;
a=c;
}
alert("Fibonacci Series: "+s)
}
</script>
</head>
<body>
<form name=frm >
<input type=text name=t1>
<input type=button name=b1 onclick="display();" value="fibonacci series">
</form>
</body>
</html>

OUTPUT:-

AVCCE/MCA/SS Java Script Programs Page 10

You might also like