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

Javascript

The document contains JavaScript code snippets for various number-related programs including calculating factorials, checking if a number is perfect, Armstrong, or prime, printing prime numbers between 1-89, checking if numbers are even or odd, reversing a number, calculating the sum of digits in a number, calculating the sum of the first and last digits, generating the Fibonacci series for a given number, and checking if a year is a leap year.

Uploaded by

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

Javascript

The document contains JavaScript code snippets for various number-related programs including calculating factorials, checking if a number is perfect, Armstrong, or prime, printing prime numbers between 1-89, checking if numbers are even or odd, reversing a number, calculating the sum of digits in a number, calculating the sum of the first and last digits, generating the Fibonacci series for a given number, and checking if a year is a leap year.

Uploaded by

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

<!--factorial 4! = 4 x 3 x 2 x 1 = 24.

-->

<!DOCTYPE html>

<html>

<head>

</head>

<body style = "text-align: center; font-size: 20px;">

<h1> javascript program to find out Factorial of given number </h1>

Enter a number: <input id = "num">

<br><br>

<button onclick = "fact()"> Factorial </button>

<script>

function fact(){

var i, num, f;

f = 1;

num = document.getElementById("num").value;

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

f = f * i;

i = i - 1;

document.write( "The factorial of the number " + i + " is: " + f);

</script>

</body>

</html>

<!-- perfect number, a positive integer that is equal to the sum of its proper divisors.
The smallest perfect number is 6, which is the sum of 1, 2, and 3.

6=1,2,3(1+2+3+=6)

-->

<html>

<head>

<script>

function Perfect()

var flag,number,remainder,addition = 0,i;

number = Number(document.getElementById("N").value);

flag = number;

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

remainder = number%i;

if(remainder==0)

addition += i;

if(addition == flag)

window.alert("-The inputed number is Perfect");

else

window.alert("-The inputed number is not Perfect");

}
}

</script>

</head>

<body>

<br>

<h1>Whether a number is Perfect or not</h1>

Enter The Number :<input type="text" name="n" id = "N"/>

<hr color="red">

<br>

<center><button onClick="Perfect()">CHECK</button>

</body>

</html>

Armstrong

<html>

<head>

<script>

function Armstrong()

var flag,number,remainder,addition = 0;

number = Number(document.getElementById("N").value);

flag = number;

while(number > 0)

remainder = number%10;

addition = addition + remainder*remainder*remainder;

number = parseInt(number/10);
}

if(addition == flag)

window.alert("-The inputed number is Armstrong");

else

window.alert("-The inputed number is not Armstrong");

</script>

</head>

<body>

<br>

<h1>Whether a number is Armstrong or not</h1>

Enter The Number :<input type="text" name="n" id = "N"/>

<hr color="red">

<br>

<center><button onClick="Armstrong()">CHECK</button>

</body>

</html>

Prime number

<!doctype html>

<html>

<body style = "text-align: center; font-size: 20px;">

<h1> javascript program to check the given number is prime or not </h1>
Enter a number: <input id = "num">

<br><br>

<button onclick = "primenum()"> prime </button>

<script>

function primenum(){

var num, i, chk=0;

num = document.getElementById("num").value;

for(i=2; i<num; i++)

if(num%2==0)

chk++;

break;

if(chk==0)

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

else

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

</script>

</body>

</html>

Prime 1 to 89

<!doctype html>
<html>

<body style = "text-align: center; font-size: 20px;">

<h1> javascript program print prime number between 1 to 89 </h1>

<button onclick = "primenum()"> prime </button>

<script>

function primenum()

var i=1,j=1, ct=0;

while(i<=89)

j=1;

ct=0;

while(j<=i)

if(i%j==0)

ct++;

j++;

if(ct==2)

document.write(i);

document.write("<br>");

i++;

}
</script>

</body>

</html>

Even odd number

<!doctype html>

<html>

<body style = "text-align: center; font-size: 20px;">

<h1> javascript program print even and odd number between 1 to 50 </h1>

<button onclick = "evenodd()"> Check </button>

<script>

function evenodd()

var num=1;

while(num<=50)

if (num % 2 == 0)

document.write("Even numbers are below");

document.write(num);

document.writ("<br>");

else

document.write("Odd numbers are below");

document.write(num);

document.writ("<br>");
}

num++;

</script>

</body>

</html>

Reverse number

<html>

<head>

<script>

function reverse()

var flag,number,remainder,addition = 0;

number = Number(document.getElementById("N").value);

flag = number;

while(number > 0)

remainder = number%10;

addition = addition * 10 + remainder;

number = parseInt(number/10);

window.alert("-the reverse number is" + addition);

}
</script>

</head>

<body>

<br>

<h1>write reverse number</h1>

Enter The Number :<input type="text" name="n" id = "N"/>

<hr color="red">

<br>

<center><button onClick="reverse()">CHECK</button>

</body>

</html>

Sum of digits

<html>

<head>

<script>

function sum()

var number,remainder=0;

number = Number(document.getElementById("N").value);

while(number > 0)

remainder += number%10;

number = parseInt(number/10);

window.alert("-the sum of number is" + remainder);


}

</script>

</head>

<body>

<br>

<h1>Sum of Dogits</h1>

Enter The Number :<input type="text" name="n" id = "N"/>

<hr color="red">

<br>

<center><button onClick="sum()">CHECK</button>

</body>

</html>

Sumof first and last digits

<html>

<head>

<script>

function sum()

var number,sum=0;

number = Number(document.getElementById("N").value);

sum = number%10;

while(number>9)

number =(number/10);

sum=sum+number;
window.alert("-the sum of number is" + sum);

</script>

</head>

<body>

<br>

<h1>Sum of Digits</h1>

Enter The Number :<input type="text" name="n" id = "N"/>

<hr color="red">

<br>

<center><button onClick="sum()">CHECK</button>

</body>

</html>

Fibonacci series

//To find the Fibonacci series of a given number

<html>

<head>

<title>JavaScript program to find the Fibonacci series of a given number</title>

</head>

<body>

<table>

<tr>

<td> <input type="text" name="a" id="first" placeholder="Enter a number"> </td>

</tr>

<tr>

<td> <button onclick="fibonacci ()" >Submit</button> </td>


</tr>

</table>

</body>

<script type="text/javascript">

function fibonacci()

var n,i,c;

n = document.getElementById ("first").value;

var a = 0;

var b = 1;

document.write("Fibonacci series of "+n+" is :</br>");

document.write(a + " "+ b);

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

c = a +b;

a = b;

b = c;

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

</script>

</html>

Leap year or not

<html>

<head>

<script>
function check_leapyear(){

//define variables

var year;

//get the entered year from text box

year = document.getElementById("year").value;

//three conditions to find out the leap year

if( (0 == year % 4) && (0 != year % 100) || (0 == year % 400) )

alert(year+" is a leap year");

else

alert(year+" is not a leap year");

</script>

</head>

<body>

<h3>Javascript Program to find leap year</h3>

<input type="text" id="year"></input>

<button onclick="check_leapyear()">Check</button>

</body>

</html>

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