Several small exercises about the for loop, such as odd and even numbers, factorial, summation, and odd factorial

Source: Internet
Author: User

Several small exercises about the for loop, such as odd and even numbers, factorial, summation, and odd factorial

1. odd and even numbers less than 100

Var js = "";

Var OS = "";
For (var I = 1; I <101; I ++)
{
If (I % 2 = 0)
{
OS = OS + "" + I;
}
Else
{
Js = js + "" + I;
}
}
Alert (OS );
Alert (js );

2. Take 7-related numbers of less than 100.

Var x = "";

For (var I = 0; I <101; I ++)
{
If (I % 7 = 0 | I % 10 = 7 | parseInt (I/10) = 7)
{
X = x + "" + I;
}
}
Alert (x );

3. Calculate the sum of numbers less than 100

Var sum = 0;

For (var I = 0; I <101; I ++)
{
Sum + = I;
}
Alert (sum );

4. Calculate the factorial of less than 10.

Var str = "";

For (var I = 1; I <11; I ++)
{
For (var j = I; j <= 9 ;)
{
Str = str + "" + I + "*" + j
}
}
Alert (s );

5. The height of basketball falls from a 1-meter-high place. The height of each bounce is 40%. After several rounds, the height of basketball is 0.1.

Var h = 1;

Var n =;
While (true)
{
   
H = h * 0.4;

If (h <0.1) {break}
N ++
      
}
Alert (n );

6. There is a checkerboard with 64 squares. Put one grain of rice in the first place, two grains of rice in the second place, four grains of rice in the third place, and eight grains of rice in the fourth place.

Var sum = 0;

For (var n = 1; I <65; n ++)
{
Var ds = 1;
For (var I = 1, I <= n-1; I ++ ){

Ds = ds * 2

}

Sum = sum + ds

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.