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

week-8

The document contains multiple JavaScript programs demonstrating the use of conditional statements, loops, and object manipulation. It includes programs to find the largest of three numbers, display weekdays using switch cases, print numbers 1 to 10 using different loops, print object data using various loop types, check for Armstrong numbers, and display bank denominations. Each program is accompanied by source code and confirms successful execution.

Uploaded by

koushi8485
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)
6 views

week-8

The document contains multiple JavaScript programs demonstrating the use of conditional statements, loops, and object manipulation. It includes programs to find the largest of three numbers, display weekdays using switch cases, print numbers 1 to 10 using different loops, print object data using various loop types, check for Armstrong numbers, and display bank denominations. Each program is accompanied by source code and confirms successful execution.

Uploaded by

koushi8485
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/ 11

8.

JavaScript Conditional Statements and Loops

a. Write a program which asks the user to enter three integers, obtains the numbers from the user
and outputs HTML text that displays the larger number followed by the words “LARGER NUMBER”
in an information message dialog. If the numbers are equal, output HTML text as “EQUAL
NUMBERS”.

Aim: To Write a program which asks the user to enter three integers, obtains the numbers from the
user and outputs HTML text that displays the larger number followed by the words “LARGER
NUMBER” in an information message dialog. If the numbers are equal, output HTML text as “EQUAL
NUMBERS”.

Source code:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Largest Number Finder</title>

<script>

function findLargestNumber() {

let num1 = parseInt(prompt("Enter the first integer:"));

let num2 = parseInt(prompt("Enter the second integer:"));

let num3 = parseInt(prompt("Enter the third integer:"));

if (num1 === num2 && num2 === num3) {

alert("EQUAL NUMBERS");

} else {

let largest = Math.max(num1, num2, num3);

alert(largest + " LARGER NUMBER");

</script>

</head>

<body>
<h1>JavaScript Conditional Statements and Loops</h1>

<button onclick="findLargestNumber()">Click to Enter Numbers</button>

</body>

</html>

OUTPUT:
RESULT:

The above program which asks the user to enter three integers, obtains the numbers from the user
and outputs HTML text that displays the larger number followed by the words “LARGER NUMBER” in
an information message dialog. If the numbers are equal, output HTML text as “EQUAL NUMBERS” is
successfully executed.

b. Write a program to display week days using switch case

AIM:To Write a program to display week days using switch case

SOURCE CODE

// Program to display week days using switch case in JavaScript

function getWeekDay(dayNumber) {

switch (dayNumber) {

case 1:

console.log("Sunday");

break;

case 2:

console.log("Monday");

break;

case 3:

console.log("Tuesday");

break;

case 4:

console.log("Wednesday");

break;

case 5:

console.log("Thursday");

break;
case 6:

console.log("Friday");

break;

case 7:

console.log("Saturday");

break;

default:

console.log("Invalid day number. Please enter a number between 1 and 7.");

// Example usage

console.log("Displaying week days using switch case:");

for (let i = 1; i <= 7; i++) {

getWeekDay(i);

OUTPUT

Displaying week days using switch case:

Sunday

Monday

Tuesday

Wednesday

Thursday

Friday

Saturday

Result: The above program to display week days using switch case was successfully Executed.
c. Write a program to print 1 to 10 numbers using for, while and do-while loops

AIM:To Write a program to print 1 to 10 numbers using for, while and do-while loops

Source code:

// Using for loop

console.log("Using for loop:");

for (let i = 1; i <= 10; i++) {

console.log(i);

// Using while loop

console.log("\nUsing while loop:");

let j = 1;

while (j <= 10) {

console.log(j);

j++;

// Using do-while loop

console.log("\nUsing do-while loop:");

let k = 1;

do {

console.log(k);

k++;

} while (k <= 10);


OUTPUT

Using for loop:

10

Using while loop:

10

Using do-while loop:

5
6

10

RESULT:

The above program to print 1 to 10 numbers using for, while and do-while loops is successfully
executed.

d. Write a program to print data in object using for-in, for-each and for-of loops

AIM: To Write a program to print data in object using for-in, for-each and for-of loops

Source code:

const person = {

name: "John",

age: 30,

city: "New York"

};

console.log("Using for-in loop:");

for (let key in person) {

console.log(`${key}: ${person[key]}`);

console.log("\nUsing forEach loop:");

Object.entries(person).forEach(([key, value]) => {

console.log(`${key}: ${value}`);

});

console.log("\nUsing for-of loop:");


for (let [key, value] of Object.entries(person)) {

console.log(`${key}: ${value}`);

Output:

Using for-in loop:

name: John

age: 30

city: New York

Using forEach loop:

name: John

age: 30

city: New York

Using for-of loop:

name: John

age: 30

city: New York

RESULT: The above program to print data in object using for-in, for-each and for-of loops was
successfully executed.

e. Develop a program to determine whether a given number is an ‘ARMSTRONG NUMBER’ or not.


[Eg: 153 is an Armstrong number, since sum of the cube of the digits is equal to the number i.e.,13
+ 53+ 33 = 153]

AIM:To Develop a program to determine whether a given number is an ‘ARMSTRONG NUMBER’ or


not.

SOURCE CODE:

function isArmstrongNumber(num) {

let sum = 0;

let temp = num;

const digits = num.toString().length;

while (temp > 0) {


let digit = temp % 10;

sum += Math.pow(digit, digits);

temp = Math.floor(temp / 10);

return sum === num;

// Example usage

const number = 153;

if (isArmstrongNumber(number)) {

console.log(`${number} is an Armstrong number.`);

} else {

console.log(`${number} is not an Armstrong number.`);

OUTPUT:

153 is an Armstrong number.

RESULT: The above program to determine whether a given number is an ‘ARMSTRONG NUMBER’ or
not was successfully executed.

F. Write a program to display the denomination of the amount deposited in the bank in terms of
100’s, 50’s, 20’s, 10’s, 5’s, 2’s & 1’s. (Eg: If deposited amount is Rs.163, the output should be 1-
100’s, 1-50’s, 1- 10’s, 1-2’s & 1-1’s)

AIM:To Write a program to display the denomination of the amount deposited in the bank in terms
of 100’s, 50’s, 20’s, 10’s, 5’s, 2’s & 1’s.

SOURCE CODE:

function getDenominations(amount) {

const denominations = [100, 50, 20, 10, 5, 2, 1];

const result = {};

for (let denom of denominations) {

if (amount >= denom) {

result[denom] = Math.floor(amount / denom);


amount %= denom;

return result;

// Example usage

const amount = 163;

const denominationResult = getDenominations(amount);

console.log(`Denominations for Rs.${amount}:`);

for (let [denom, count] of Object.entries(denominationResult)) {

console.log(`${count} - Rs.${denom}`);

OUTPUT:

Denominations for Rs.163:

1 - Rs.1

1 - Rs.2

1 - Rs.10

1 - Rs.50

1 - Rs.100

RESULT: The above program to display the denomination of the amount deposited in the bank in
terms of 100’s, 50’s, 20’s, 10’s, 5’s, 2’s & 1’s was successfully executed.

You might also like