C language programming Chapter 2 example source code

Source: Internet
Author: User

[Example 2-1]
# Include <stdio. h>
Int main (void)
{
Printf ("Hello world! \ N ");
Return 0;
}
[Example 2-2]
# Include <stdio. h>
Int main (void)
{
Printf ("programming is fun. \ n ");
Printf ("and programming in C is even more fun! \ N ");
Return 0;
}
[Example 2-3]
# Include <stdio. h>
Int main (void)
{
Int Celsius, Fahr;
Fahr = 100;
Celsius = 5 * (fahr-32)/9;
Printf ("Fahr = % d, Celsius = % d \ n", Fahr, Celsius );
Return 0;
}
[Example 2-4]
# Include <stdio. h>
Int main (void)
{
Double X, Y;
Printf ("Enter x (x> = 0): \ n ");
Scanf ("% lf", & X );
If (x <= 15)
{
Y = 4 * X/3;
}
Else
{
Y = 2.5 * x-10.5;
}
Printf ("Y = f (% F) = %. 2f \ n", x, y );
Return 0;
}
[Example 2-5]
# Include <stdio. h>
# Include <math. h>
Int main (void)
{
Int money, year;
Double rate, sum;
Printf ("Enter money :");
Scanf ("% d", & money );
Printf ("Enter year :");
Scanf ("% d", & year );
Printf ("Enter rate :");
Scanf ("% lf", & rate );
Sum = money * POW (1 rate), year );
Printf ("sum = %. 2f \ n", sum );
Return 0;
}
[Example 2-6]
# Include <stdio. h>
Int main (void)
{
Int Fahr, lower, upper;
Double Celsius;
Printf ("Enter lower :");
Scanf ("% d", & Lower );
Printf ("enter upper :");
Scanf ("% d", & upper );
Printf ("Fahr Celsius \ n ");
For (Fahr = lower; Fahr <= upper; Fahr)
{
Celsius = (5.0/9.0) * (fahr-32 );
Printf ("% d % 6.1f \ n", Fahr, Celsius );
}
Return 0;
}
[Example 2-7]
# Include <stdio. h>
Int main (void)
{
Int I, n, sum;
Printf ("Enter N :");
Scanf ("% d", & N );
Sum = 0;
For (I = 1; I <= N; I)
{
Sum = sum I;
}
Printf ("sum of numbers from 1 to % d is % d \ n", N, sum );
Return 0;
}
[Example 2-8]
# Include <stdio. h>
Int main (void)
{
Int denominator, flag, I, N;
Double item, sum;
Printf ("Enter N :");
Scanf ("% d", & N );
Flag = 1;
Denominator = 1;
Sum = 0;
For (I = 1; I <= N; I)
{
Item = flag * 1.0/denominator;
Sum = sum item;
Flag =-flag;
Denominator = Denominator 2;
}
Printf ("sum = % F \ n", sum );
Return 0;
}
[Example 2-9]
# Include <stdio. h>
Int main (void)
{
Int I, N;
Double product;
Printf ("Enter N :");
Scanf ("% d", & N );
Product = 1;
For (I = 1; I <= N; I)
{
Product = product * I;
}
Printf ("Product = %. 0f \ n", product );
Return 0;
}
[Example 2-10-1]
# Include <stdio. h>
# Include <math. h>
Int main (void)
{
Int I, N;
Double power;
Printf ("Enter N :");
Scanf ("% d", & N );
For (I = 0; I <= N; I)
{
Power = POW (2, I );
Printf ("Pow (2, % d) = %. 0f \ n", I, power );
}
Return 0;
}
[Example 2-10-2]
# Include <stdio. h>
Double fact (int n );
Int main (void)
{
Int I, N;
Double result;
Printf ("Enter N :");
Scanf ("% d", & N );
For (I = 0; I <= N; I)
{
Result = fact (I );
Printf ("% d! = %. 0f \ n ", I, result );
}
Return 0;
}
Double fact (int n)
{
Int I;
Double product;
Product = 1;
For (I = 1; I <= N; I)
{
Product = product * I;
}
Return product;
}

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.