0% found this document useful (0 votes)
29 views40 pages

Javascript

The document contains 12 code snippets demonstrating JavaScript programs for finding the smallest of 3 numbers, swapping two numbers using a function, checking if a number is Armstrong, checking if a number is prime, generating the Fibonacci series up to a limit, printing patterns, sorting elements in an array, calculating the sum and average of 3 numbers using prompts, converting binary to decimal, reversing a number using a form, searching an element in an array, and performing arithmetic operations using a switch statement.

Uploaded by

binila1125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views40 pages

Javascript

The document contains 12 code snippets demonstrating JavaScript programs for finding the smallest of 3 numbers, swapping two numbers using a function, checking if a number is Armstrong, checking if a number is prime, generating the Fibonacci series up to a limit, printing patterns, sorting elements in an array, calculating the sum and average of 3 numbers using prompts, converting binary to decimal, reversing a number using a form, searching an element in an array, and performing arithmetic operations using a switch statement.

Uploaded by

binila1125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 40

1.

Smallest of 3 Numbers

<!DOCTYPE html>
<html>
<head>
<title>Smallest of 3 Numbers</title>
</head>
<body>
<script type="text/javascript">
vara,b,c;
a=parseInt(prompt("Enter First Number"));
b=parseInt(prompt("Enter Second Number"));
c=parseInt(prompt("Enter Third Number"));
if(a<b)
{
if(a<c)
{
document.write(a+"is Smallest");

}
else
{
document.write(c+"is Smallest");
}
}
else
{
if(b<c)
{
document.write(b+"is Smallest");
}
else
{
document.write(c+"is Smallest");
}
}

</script>
</body>
</html>
2. Swapping of 2 numbers using function

<!DOCTYPE html>

<html>

<head>

<title>Swapping of 2 Numbers</title>

</head>

<body>

<script type="text/javascript">

function swapping()

vara,b;

a=parseInt(prompt("Enter First Number"));

b=parseInt(prompt("Enter Second Number"));

document.write("Before Interchanging the Value of A="+a);

document.write("<br>");

document.write("Before Interchanging the Value of B="+b);

document.write("<br>");

a=a+b;

b=a-b;

a=a-b;

document.write("After Interchanging the Value of A="+a);

document.write("<br>");

document.write("After Interchanging the Value of B="+b);

swapping();

</script>
</body>

</html>
3. Amstrong or not

<!DOCTYPE html>
<html>
<head>
<title>Amstrong or not</title>
</head>
<body>
<script type="text/javascript">
vartemp,n,rem,sum=0;
n=prompt("Enter a Number:");
temp=n;

while(n>0)
{
rem=n%10;
sum=sum+(rem*rem*rem);
n=parseInt(n/10);
}
if(temp==sum)
{
document.write(temp+" is an Amstrong Number");

}
else
{
document.write(temp+" is Not an Amstrong Number");
}
</script>

</body>
</html>
4. Prime or Not

<!DOCTYPE html>

<html>

<head>

<title>Prime or Not</title>

</head>

<body>

<script type="text/javascript">

varn,i=2,p=1;

n=parseInt(prompt("Enter a Number"));

do

if(n%i==0)

p=0;

break;

i++;

}while(i<=n/2);

if (p==1)

document.write(n+" is a Prime Number");

else

document.write(n+" is Not a Prime Number");


}

</script>

</body>

</html>
5. Fibonacci series up to the limit
<!DOCTYPE html>
<html>
<head>
<title>Fibonacci Series</title>
<script type="text/javascript">

functionfibonacci(n)
{
var i,f1=-1,f2=1,f3;
for(i=1;i<=n;i++)
{
f3=f1+f2;
document.write(f3+" ");
f1=f2;
f2=f3;
}
}
</script>
</head>
<body>
<script type="text/javascript">
var n;
n=parseInt(prompt("Enter the Limit"));
fibonacci(n);
</script>
</body>
</html>
6. Pattern printing

**********
*********
********
*******
******
*****
****
***
**
*
<!DOCTYPE html>

<html>

<head>

<title> Pattern Printing</title>

</head>

<body>

<script type="text/javascript">

vari,j,n;

n=parseInt(prompt("Enter the Pattern Limit:"));

for(i=1;i<=n;i++)

for(j=n;j>=i;j--)

document.write("*"+" ");

document.write("<br>");

</script>
</body>

</html>
7. Sort an element from an array

<!DOCTYPE html>

<html>

<head>

<title> To Sort Integer Array</title>

</head>

<body>

<script type="text/javascript">

vararr=[10,9,40,23,1,46,2,8];

document.write("Array = ");

document.write(arr);

