[go: up one dir, main page]

0% found this document useful (0 votes)
19 views13 pages

Untitled 1

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

Untitled 1

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

1 Write JS

code to
accept the
Principal
Amount, rate
and time
from the
user.
Calculate and display

Simple interest and Compound interest.

SI=PA*R*T

CI=PA*(1+R/100)Time

[Use Math.pow(1+R/100, Time)]

Code-

<script>

pa=parseInt(prompt("Enter pa:"))

r=parseInt(prompt("Enter r:"))

t=parseInt(prompt("Enter t:"))

Simpleinterest=pa*r*t

compoundinterest=Math.pow(1+r/100,t)

document.write("si="+Simpleinterest+"<br>")

document.write("ci="+compoundinterest)

</script>

Output-

2 Write JS code to accept a number and check whether it is even or odd.

Code-

<script>

num=parseInt(prompt("Enter The number:"))

if(num%2==0)
document.write("even")

else

document.write("odd")

</script>

Output-

3 Write JS code to accept a number and check whether it is positive, negative or zero

Code-

<script>

num=parseInt(prompt("Enter The number:"))

if(num>0)

document.write("positive")

else if(num<0)

document.write("negative")

else

document.write("zero")

</script>

Output-

4 Write JS code to accept the total mark of a student and display the grade as per the

following table.

TOTAL GRADE

>=90 A

>=80 B

>=70 C
>=60 D

<60 F

Code-

<script>

TotalMarks=parseInt(prompt("Enter Total Marks:"))

if(TotalMarks>=90)

document.write("A")

else if(TotalMarks>=80)

document.write("B")

else if(TotalMarks>=70)

document.write("C")

else if(TotalMarks>=60)

document.write("D")

else

document.write("F")

</script>

Output-

5 Write JS code to accept the index of the month and display the month name using switch.

Code-

<script>

Month=parseInt(prompt("Enter number from 1 to 12:"))

switch(Month)

case 1:

document.write("january");

break;
case 2:

document.write("February")

break;

case 3:

document.write("March");

break;

case 4:

document.write("April");

break;

case 5:

document.write("May");

break;

case 6:

document.write("June");

break;

case 7:

document.write("july");

break;

case 8:

document.write("august");

break;

case 9:

document.write("september");

break;

case 10:

document.write("october");

break;

case 11:

document.write("november");
break;

case 12:

document.write("december");

break;

default:

document.write("invalid");

break;

</script>

Output-

6Write JS code to display the first 15 natural numbers and their sum.

Code-

<script>

sum=0

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

sum=sum+i

document.write("sum of numbers is"+sum)

</script>

Output-

7accet a number from the user and display its multiplication table on the browser window.

Code-

<script>
num=parseInt(prompt("Enter A Number:"))

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

document.write(num+"x"+i+"="+num*i+"<br>")

</script>

Output-

8reate the following pattern using a nested for loop

##

###

####

Code-

<script>

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

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

document.write("#")
}

document.write("<br>")

</script>

Output-

9 Create the following html form. Write JS code to perform the arithmetic operations on clicking the

corresponding buttons and display answer in the result text field.

Code-

<html>

<script>

function add()

n1=parseInt(document.form1.txt1.value)

n2=parseInt(document.form1.txt2.value)

document.form1.txt3.value=n1+n2

function sub()

n1=parseInt(document.form1.txt1.value)

n2=parseInt(document.form1.txt2.value)

document.form1.txt3.value=n1-n2

function mul()

{
n1=parseInt(document.form1.txt1.value)

n2=parseInt(document.form1.txt2.value)

document.form1.txt3.value=n1*n2

function div()

n1=parseInt(document.form1.txt1.value)

n2=parseInt(document.form1.txt2.value)

document.form1.txt3.value=n1/n2

function mod()

n1=parseInt(document.form1.txt1.value)

n2=parseInt(document.form1.txt2.value)

document.form1.txt3.value=n1%n2

</script>

<body>

<h1> Arithmetic Calculator </h1>

<form name=form1>

Enter First Number<input type=text name=txt1 value=0>

<br> <br>

Enter Second Number<input type=text name=txt2 value=0>

<br><br>

<input type=button name=addition value=+ onclick=add()>

<input type=button name=subtraction value=- onclick=sub()>

<input type=button name=multiplication value=* onclick=mul()>

<input type=button name=division value=/ onclick=div()>

<input type=button name=modulo value=% onclick=mod()>


<br><br>

result<input type=text name=txt3 value=0>

</form>

</body>

</html>

Output-

10) Create the following html form. Write JS code to perform the numeric operations on clicking the

corresponding buttons and display answer in the result text field.

Code-

<html>

<script>

function sumofdigits()

n1=parseInt(document.form1.txt1.value)

sum=0

while(n1>0)

rem=n1%10

sum=sum+rem
n1=parseInt(n1/10)

document.form1.txt3.value=sum

function productofdigits()

n1=parseInt(document.form1.txt1.value)

pro=1

while(n1>0)

rem=n1%10

pro

=pro*rem

n1=parseInt(n1/10)

document.form1.txt3.value=pro

function sumofalternatedigits()

n1=parseInt(document.form1.txt1.value)

sum=0

while(n1>0)

rem=n1%10

sum=sum+rem

n1=parseInt(n1/100)

document.form1.txt3.value=sum
}

function reverseofdigits()

n1=parseInt(document.form1.txt1.value)

rev=0

while(n1>0)

rem=n1%10

rev=rev*10+rem

n1=parseInt(n1/10)

document.form1.txt3.value=rev

function prime()

n1=parseInt(document.form1.txt1.value)

flag=0

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

if(n1%i==0)

flag=1;

break;

if(flag==0)

document.form1.txt3.value="prime"

else

document.form1.txt3.value="composite"
}

</script>

<body>

<h1> Arithmetic Calculator </h1>

<form name=form1>

Enter The Number<input type=text name=txt1>

<br> <br>

<input type=button name=sod value="Sum Of Digits" onclick=sumofdigits()>

<input type=button name=pod value="Product Of Digits" onclick=productofdigits()>

<input type=button name=soad value="Sum Of Alternate Digits" onclick=sumofalternatedigits()>

<input type=button name=rod value="Reverse" onclick=reverseofdigits()>

<input type=button name=primeorcomposite value="Check prime" onclick=prime()>

<br><br>

result<input type=text name=txt3>

</form>

</body>

</html>

Output-

You might also like