C # -- 2nd weekly lab -- Task 4 -- compile a console application -- use the for, while, and do-while statements to calculate n respectively!

Source: Internet
Author: User

/* (Start of program header annotation)
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: compile a console application. Use the for, while, and do-while statements to calculate n!
* Author: Lei hengxin
* Completion date: January 1, September 08, 2012
* Version No.: V1.0
* Description of tasks and Solutions
* Input description:
* Problem description:
* Program output:
* End the comment in the program Header
*/

[Csharp]
<Span style = "font-size: 24px;"> using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
 
Namespace ConsoleApplication_do_while
{
Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("this is a loop control statement that uses for while do-while to calculate n! ");
Console. Write ("Enter the number :");
String n = Console. ReadLine ();
Int I, m = 1;
Int x = int. Parse (n); // type conversion
For (I = 1; I <x; ++ I)
{
M = m * (I + 1 );
}
Console. WriteLine ("Use the for loop control statement to calculate {0 }! Value: {1} ", x, m );
 
I = 1;
M = 1;
While (I <x)
{
M = m * (I + 1 );
 
++ I;
 
}
Console. WriteLine ("Use the while loop control statement to calculate {0 }! Value: {1} ", x, m );
 
I = 1;
M = 1;
-- X;
Do
{
M = m * (I + 1 );
 
++ I;
} While (I <= x );
Console. WriteLine ("Use the do-while loop control statement to calculate {0 }! Value: {1} ", (x + 1), m );
 
Console. ReadKey ();
}
}
}
</Span>

 
Running result:

 

 

 
 
Experience Accumulation:
1. The for while do-while statements in C # And C ++ are identical.

Related Article

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.