Chapter 3 exercises and exercises in C language programming

Source: Internet
Author: User

[Exercise 3-1]
No; because all possible conditions are covered.
[Exercise 3-2]
# Include <stdio. h>
Int main (void)
{
Int y;
Double X;
Printf ("Enter X :");
Scanf ("% lf", & X );
If (x <0)
Y =-1;
Else if (x = 0)
Y = 0;
Else
Y = 1;
Printf ("Y = Sign (% F) = % d \ n", x, y );
Return 0;
}
[Exercise 3-3]
# Include <stdio. h>
Int main (void)
{
Char ch;
Int I, digit, letter, Se, other;
Printf ("Enter 15 characters :");
Digit = letter = Se = Other = 0;
For (I = 1; I <= 15; I)
{
Ch = getchar ();
If (CH> = '0' & Ch <= '9 ')
Digit;
Else if (CH> = 'A' & Ch <= 'Z') | (CH> = 'A' & Ch <= 'Z '))
Letter;
Else if (CH = ''| CH = '\ n ')
Se;
Else
Other;
}
Printf ("digit = % d, letter = % d, Se = % d, other = % d \ n", digit, letter, Se, other );
Return 0;
}
[Exercise 3-4]
The output value is "Price = 0.0". If the break is not used, not only the statement segment is met, but also all subsequent statement segments are executed.
[Exercise 3-5]
# Include <stdio. h>
Int main (void)
{
Double value1, value2;
Char operato;
Printf ("type in an expression :");
Scanf ("% lf % C % lf", & value1, & operato, & value2 );
Switch (operato)
{
Case '':
Printf ("= %. 2f \ n", value1 value2 );
Break;
Case '-':
Printf ("= %. 2f \ n", value1-value2 );
Break;
Case '*':
Printf ("= %. 2f \ n", value1 * value2 );
Break;
Case '/':
If (value2 = 0)
Printf ("You are wrong \ n ");
Else
Printf ("= %. 2f \ n", value1/value2 );
Break;
Default:
Printf ("unknown operator \ n ");
Break;
}
Return 0;
}
[Exercise 3-6]
# Include <stdio. h>
Int main (void)
{
Char level;
Printf ("Enter level (~ E ):");
Level = getchar ();
Switch (level)
{
Case 'A ':
Printf ("90 ~ 100 \ n ");
Break;
Case 'B ':
Printf ("80 ~ 89 \ n ");
Break;
Case 'C ':
Printf ("70 ~ 79 \ n ");
Break;
Case 'D ':
Printf ("60 ~ 69 \ n ");
Break;
Case 'E ':
Printf ("0 ~ 59 \ n ");
Break;
Default:
Printf ("Wrong level \ n ");
Break;
}
Return 0;
}
[Exercise 3-7]
# Include <stdio. h>
Int main (void)
{
Int I, choice;
Double price;
Printf ("[1] apples \ n ");
Printf ("[2] pears \ n ");
Printf ("[3] oranges \ n ");
Printf ("[4] Grapes \ n ");
Printf ("[0] exit \ n ");
For (I = 1; I <= 5; I)
{
Printf ("Enter choice :");
Scanf ("% d", & choice );
If (choice = 0)
{
Printf ("Byebye! \ N ");
Break;
}
Switch (choice)
{
Case 1:
Price = 3.00;
Break;
Case 2:
Price = 2.50;
Break;
Case 3:
Price = 4.10;
Break;
Case 4:
Price = 10.20;
Break;
Default:
Price = 0.00;
}
Printf ("Price = %. 2f \ n", price );
If (I = 5) & (choice! = 0 ))
Printf ("You have used five times. Please try again. \ n ");
}
Return 0;
}
[Exercise 3-8]
# Include <stdio. h>
Int main (void)
{
Int year;
Printf ("Enter year :");
Scanf ("% d", & year );
If (Year % 4 = 0 & year0! = 0) | (Year @ 0 = 0 ))
{
Printf ("It is leap year. \ n ");
}
Else
{
Printf ("It isn' t leap year. \ n ");
}
Return 0;
}
[Exercise 3-9]
# Include <stdio. h>
Int main (void)
{
Int N, I, count;
Double total, grade;
Printf ("Enter N :");
Scanf ("% d", & N );
Total = 0;
Count = 0;
For (I = 1; I <= N; I)
{
Printf ("enter grade # % d:", I );
Scanf ("% lf", & grade );
Total = total grade;
If (grade> = 60)
{
Count;
}
}
Printf ("The avaerage = %. 2f \ n", total/N );
Printf ("the number of qualifies = % d \ n", count );
Return 0;
}
[Exercise 3-10]
# Include <stdio. h>
Int main (void)
{
Double X, Y;
Printf ("Enter X :");
Scanf ("% lf", & X );
If (x <0)
{
Y = 0;
}
If (x> = 0)
{
If (x <= 15)
{
Y = 4 * X/3;
}
Else
{
Y = 2.5 * x-10.5;
}
}
Printf ("F (%. 2f) = %. 2f \ n", x, y );
Return 0;
}
[Exercise 3-11]
The conditions for rewriting the first two statements are: X less than 1; X greater than or equal to 1 and less than 2. The execution conditions of the two statements after rewriting are: X less than 1; X greater than or equal to 2.
[Exercise 3-1]
# Include <stdio. h>
# Include <math. h>
Int main (void)
{
Int A, B, C;
Double area, S, perimeter, part;
Printf ("enter :");
Scanf ("% d", & );
Printf ("Enter B :");
Scanf ("% d", & B );
Printf ("Enter C :");
Scanf ("% d", & C );
If (a B> C) & (B c> A) & (c a> B ))
{
S = (a B C)/2;
Part = S * (S-A) * (S-B) * (s-c );
Area = SQRT (part );
Perimeter = a B C;
Printf ("Area = %. 2f \ n", area );
Printf ("Perimeter = %. 2f \ n", perimeter );
}
Else
{
Printf ("these sides do not correspond to a valid triangle. \ n ");
}
Return 0;
}
[Exercise 3-2]
# Include <stdio. h>
Int main (void)
{
Int salary, rate;
Double tax;
Printf ("Enter salary :");
Scanf ("% d", & Salary );
If (salary <= 850)
{
Rate = 0;
}
Else if (salary <= 1350)
{
Rate = 5;
}
Else if (salary <= 2850)
{
Rate = 10;
}
Else if (salary <= 5850)
{
Rate = 15;
}
Else
{
Rate = 20;
}
Tax = Rate * (salary-850)/100;
Printf ("tax = %. 2f \ n", tax );
Return 0;
}
[Exercise 3-3]
# Include <stdio. h>
Int main (void)
{
Int km, min;
Double part, money;
Printf ("Enter km :");
Scanf ("% d", & km );
Printf ("Enter min :");
Scanf ("% d", & min );
If (km <= 3)
{
Money = 10;
}
Else
{
Money = 10 (km-3) * 3;
}
If (min> 0)
{
Part = min/5;
Money = money part * 3;
}
Printf ("Money = %. 0f \ n", money );
Return 0;
}
[Exercise 3-4]
# Include <stdio. h>
Int main (void)
{
Int A, B, C, D, E, N, I;
Double grade, sum, average;
Printf ("Enter N :");
Scanf ("% d", & N );
A = B = c = d = E = sum = 0;
For (I = 1; I <= N; I)
{
Printf ("enter grade # % d:", I );
Scanf ("% lf", & grade );
Sum = sum grade;
If (Grade <60)
{
E;
}
Else if (Grade <70)
{
D;
}
Else if (Grade <80)
{
C;
}
Else if (Grade <90)
{
B;
}
Else
{
A;
}
}
Average = sum/N;
Printf ("average = %. 2f \ n", average );
Printf ("A = % d \ NB = % d \ NC = % d \ nD = % d \ Ne = % d \ n", a, B, c, d, e );
Return 0;
}
[Exercise 3-5]
# Include <stdio. h>
Int main (void)
{
Int I;
For (I = 2000; I <= 3000; I)
{
If (I % 4 = 0) & (I0! = 0) | (I @ 0 = 0 ))
{
Printf ("% d \ n", I );
}
}
Return 0;
}

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.