-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop.html
More file actions
103 lines (92 loc) · 2.34 KB
/
Copy pathloop.html
File metadata and controls
103 lines (92 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- <input id="x" type="number"> -->
<button onclick="myFunction()">Try it</button>
<button onclick="factorial()">factorial</button>
<button onclick="Fibonacci()">Fibonacci</button>
<p id="demo"></p>
<script>
// 1- Write a program that prints numbers from 1 to 10 using a for loop.
for (let a=1;a<=10;a++){
console.log(a)
}
// 2-Write a program that prints the even numbers from 1 to 10 using a for loop.
// prefer start 1 use if !(a%2)
for(a=2;a<11;a+=2){
console.log(a);
}
// 3- Write a program that prints the odd numbers from 1 to 10 using a while loop.
// for(a=1;a<10;a+=2){
// console.log(a);
// }
a=1;
while(a<11){
if(a%2){
console.log(a);
}
++a;
}
// 4- Write a program that prints the multiplication table of a number entered by the user using a for loop.
function myFunction() {
let x = prompt("Please enter your number", "your number");
if (x != NaN) {
for (let a=1;a<=10;a++){
console.log(a *x)
}
// document.getElementById("demo").innerHTML =a*x;
}
}
// 5- Write a program that calculates the sum of numbers from 1 to 100 using a while loop.
let b=0,i=0;
while (i++ < 100) {
b += i;
}
console.log(b)
// 6- Write a program that calculates the factorial of a number entered by the user using a for loop.
function factorial() {
let y = prompt("Please enter your number", "your number");
let f=1
for (y;y>0;--y){
f*=y
}
console.log(f)}
// 7- Write a program that prints the Fibonacci series up to a certain number entered by the user using a while loop.
function Fibonacci() {
let z = prompt("Please enter your number", "your number");
let b=1,d=0,c=a+b;
// for (i=1;i<z;i+=2){
// console.log(d)
// console.log(b)
// d+=b
// b+=d
// }
// while((b+=d)<z){
// console.log(d)
// // b+=d
// console.log(b)
// d+=b
// }
// do{
// (b+=d)
// console.log(d)
// console.log(b)
// d+=b
// }while((b+=d)<=z)
}
// 8- Write a program that prints the reverse of the following numbers:
//5 , 10 , 15 , 20
// using a while loop.
let num=[5 , 10 , 15 , 20]
i=num.length
while (--i>=0) {
console.log(num[i])
}
</script>
</body>
</html>