Repository navigation
Expand file tree
/
Copy pathForLoop.js
More file actions
111 lines (74 loc) · 2.67 KB
/
Copy pathForLoop.js
File metadata and controls
111 lines (74 loc) · 2.67 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
104
105
// Accept an integer and Print hello world n times.
let n = Number(prompt("enter a number to check natural number"));
if (isNaN(n)) {
console.log("Inavlid !!");
}
else {
let i;
for (i = 0; i <= n; i++) {
console.log(i);
}
console.log(i);
}
// ----------------------------------------------------------------------------------------------
// Print natural number up to n.
let n = Number(prompt("enter a number"));
for (let i = n; i >=1; i--) {
console.log(i);
}
// -----------------------------------------------------------------------------------------------
// Take a number as input and print its table
5 * 1 = 5
5 * 2 = 10 ... up to 10 terms
let n = Number(prompt("enter a num to print the table "))
for (let i = 0; i <= 10; i++) {
console.log(`${n} * ${i} = ${n*i}`);
}
// -------------------------------------------------------------------------------------------------
// sum upto nth terms matlb if n= 5 1+2+3+4+5=15
let n = Number(prompt("enter the num "))
let sum = 0 ;
for (let i = 1; i <= n; i++) {
sum += i;
}
console.log(sum );
// ----------------------------------------------------------------------------------------------------
// Factorial of a number
let n = Number(prompt("enter a num"));
let fact = 1;
for (let i = 1; i <= n; i++) {
fact = fact * i;
}
console.log(`factorial of ${n} is ${fact}`);
// ---------------------------------------------------------------------------------------------------------
//Print the sum of all even & odd numbers in a range seperately.
let n = Number(prompt("enter a num to check wheather it is even or odd"))
let evenSum = 0 , oddSum = 0 ;
for (let i = 1; i <=n ; i++) {
if(i % 2 == 0) evenSum = evenSum + i;
else oddSum = oddSum + i;
}
console.log("evenSum is "+evenSum);
console.log("oddSum is "+oddSum);
//----------------------------------------------------------------------------------------------------------
// Print all the factors of a number.
let n = Number(prompt("enter a num "))
for (let i = 1; i <= n/2 ; i++) {
if(n % i == 0) console.log(i);
}
console.log(n);
// ----------------------------------------------------------------------------------------------------------
// Check if the number is Prime or not.
let n = Number(prompt("enter a num "))
let isPrime = true;
for (let i = 2; i <= n/2; i++) {
if(n % i== 0)
{
isPrime = false;
break;
}
}
if(isPrime) console.log("its a prime number");
else console.log("not a prime number");
//-----------------------------------------------------------------------------------------------------------
//