Chapter 4 exercises and exercises in C language programming

Source: Internet
Author: User

[Exercise for 4-1]
0.0000; The while LOOP statement cannot be used. Yes; the number of cycles is reduced.
[Practice 4-2]
Not changed. 0.00; if the negative number does not meet the conditions of the loop statement, the statement jumps out.
[Exercise 4-3]
# Include <stdio. h>
Int main (void)
{
Int flag, denominator;
Double eps, item, s;
Printf ("Enter eps :");
Scanf ("% lf", & eps );
Flag = 1;
S = 0;
Denominator = 1;
Item = 1;
While (item> = eps)
{
S = s + flag * item;
Denominator = denominator + 3;
Item = 1/denominator;
Flag =-flag;
}
S = s + flag * item;
Printf ("s = %. 2f \ n", s );
Return 0;
}
[Exercise 4-4]
# Include <stdio. h>
Int main (void)
{
Int grade, count;
Double sum;
Printf ("Enter grades :");
Scanf ("% d", & grade );
Sum = 0;
Count = 0;
While (grade> = 0)
{
Sum = sum + grade;
Count ++;
Scanf ("% d", & grade );
}
If (count! = 0)
{
Printf ("The average is %. 2f \ n", sum/count );
}
Else
{
Printf ("The average is 0 \ n ");
}
Return 0;
}
[Practice 4-5]
Yes; the loop statement will be executed once more. Add "if (number = 0) break;" at the end of the while statement ;".
[Exercise 4-6]
No. The statement is printed repeatedly.
[Exercise 4-7]
# Include <stdio. h>
Int main (void)
{
Int I, j, n, m;
Printf ("Enter n :");
Scanf ("% d", & n );
For (I = 1; I <= n; I ++)
{
Printf ("Enter # % d number:", I );
Scanf ("% d", & m );
For (j = 2; j <= m/2; j ++)
{
If (m % j = 0)
{
Break;
}
}
If (j> m/2)
{
Printf ("% d is prime number. \ n", m );
}
Else
{
Printf ("% d isn't prine number. \ n", m );
}
}
Return 0;
}
[Exercise 4-8]
# Include <stdio. h>
Int main (void)
{
Int I, j, n;
Double item, sum;
Printf ("Enter n :");
Scanf ("% d", & n );
Sum = 0;
For (I = 1; I <= n; I ++)
{
Item = 1;
For (j = 1; j <= I; j ++)
{
Item = item * j;
}
Sum = sum + 1/item;
}
Printf ("e = %. 2f \ n", sum );
Return 0;
}
[Exercise 4-9]
The highest score is the first input score, and an if statement is added.
[Exercise 4-10]
The highest score is the input negative number. Add an if statement.
[Exercise 4-11]
# Include <stdio. h>
Int main (void)
{
Int min, n, I, number;
Printf ("Enter n :");
Scanf ("% d", & n );
Printf ("Enter #1 number :");
Scanf ("% d", & number );
Min = number;
For (I = 2; I <= n; I ++)
{
If (min> number)
{
Min = number;
}
Printf ("Enter # % d number:", I );
Scanf ("% d", & number );
}
Printf ("The min number is % d \ n", min );
Return 0;
}
[Exercise 4-12]
# Include <stdio. h>
Int main (void)
{
Int number, m;
Printf ("Enter a number :");
Scanf ("% d", & number );
If (number <0)
{
Number =-number;
}
Do
{
M = number;
Printf ("% d", m );
Number = number/10;
}
While (number! = 0 );
Printf ("\ n ");
Return 0;
}
[Exercise 4-13]
# Include <stdio. h>
# Include <math. h>
Int main (void)
{
Int I, j, count;
Count = 0;
For (I = 100; I <= 200; I ++)
{
For (j = 2; j <sqrt (I); j ++)
{
If (I % j = 0)
{
Break;
}
}
If (j> sqrt (I ))
{
Printf ("m", I );
Count ++;
}
If (count/8> 0 & count % 8 = 0)
{
Printf ("\ n ");
}
}
Printf ("\ n ");
Return 0;
}
[Exercise 4-14]
# Include <stdio. h>
Int main (void)
{
Int x, x1, x2, I, count;
X1 = 1;
X2 = 1;
Count = 2;
Printf ("mm", x1, x2 );
For (I = 1; I <= 18; I ++)
{
X = x1 + x2;
Printf ("m", x );
Count ++;
X1 = x2;
X2 = x;
If (count/10> 0 & count = 0)
{
Printf ("\ n ");
}
}
Return 0;
}
[Exercise 4-1]
# Include <stdio. h>
Int main (void)
{
Int number, sum;
Printf ("Enter number :");
Scanf ("% d", & number );
Sum = 0;
If (number> 0)
{
While (number % 2 = 0)
{
Sum = sum + number;
Scanf ("% d", & number );
}
Printf ("sum = % d \ n", sum );
}
Return 0;
}
[Exercise 4-2]
# Include <stdio. h>
# Include <math. h>
Int fact (int n );
Int main (void)
{
Int I, j;
Double s, x, item;
Printf ("Enter x :");
Scanf ("% lf", & x );
S = 0;
I = 1;
J = 1;
Item = pow (x, I)/fact (j );
While (fabs (item) & gt; 0.00001)
{
S = s + item;
I ++;
J ++;
Item = pow (x, I)/fact (j );
}
Printf ("s = %. 2f \ n", s );
Return 0;
}
Int fact (int n)
{
Int I, s;
S = 1;
For (I = 1; I <= n; I ++)
{
S = s * I;
}
Return s;
}
[Exercise 4-3]
# Include <stdio. h>
Int main (void)
{
Int number, sum, digit, item;
Printf ("Enter number :");
Scanf ("% d", & number );
Sum = 0;
Digit = 0;
While (number> 0)
{
Item = number;
Sum = sum + item;
Digit ++;
Number = number/10;
}
Printf ("sum = % d, digit = % d \ n", sum, digit );
Return 0;
}
[Exercise 4-4]
# Include <stdio. h>
Int main (void)
{
Int numerator, I, n, x;
Double item, sum, denominator;
Printf ("Enter n :");
Scanf ("% d", & n );
Sum = 0;
Denominator = 1;
Numerator = 2;
For (I = 1; I <= n; I ++)
{
Item = numerator/denominator;
Sum = sum + item;
X = numerator;
Numerator = numerator + denominator;
Denominator = x;
}
Printf ("sum = %. 2f \ n", sum );
Return 0;
}
[Exercise 4-5]
# Include <stdio. h>
Int main (void)
{
Int a, n, I, j, sum, item;
Printf ("Enter :");
Scanf ("% d", & );
Printf ("Enter n :");
Scanf ("% d", & n );
Sum = 0;
Item = 0;
For (I = 1; I <= n; I ++)
{
Item = item * 10 +;
Sum = sum + item;
}
Printf ("s = % d \ n", sum );
Return 0;
}
[Exercise 4-6]
# Include <stdio. h>
Int main (void)
{
Int a, B, c;
For (a = 1; a <= 10; a ++)
{
For (B = 1; B <= 5; B ++)
{
For (c = 1; c <= 2; c ++)
{
If (a + B * 2 + c * 5 = 10)
{
Printf ("1fen = % d, 2fen = % d, 5fen = % d \ n", a, B, c );
}
}
}
}
Return 0;
}
[Exercise 4-7]
# Include <stdio. h>
# Include <math. h>
Int main (void)
{
Int I, m, x, count;
Double sum;
For (I = 1; I <= 10000; I ++)
{
X = I;
Sum = 0;
While (x! = 0)
{
M = x;
Sum = sum + pow (m, 3 );
X = x/10;
}
If (sum = I)
{
Printf ("m", I );
Count ++;
}
If (count> 0 & count % 5 = 0)
{
Printf ("\ n ");
}
}
Printf ("\ n ");
Return 0;
}
[Exercise 4-8]
# Include <stdio. h>
# Include <math. h>
Int main (void)
{
Int I, k, j, l, count, n;
Double m;
For (I = 6; I <= 100; I ++)
{
If (I % 2 = 0)
{
For (j = 2; j <= I/2; j ++)
{
M = sqrt (j );
For (k = 2; k <= m; k ++)
{
If (j % k = 0)
{
Break;
}
}
If (k> m)
{
N = I-j;
If (n> 0)
{
M = sqrt (n );
For (l = 2; l <= m; l ++)
{
If (n % l = 0)
{
Break;
}
}
If (l> m)
{
Printf ("m = % d + % d", I, j, n );
Count ++;
If (count! = 0 & count % 5 = 0)
{
Printf ("\ n ");
}
}
}
}
}
}
}
Printf ("\ n ");
Return 0;
}
[Exercise 4-9]
# Include <stdio. h>
Int main (void)
{
Int n, a, B, c;
Printf ("Enter number :");
Scanf ("% d", & n );
B = 0;
While (n! = 0)
{
B = B * 10;
A = n;
N = n/10;
B = B +;
}
While (B! = 0)
{
C = B;
Printf ("=", c );
B = B/10;
}
Printf ("\ n ");
Return 0;
}
[Exercise 4-10]
# Include <stdio. h>
Int main (void)
{
Int n, I, m;
Printf ("Enter number :");
Scanf ("% d", & n );
Printf ("% d =", n );
M = n/2;
For (I = 2; I <= m; I ++)
{
If (n % I = 0)
{
Printf ("% d", I );
N = n/I;
Break;
}
}
While (n> 1)
{
For (I = 2; I <= m; I ++)
{
If (n % I = 0)
{
Printf ("* % d", I );
N = n/I;
Break;
}
}
}
Printf (". \ n ");
Return 0;
}
[Exercise 4-11]
# Include <stdio. h>
Int main (void)
{
Int m, n, k, l, max, min, I;
Printf ("Enter m :");
Scanf ("% d", & m );
Printf ("Enter n :");
Scanf ("% d", & n );
Max = 1;
Min = m * n;
L = m * n;
If (m> n)
{
K = m;
}
Else
{
K = n;
}
For (I = 2; I <= k; I ++)
{
If (m % I = 0 & n % I = 0)
{
If (I> max)
{
Max = I;
}
}
}
For (I = 2; I <= min; I ++)
{
If (min % I = 0)
{
If (m % I = 0 & n % I = 0)
{
L = min/I;
If (l <min)
{
Min = l;
}
}
}
}
Printf ("max = % d, min = % d \ n", max, min );
Return 0;
}
[Exercise 4-12]
# Include <stdio. h>
Int main (void)
{
Int I;
Double n, sum;
Sums = 100;
N = 100;
For (I = 2; I <= 10; I ++)
{
Sum = sum + n;
N = n/2;
}
Printf ("sum = %. 4f, high = %. 4f \ n", sum, n );
Return 0;
}
[Exercise 4-13]
# Include <stdio. h>
Int main (void)
{
Int I, j, k;
For (I = 1; I <= 4; I ++)
{
For (j = 1; j <(5-i); j ++)
{
Printf ("");
}
J = 2 * I-1;
For (k = 1; k <= j; k ++)
{
Printf ("*");
}
Printf ("\ n ");
}
For (I = 3; I> = 1; I --)
{
For (j = 1; j <(5-i); j ++)
{
Printf ("");
}
J = 2 * I-1;
For (k = 1; k <= j; k ++)
{
Printf ("*");
}
Printf ("\ n ");
}
Return 0;
}
[Exercise 4-14]
# Include <stdio. h>
Int main (void)
{
Int I, n;
N = 1;
For (I = 1; I <= 9; I ++)
{
N = (n + 1) * 2;
}
Printf ("n = % d \ n", n );
Return 0;
}

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.