[go: up one dir, main page]

0% found this document useful (0 votes)
5 views6 pages

Reverse of a Number

The document contains multiple HTML scripts demonstrating various programming concepts, including reversing a number, checking for palindromes, calculating the sum of digits, finding factorials, checking for prime numbers, generating Fibonacci series, and verifying Armstrong numbers. Additionally, it includes a form for validating user input such as name, email, and mobile number with associated JavaScript functions for input validation. Each section is encapsulated within its own HTML structure and utilizes JavaScript for functionality.

Uploaded by

sandhyadevit
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)
5 views6 pages

Reverse of a Number

The document contains multiple HTML scripts demonstrating various programming concepts, including reversing a number, checking for palindromes, calculating the sum of digits, finding factorials, checking for prime numbers, generating Fibonacci series, and verifying Armstrong numbers. Additionally, it includes a form for validating user input such as name, email, and mobile number with associated JavaScript functions for input validation. Each section is encapsulated within its own HTML structure and utilizes JavaScript for functionality.

Uploaded by

sandhyadevit
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/ 6

1 Reverse of a number

<html>
<head>
<title>String Problem</title>
<script>
function reverseNum(num)
{
var reverse = 0;
while(num != 0)
{
reverse = reverse * 10;
reverse = reverse + num%10;
num = Math.trunc(num/10);
}
return reverse;
}
document.write(reverseNum(162));
</script>
</head>
<body>
</body>
</html>

2 number Palindrome

<html>
<head>
<title>String Problem</title>
<script>
function palin(number)
{
num=number;
var reverse = 0;
while(num != 0)
{
reverse = reverse * 10;
reverse = reverse + num%10;
num = Math.trunc(num/10);
}
return number===reverse;
}
document.write(palin(262));
</script>
</head>
<body>

</body>
</html>

3 Sum of Digits

<html>
<head>
<title>String Problem</title>
<script>
function digitSum(num) {
var sum=0;
while(num!=0){
sum += num % 10;
num = Math.floor(num/10);
}
return sum;
}
document.write(digitSum(512));

</script>
</head>
<body>
</body>
</html>

4 Factorial using recursive function

<html>
<head>
<title>String Problem</title>
<script>
function fact(num) {
if(num==1){
return 1; // Termination condition
}
else
return num*fact(num-1);
}
document.write(fact(6));
</script>
</head>
<body>
</body>
</html>

5 Prime number or not

<html>
<head>
<title>Find the area and circumference of a circle</title>
</head>
<body>
<script>
function checkPrime(){
number=document.getElementById("num").value;

var isPrime = true;


if (number === 1) {
document.write("1 is neither prime nor composite number.");
}
else if (number > 1) {

for (var i = 2; i <= number/2; i++)


{
if (number % i == 0)
{
isPrime = false;
break;
}
}

if (isPrime) {
document.write(' prime number');
} else {
document.write('not prime number');
}
}
}
</script>
<form name=form1>
Enter a number
<input type="text" id="num" size=10><br>
<input type="button" value="Check"
onClick='checkPrime();'>
</form>
</script>
</body>
</html>

6 Fibonacci Series upto nth term

<html>
<head>
<title>String Problem</title>
<script>
function fibonacci(n) {
var n1=0;
var n2=1;
var count=2;
var n3;
document.write(n1,n2);
while(count<n){
n3=n1+n2;
document.write(n3); //print current element
n1=n2;
n2=n3;
count++;
}}

document.write(fibonacci(6));

</script>
</head>
<body>
</body>
</html>

7 Armstrong number

<html>
<head>
<title>String Problem</title>
<script>
function armstrong(number)
{
var sum = 0;
var temp = number;
alert(number);
while (temp > 0)
{
var remainder = temp % 10;
sum += remainder * remainder * remainder;
temp = parseInt(temp / 10);
}

return sum === number;


}
document.write(armstrong(153));
</script>
</head>
<body>
<input type="submit" value="submit" onclick="reverse()">
</body>
</html>
Validation of form

<html>
<head>
<title>Resume update Web Page</title>
</head>
<body '>
<div align="center">
<form method="post" action="#" >
<table width="90%" border=1 cellpadding="10px" >
<tr>
<td width="40%"><div align="right">Name of the
Candidate </div></td>
<td width='33%'><input name="cname" type="text" onkeypress="return onlyAlphabets(event,this);"
required></td>
</tr>
<td><div align="right">Email Address </div></td>
<td><input name="email" type="email" onblur="validateEmail(this);" required></td>
</tr>
<tr>
<td><div align="right">Mobile No. </div></td>
<td class="inputtext"> <input name="mobile" type="text" onkeypress="return isNumber(event)"
required></td>
</tr>

</table>
<p>
<input name="Submit" type="submit" class="inputtext" value="Submit">
<input name="Submit2" type="reset"
class="inputtext" value="Reset">
</p>
</form>
<script type='text/javascript'>
function isNumber(evt) {
evt = (evt) ? evt :window.event;
var charCode = (evt.which) ? evt.which :evt.keyCode;
if (charCode> 31 && (charCode< 48 || charCode>57))
{
return false;
}
return true;
}
</script>
<script type='text/javascript'>
function onlyAlphabets(e, t) {
try {
if (window.event) {
var charCode = window.event.keyCode;
}
else if (e) {
var charCode = e.which;
}
else { return true; }
if ((charCode> 64 &&charCode< 91) || (charCode>96 &&charCode< 123))
return true;
else
return false;
}
catch (err) {
alert(err.Description);
}}
</script>
<script type='text/javascript'>
function validateEmail(emailField){
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if (reg.test(emailField.value) == false)
{
alert('Invalid Email Address');
return false;
}
return true;
}
</script>
</body>
</html>

You might also like