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)