1.1到100的和
var sum = 0 ;
for(var i = 1; i<= 100; i++){
sum = sum + i;
}
console.log("从1加到100的和:" + sum);
2.6的阶乘
var result = 1;
for(var i = 1; i <= 6 ; i++){
result = result * i;
}
console.log("6的阶乘为:" + result);
3.水鲜花数
var a , b , c;//a为百位,b为十位,c为个位
for(var i = 100; i < 1000; i++){
a = parseInt(i/100);
b = parseInt((i%100)/10);
c = parseInt(i%10);
if (i == a * a * a + b * b * b + c * c * c ) {
console.log("水鲜花数为:" + i);
}
}
4 .取出四位数的每一位数
var num = 4396;
var a = parseInt(num/1000);(取出千位)
var b = parseInt((num%1000)/100);(取出百位)
var c = parseInt(((num&1000)%100)/10;(取出十位)
var d = num%10;(取出个位)
console.log(a,b,c,d);
5.99乘法表
var z = "";
for(var x = 1; x <= 9; x++ ){
for(var y = 1; y <= x ; y++){
z += x + "*" + y + "="+ x * y + " ";
}
z+="
";
}
console.log(z);
6.计算1990到2100之间的闰年
console.log("闰年:")
var result6 = "";
var flag = 0;
for (var year = 1900; year <=2100; year++){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
result6 += year +" ";
flag++;
}
if (flag == 5) {
result6 += "
";
flag = 0;
}
}
console.log(result6);
7.1000以内能被5和6同时整除的数
var flag = 0 ;
for(var i = 1 ; i <= 1000; i++){
if(i % 5 == 0 && i % 6 == 0 ){
console.log(i);
}
}
8.1到100之间的素数
var flag;
for(var i = 2; i <100; i++){
flag = true;
for(var j = 2; j<i; j++){
if(i % j == 0){
flag = false;
}
}
if(flag){
console.log(i);
}
9.交换两个数
第一种方法
var b = 123 , c = 234 ;
var d;
d = b;
b = c;
c = d;
console.log(b);
console.log(c);
第二种方法
var m = 456 , n = 789;
var m = m + n;
n = m - n;
m = m - n;
console.log(m,n);