Professional Documents
Culture Documents
1. Write a program that reads an integer and determines and prints whether it is odd or even.
[Hint: Use the modulus operator. An even number is a multiple of two. Any multiple of two
leaves a remainder of zero when divided by 2.] (10 marks)
Answer:
<!DOCTYPE html>
<html>
<head>
<title>Even and Odd Numbers</title>
</head>
<body>
<script type="text/javascript">
var x;
var y;
x = window.prompt("Enter the Number Input by the User");
y= parseInt(x);
if (y%2==0)
{
document.write("The number is Even");
}
else
{
document.write("The number is Odd");
}
</script>
</body>
</html>
2. Write a program that inputs three integers from the keyboard and prints the sum, average,
product, smallest and largest of these numbers. The screen dialog should appear as follows: (10
marks)
i. Input three different integers:
ii. Sum is
iii. Average is
iv. Product is
v. Smallest is
vi. Largest is
Answer:
<!DOCTYPE html>
<html>
<head>
<title>3 Integers</title>
</head>
<script type = "text/javascript">
<!-var number1;
var number2;
var number3;
var firstNumber;
var secondNumber;
var thirdNumber;
var largest;
var smallest;
number1 = window.prompt( "Enter the first number",0 );
number2 = window.prompt( "Enter the second number",0 );
number3 = window.prompt( "Enter the third number" ,0);
3. Write a program that reads in two integers and determines and prints if the first is a multiple of
the second. [Hint: Use the modulus operator.] (5 marks)
Answer:
<!DOCTYPE html>
<html>
<head>
<title>Multiple Of a Number</title>
</head>
<body>
<script type="text/javascript">
var a,b,x,y;
a = window.prompt("Enter the 1st Number");
b = window.prompt("Enter the 2nd Number");
x = parseInt(a);
y = parseInt(b);
if (x%y==0)
{
document.write(" x is a Multiple of y");
}
else
{
document.write("x is not a Multiple of y");
}
</script>
</body>
</html>
4. Write a function that reads in the radius of a circle as an integer and prints the circle's diameter,
circumference and area. Use the constant value 3.14159 for (15 marks)
<!DOCTYPE html>
<html>
<head>
<title>Function </title>
<script type="text/javascript">
<!-function circle(r)
{
Area = 3.14159 * r * r;
return Area;
}
</script>
</head>
<body>
<script type="text/javascript">
var diameter,circumference;
diameter = parseInt(r+r);
circumference = (3.14159 * diameter) ;
document.write("The Area is :" + circle(4) + "<br>");
document.write("The Diameter is :" + diameter + "<br>");
document.write("The Circumference is :" + circumference + "<br>");
</script>
</body>
</html>
5. A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this Quiz are
available to you. Using a for loop, write a code that can calculate the average marks of the class.
(10Marks)
<!DOCTYPE html>
<html >
<head>
<title>Class Average Program</title>
<script type = "text/javascript">
<!-var total, // sum of grades
gradeCounter, // number of grades entered
gradeValue, // grade value
average, // average of all grades
grade; // grade typed by user
total = 0; // clear total
for (gradeCounter=1;gradeCounter<=10;gradeCounter++)
{
grade = window.prompt( "Enter integer grade:", "0" );
gradeValue = parseInt( grade );// convert grade from a string to an integer
total = total + gradeValue;// add gradeValue to total
}// Termination Phase