6, C # Basic finishing (For statement classic exercise--for loop nesting, exhaustive)

Source: Internet
Author: User

1, for loop nesting----The most basic problem: to find factorial and

 intsum =0; intn =int. Parse (Console.ReadLine ());  for(inti =0; I < n; i++) {      intSUM1 =1;//define the variable sum1, each time the loop is given its initial value of 1, to find the factorial dedicated       for(intj =1; J <= i+1; J + +) {sum1= Sum1 *J; } Sum= sum + sum1;//Add and add each factorial} Console.WriteLine (sum);

2. For the poor lifting of the loop

Example: 100 yuan buy 2 yuan of pencils, 5 yuan pencil case, 10 Yuan folder, 15 yuan of color pens, just spend light, each kind of goods must have one, altogether how many kinds of possibility?

intCount =0; for(intQB =1;qb< -; qb++){      for(inthe =1; He < -; he++)     {             for(intJia =1; Jia <Ten; jia++)            {                  for(intCai =1; Cai <7; cai++ )                 {                      if(QB *2+ He *5+ jia*Ten+cai* the== -) {Count++; Console.WriteLine ("pencil: {0}, pencil case: {1}, folder: {2}, Color pen: {3}", QB, HE,JIA,CAI);    }}}}}console.writeline (count); 

3. Print a diamond with a nested for loop :

1Console.WriteLine ("Please enter the length of the side:");2             intII =int. Parse (Console.ReadLine ());3Console.WriteLine ("the printed diamond is:");4             //Print the triangle above5              for(intg =0; G < II; g++)6             {7                 8                  for(intn = ii-g; n >1; n--)9                 {TenConsole.Write ("  "); One                 } A                  for(intm =1; M <= G; m++) -                 { -Console.Write (" #"); the                 } -                 -                  for(intp =1; P <= G; p++) -                 { +Console.Write (" #"); -                 } +Console.WriteLine (" #"); A             } at             //Print the following triangle -              for(intj =0; J < II-1; J + +) -             { -                  for(intA =1; A <= J +1; a++) -                 { -Console.Write ("  "); in                 } -                  for(intb = ii-j; B >2; b--) to                 { +Console.Write (" #"); -                 } the                  for(intc = ii-j; C >2; c--) *                 { $Console.Write (" #");Panax Notoginseng                 } -Console.WriteLine (" #"); the}

6, C # Basic finishing (For statement classic exercise--for loop nesting, exhaustive)

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.