Chapter 2 exercises and exercises in C language programming

Source: Internet
Author: User

[Exercise for 2-1]
# Include <stdio. h>
Int main (void)
{
Printf ("programming in C is even more fun! \ N ");
Return 0;
}
[Exercise for 2-2]
Printf ("programming is fun. And programming in C is even more fun! \ N ");
# Include <stdio. h>
Int main (void)
{
Printf ("programming is fun. And programming in C is even more fun! \ N ");
Return 0;
}
[Exercise 2-3]
****
***
**
*
# Include <stdio. h>
Int main (void)
{
Printf ("***** \ n ");
Printf ("*** \ n ");
Printf ("** \ n ");
Printf ("* \ n ");
Return 0;
}
[Exercise 2-4]
# Include <stdio. h>
Int main (void)
{
Int Celsius, Fahr;
Fahr = 150;
Celsius = 5 * (fahr-32)/9;
Printf ("Fahr = % d, Celsius = % d \ n", Fahr, Celsius );
Return 0;
}
[Exercise 2-5]
No. It is not recognized by the computer. It will affect the division operation between integers.
[Exercise 2-6]
# Include <stdio. h>
# Include <math. h>
Int main (void)
{
Int h;
Float A, T;
H = 100;
A = 2 * H/10;
T = SQRT ();
Printf ("H = % d, t = %. 2f \ n", h, t );
Return 0;
}
[Exercise 2-7]
No.
[Exercise 2-8]
# Include <stdio. h>
Int main (void)
{
Int Celsius, Fahr;
Printf ("Enter Fahr :");
Scanf ("% d", & Fahr );
Celsius = 5 * (fahr-32)/9;
Printf ("Fahr = % d, Celsius = % d", Fahr, Celsius );
Return 0;
}
[Exercise 2-9]
# Include <stdio. h>
Int main (void)
{
Int A, B, C, D;
Printf ("enter :");
Scanf ("% d", & );
Printf ("Enter B :");
Scanf ("% d", & B );
Printf ("Enter C :");
Scanf ("% d", & C );
D = (a B C)/3;
Printf ("d = % d", d );
Return 0;
}
[Practice 2-10]
# Include <stdio. h>
Int main (void)
{
Double X, Y;
Printf ("Enter X :");
Scanf ("% lf", & X );
If (X! = 0)
{
Y = 1/X;
}
Else
{
Y = 0;
}
Printf ("Y = f (% F) = %. 3f", x, y );
Return 0;
}
[Exercise 2-11]
# Include <stdio. h>
# Include <math. h>
Int main (void)
{
Double X, Y;
Printf ("Enter X :");
Scanf ("% lf", & X );
If (x <0)
{
Y = POW (x, 5.0) x * 2 1/X;
}
Else
{
Y = SQRT (X );
}
Printf ("Y = f (% F) = %. 2f", x, y );
Return 0;
}
[Exercise 2-12]
# Include <stdio. h>
Int main (void)
{
Int lower, upper, Fahr;
Double Celsius;
Printf ("Enter lower :");
Scanf ("% d", & Lower );
Printf ("enter upper :");
Scanf ("% d", & upper );
Printf ("Fahr Celsius \ n ");
For (Fahr = lower; Fahr <= upper; Fahr = Fahr 2)
{
Celsius = (5.0/9.0) * (fahr-32 );
Printf ("% d %. 1f \ n", Fahr, Celsius );
}
Return 0;
}
[Exercise 2-13]
# Include <stdio. h>
Int main (void)
{
Int I, N;
Double sum;
Printf ("Enter N :");
Scanf ("% d", & N );
Sum = 0;
For (I = 1; I <= N; I)
{
Sums = sum 1.0/I;
}
Printf ("sum is %. 3f \ n", sum );
Return 0;
}
[Exercise 2-14]
# Include <stdio. h>
Int main (void)
{
Int I, n, denominator;
Double sum, item;
Printf ("Enter N :");
Scanf ("% d", & N );
Sum = 0;
For (I = 1; I <= N; I)
{
Denominator = 2 * I-1;
Item = 1.0/denominator;
Sum = sum item;
}
Printf ("sum is %. 3f", sum );
Return 0;
}
[Exercise 2-15]
# Include <stdio. h>
Int main (void)
{
Int I, n, denominator, flag;
Double item, sum;
Printf ("Enter N :");
Scanf ("% d", & N );
Flag = 1;
Sum = 0;
Denominator = 1;
For (I = 1; I <= N; I)
{
Item = flag * 1.0/denominator;
Sum = item sum;
Flag =-flag;
Denominator = Denominator 3;
}
Printf ("sum is %. 3f \ n", sum );
Return 0;
}
[Exercise 2-16]
# Include <stdio. h>
Int main (void)
{
Int m, n, I, sum;
Double A, B;
Printf ("Enter M :");
Scanf ("% d", & M );
Printf ("Enter N :");
Scanf ("% d", & N );
A = 1; B = 1;
For (I = 1; I <= m; I)
{
A = A * I;
}
For (I = 1; I <= N; I)
{
B = B * I;
}
Sum = a B;
Printf ("sum is % d \ n", sum );
Return 0;
}
[Exercise 2-17]
10
[Exercise 2-18]
# Include <stdio. h>
# Include <math. h>
Int main (void)
{
Int I, N;
Double product;
Printf ("Enter N :");
Scanf ("% d", & N );
For (I = 0; I <= N; I)
{
Product = POW (3, I );
Printf ("Pow (3, % d) = %. 0f \ n", I, product );
}
Return 0;
}
[Exercise 2-19]
# Include <stdio. h>
Double fact (int n );
Int main (void)
{
Int I, N;
Double result;
Printf ("Enter N :");
Scanf ("% d", & N );
For (I = 1; 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;
}
[Exercise 2-20]
# Include <stdio. h>
Double fact (int n );
Int main (void)
{
Int M, N;
Double compose;
Printf ("m <= n \ n ");
Printf ("Enter M :");
Scanf ("% d", & M );
Printf ("Enter N :");
Scanf ("% d", & N );
Compose = fact (N)/fact (m)/fact (n-M );
Printf ("compose is %. 0f \ n", compose );
Return 0;
}
Double fact (int n)
{
Int I;
Double product;
Product = 1;
For (I = 1; I <= N; I)
{
Product = product * I;
}
Return product;
}
[Exercise 2-1]
# Include <stdio. h>
# Include <math. h>
Int main (void)
{
Int num1, num2, sum, difference;
Double accumulate, quotient, surplus;
Printf ("Enter num1 :");
Scanf ("% d", & num1 );
Printf ("Enter num2 :");
Scanf ("% d", & num2 );
Sum = num1 num2;
Difference = num1-num2;
Accumulate = num1 * num2;
Quotient = num1/num2;
Surplus = fmod (num1, num2 );
Printf ("sum is % d \ ndifference is % d \ n", sum, difference );
Printf ("accumulate is %. 0f \ n", accumulate );
Printf ("quotient is %. 0f \ n", quotient );
Printf ("surplus is %. 0f \ n", surplus );
Return 0;
}
[Exercise 2-2]
# Include <stdio. h>
Int main (void)
{
Int X;
Double Y, result;
Printf ("Enter X :");
Scanf ("% d", & X );
If (x <= 50)
{
Y = 0.53;
}
Else
{
Y = 0.58;
}
Result = x * Y;
Printf ("result is %. 2f \ n", result );
Return 0;
}
[Exercise 2-3]
# Include <stdio. h>
Int main (void)
{
Int N, I;
Double X, product;
Printf ("Enter X :");
Scanf ("% lf", & X );
Printf ("Enter N :");
Scanf ("% d", & N );
Product = 1;
For (I = 1; I <= N; I)
{
Product = product * X;
}
Printf ("product is %. 2f \ n", product );
Return 0;
}
[Exercise 2-4]
# Include <stdio. h>
Int main (void)
{
Int m, n, I;
Double result, part;
Printf ("m <= n \ n ");
Printf ("Enter M :");
Scanf ("% d", & M );
Printf ("Enter N :");
Scanf ("% d", & N );
Result = 0;
For (I = m; I <= N; I)
{
Part = I * I 1.0/I;
Result = Result part;
}
Printf ("result is %. 2f \ n", result );
Return 0;
}
[Exercise 2-5]
# Include <stdio. h>
Int main (void)
{
Int flag, I, N;
Double sum, part, denominator;
Printf ("Enter N :");
Scanf ("% d", & N );
Flag = 1;
Sum = 0;
For (I = 1; I <= N; I)
{
Denominator = I * 2-1;
Part = flag * I/denominator;
Sum = sum part;
Flag =-flag;
}
Printf ("sum is %. 2f \ n", sum );
Return 0;
}
[Exercise 2-6]
# Include <stdio. h>
# Include <math. h>
Int main (void)
{
Int loan, month, year;
Double rate, money;
Printf ("Enter loan :");
Scanf ("% d", & Loan );
Printf ("Enter rate :");
Scanf ("% lf", & rate );
Month = 70;
Printf ("year money \ n ");
For (year = 5; year <= 30; Year)
{
Money = Loan * rate * POW (1 rate, month)/(POW (1 rate, month)-1 );
Month = Month 12;
Printf ("% 2.d % F \ n", year, money );
}
Return 0;
}
[Exercise 2-7]
# Include <stdio. h>
# Include <math. h>
Int main (void)
{
Int N, I, part, sum;
Printf ("Enter N :");
Scanf ("% d", & N );
Sum = 0;
For (I = 1; I <= N; I)
{
Part = POW (2, I );
Sum = sum part;
}
Printf ("sum is % d \ n", sum );
Return 0;
}
[Exercise 2-8]
# Include <stdio. h>
Double fact (int n );
Int main (void)
{
Int N, I, e, part;
Printf ("Enter N :");
Scanf ("% d", & N );
E = 0;
For (I = 0; I <= N; I)
{
Part = fact (I );
E = e part;
}
Printf ("e = % d \ n", e );
Return 0;
}
Double fact (int n)
{
Int I, product;
Product = 1;
For (I = 1; I <= N; I)
{
Product = product * I;
}
Return product;
}

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.