vari,j,t;

varlen=arr.length;

for(i=0;i<=len;i++)

for(j=i+1;j<len;j++)

if(arr[i]>arr[j])

t=arr[i];

arr[i]=arr[j];

arr[j]=t;

var r=arr.join();

//alert (r);
document.write("<br><br><br> Sorted Array = ")

document.write(r);

</script>

</body>

</html>
8. Sum and average of 3 numbers using prompt
<!DOCTYPE html>
<html>
<head>
<title>Sum and Average of 3 Numbers</title>
</head>
<body>
<script type="text/javascript">
vara,b,c,sum,average;
a=parseInt(prompt("Enter 1st Number"));
b=parseInt(prompt("Enter 2nd Number"));
c=parseInt(prompt("Enter 3rd Number"));
sum=a+b+c;
average=sum/3;
document.write("Sum="+sum);
document.write("<br>Average="+average);

</script>

</body>
</html>
9. Binary to Decimal conversion

<!DOCTYPE html>

<html>

<head>

<title>BINARY TO DECIMAL CONVERSION</title>

</head>

<body>

<form>

<b>ENTER BINARY CODE</b>

<br>

<input type="number" id="input" >

<br><br>

<input type="button" name="button" onclick="binary()" value="FIND">

<input type="reset" name="button">

<br><br>

<b>DECIMAL NUMBER</b>

<br>

<input type="number" id="output">

</form>

<script type="text/javascript">

function binary()

var n=parseInt(input.value);

var rem;

var p=0;

vardec=0;
while (n>0)

rem = n % 10;

dec = dec+(rem*Math.pow(2,p));

p=p+1

n = parseInt(n/10);

output.value = dec;

</script>

</body>

</html>
10. Reverse of a Number using Form

<!DOCTYPE html>
<html>
<head>
<title>Reverse of a Number</title>
</head>
<body>
<form>
<b>Enter a Number</b>
<br>
<input type="number" name="input" id="input">
<br><br>
<input type="button" name="button" value="REVERSE" onclick="reverse()">
<input type="reset" name="reset" value="RESET">
<br><br>
<b>Result</b>
<br>
<input type="number" name="output" id="output">
</form>
<script type="text/javascript">
function reverse()
{
var n=parseInt(input.value);
var rem;
var rev=0;

while (n>0)
{
rem=n % 10;
rev=rev*10+rem;
n=parseInt(n/10);
}
output.value=rev;

}
</script>
</body>
</html>
11. Search an Element from a given Array

<!DOCTYPE html>
<html>
<head>
<title>Search an Element in an Array</title>
</head>
<body>
<script type="text/javascript">
var i,n,p=0,len;
var ar=[5,10,15,20,25,35,40];
n=parseInt(prompt("Enter Element to Search"));
len=ar.length;
for(i=0;i<len;i++)
{
if(n==ar[i])
{
p=1;
break;
}
}
if (p==1)
{
document.write(n+ " Element Found at Position "+ (i+1));
}
else
{
document.write(n+" Not Found ");
}
</script>
</body>
</html>
12. Arithmetic Operations using switch statement

<!DOCTYPE html>
<html>
<head>
<title>Arithametical Operations using Switch Statement</title>
</head>
<body>
<script type="text/javascript">
var ap;
var a=parseInt(prompt("Enter First Number"));
var b=parseInt(prompt("Enter Second Number"));

ap=parseInt(prompt("Please Select your Choice \n 1.Addition \n 2.Subtraction \n


3.Multliplication \n 4.Division \n 5.Remainder"));

switch (ap)
{
case 1:
document.write("Addition is "+ (a+b));
break;

case 2:
document.write("Subtraction is "+(a-b));
break;

case 3:
document.write("Multliplication is "+(a*b));
break;

case 4:
document.write("Division is "+(a/b));
break;

case 5:
document.write("Remainder is "+(a%b));
break;

default:
document.write("Enter valid number");
}
</script>
</body>
</html>
13 .Factorial of a Number using Form

<!DOCTYPE html>

<html>

<head>

<title>Factorial of a Number using Form</title>

</head>

<body>

<form>

<b>Enter a Number</b>

<br>

<input type="number" name="input" id="input">

<br><br>

<input type="button" name="button" value="FIND" onclick="find()">

<input type="reset" name="button">

<br><br>

<b>Output</b>

<br>

<input type="number" name="Output" id="output">

</form>

<script type="text/javascript">

function find()

var n=parseInt(input.value);

var i=1;
var fact=1;

for (var i =1 ; i <=n ; i++)

fact=fact*i;

output.value=fact;

</script>

</body>

</html>

You might also like