0% found this document useful (0 votes)
215 views10 pages

Program For Javascript

The document contains 11 code snippets showing different JavaScript programs that demonstrate basic programming concepts like arithmetic operators, conditional statements, loops, functions etc. The programs include calculating addition, subtraction etc of two numbers, checking if a student passed or failed an exam based on marks, determining grade based on percentage, checking if an alphabet is a vowel, calculating factorial of a number, printing Fibonacci series, finding greatest among three numbers, calculating sum of numbers, checking if a number is even or odd, printing table of a number and printing number patterns using loops.

Uploaded by

Revati Menghani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
215 views10 pages

Program For Javascript

The document contains 11 code snippets showing different JavaScript programs that demonstrate basic programming concepts like arithmetic operators, conditional statements, loops, functions etc. The programs include calculating addition, subtraction etc of two numbers, checking if a student passed or failed an exam based on marks, determining grade based on percentage, checking if an alphabet is a vowel, calculating factorial of a number, printing Fibonacci series, finding greatest among three numbers, calculating sum of numbers, checking if a number is even or odd, printing table of a number and printing number patterns using loops.

Uploaded by

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

Programm

1) Write a program to show the use of arithmetic operators in JavaScript.


<html>
<head>
<script type="text/javascript">
var a= parseInt(prompt("enter the number for a"));
var b= parseInt(prompt("enter the number for b"));

var add= a+b;


document.write( "The addition is" +add );
document.write("</br>");

var sub=a-b;
document.write( "The Subtraction is" +sub );
document.write("</br>");

var mul= a*b;


document.write( "Themultiplication is" +mul );
document.write("</br>");

var div= a/b;


document.write( "The Quotient is" +div );
document.write("</br>");

var rem= a%b;


document.write( "The remainder is" +rem );
document.write("</br>");

</script></head></html>
2) Determine if a student has passed in his exam or not. if the marks are
greater than 40 then he has a passed else he has a failed.

<html>

<head>

<Title> Result </title>

<script type=”text/javscript”>

Var a;

a = parseInt(Prompt(“Enter the marks “));

if(a>40)

alert(“student has passed in exam”);

else

alert(“student has Failed in exam”);

</script></head></html>
3) Determine what grade he has obtained based on the marks obtained if 90-100 is a+; 80-90
is a ; 70-80 is b+; 60-70 is b ; 50-60 is c; 40-50 is d and below 40 is fail [hint : use the if-else
if..-else
<html>

<head>

<script type="text/javascript">

var a= prompt("enter the percentage :");

if(a>=90 && a<=100)

document.write("You got A+ grade");

else if(a>=80 && a<=90)

document.write("You got A grade");

else if(a>=70 && a<=80)

document.write("You got B+ grade");

else if(a>=60 && a<=70)

document.write("You got B grade");

else if(a>=50 && a<=60)

document.write("You got C grade");

else if(a>=40 && a<=50)

document.write("You got D grade");

else

document.write("You Failed");

</script></head></html>
4) Write a aprogram to check the given alphabet is vowel or not using switch statement
<html>

<head>

<script type="text/javascript">

var alpha= prompt("Enter vowel:");

switch(alpha)

case 'a':

document.write("Its a Vowel");

break;

case 'e':

document.write("Its a Vowel");

break;

case 'i':

document.write(" Its a Vowel");

break;

case 'o':

document.write("Its a Vowel");

break;

case 'u':

document.write("Its a Vowel");

break;

case 'A':

document.write("Its a Vowel");

break;

case 'E':

document.write("Its a Vowel");

break;

case 'O':

document.write("Its a Vowel");
break;

case 'I':

document.write("Its a Vowel");

break;

case 'U':

document.write("Its a Vowel");

break;

default:

document.write("Consonant");

</script>

</head>

</html>

5) Write a JavaScript program to calculate the factorial of a number.


in mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all
positive integers less than or equal to n. For example, 5! = 5 x 4 x 3 x 2 x 1 = 120

<html>

<head>

<script type="text/javascript">

var a;

var fact=1;

for(a=5;a>=1;a--)

fact= fact*a;

document.write( "The factorial " +fact +"</br>");

</script>

</head>

</html>
6) Writea JavaScript program to get the first n Fibonacci numbers.
note : the Fibonacci sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . each
subsequent number is the sum of the previous two numbers.

<html>

<head>

<script type="text/javascript">

var a=0;

var b=1;

var c;

var i;

var number ;

var number =eval( prompt("Enter the number"));

document.write(+ a + "," +b) //printing 0 and 1

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

c=a+b;

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

a=b;

b=c;

</script>

</head>

</html>
Write a JavaScript program to design the following pattern, using a nested for
loop.
*
**
7
** *
** * *
** * * *

<html>

<head>

<body>

<p>Printing a triangle</p>

<script type="text/javascript">

var i, j;

for(i=1; i <=5; i++) //outer loop

for(j=1; j<=i; j++) //inner loop

document.write(i);

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

</script>

</body>

</html>
8) Write a program to check the greatest number among 3 number

<html>

<head>

<script type="text/javascript">

var a= prompt("Enter the number for a:");

var b= prompt("Enter the number b:");

var c= prompt("Enter the number c:");

if(a>b && a>c)

document.write("A is greater number ");

else

if(b>a && b>c)

document.write("B is a greater number");

else

document.write("C is a greater number");

</script>

</head>

</html>
Q9 Write a program to find the sum of given number for e. g (given number =5 i.e. 1+2+3+4+5 =15)

<html>

<head>

<script type="text/javascript">

var a;

var sum=0;

for(a=1;a<=5;a++)

sum= sum+a;

document.write( "The sum is: " +sum +"</br>");

</script>

</head></html>

Q9 ) Check whether the given number is odd or even ?

<html>

<head>

<script type="text/javascript">

var a= prompt("Enter the number");

if(a%2==0)

document.write("Its an even number");

else

document.write("Its an odd number");

</script>

</head></html>
Q10) write a program to print table of given number

<html>

<head>

<script type="text/javascript">

var a;

var table;

var p=prompt("Enter the number for table :");

document.write( "The table of " +p +" is:" +"</br>");

for(a=1;a<=10;a++)

table=p*a;

document.write( +p + "*" +a + "= " +table +"</br>" );

</script>

</head>

</html>

Q11)
Write a JavaScript program to design the following pattern, using a nested for loop.
1
12
123
1234
12345

<html>

<head>

<body>

<p>Printing number triangle</p>

<script type="text/javascript">

var i, j;

for(i=1; i <=5; i++) //outer loop

for(j=1; j<=i; j++) //inner loop

document.write(j);

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

</script>

</body>

</html>

You might also like