[Example 3-1]
# Include <stdio. h>
Int main (void)
{
Int digit, I, letter, other;
Char ch;
Digit = letter = other = 0;
Printf ("Enter 10 characters :");
For (I = 1; I <= 10; I)
{
Ch = getchar ();
If (ch> = 'A' & ch <= 'Z') | (ch> = 'A' & ch <= 'Z '))
Letter;
Else if (ch> = '0' & ch <= '9 ')
Digit;
Else
Other;
}
Printf ("letter = % d, digit = % d, other = % d \ n", letter, digit, other );
Return 0;
}
[Example 3-3]
# Include <stdio. h>
Int main (void)
{
Double x, y;
Printf ("Enter x :");
Scanf ("% lf", & x );
If (x <0)
{
Y = 0;
}
Else if (x <= 15)
{
Y = 4 * x/3;
}
Else
{
Y = 2.5 * x-10.5;
}
Printf ("f (%. 2f) = %. 2f \ n", x, y );
Return 0;
}
[Example 3-4]
# Include <stdio. h>
Int main (void)
{
Int choice, I;
Double price;
For (I = 1; I <= 5; I)
{
Printf ("[1] Select crisps \ n ");
Printf ("[2] Select popcorn \ n ");
Printf ("[3] Select chocolate \ n ");
Printf ("[4] Select cola \ n ");
Printf ("[0] exit \ n ");
Printf ("Enter choice :");
Scanf ("% d", & choice );
If (choice = 0)
Break;
Switch (choice)
{
Case 1: price = 3.0; break;
Case 2: price = 2.5; break;
Case 3: price = 4.0; break;
Case 4: price = 3.5; break;
Default: price = 0.0; break;
}
Printf ("price = % 0.1f \ n", price );
}
Printf ("Thanks \ n ");
Return 0;
}
[Example 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 '/':
Printf ("= %. 2f \ n", value1/value2 );
Break;
Default:
Printf ("Unknown operator \ n ");
Break;
}
Return 0;
}
[Example 3-6]
# Include <stdio. h>
Int main (void)
{
Int blank, digit, I, other;
Char ch;
Blank = digit = other = 0;
Printf ("Enter 10 characters :");
For (I = 1; I <= 10; I)
{
Ch = getchar ();
Switch (ch)
{
Case '':
Case '\ N ':
Blank;
Break;
Case '0': case '1': case '2': case '3': case '4 ':
Case '5': case '6': case '7': case '8': case '9 ':
Digit;
Break;
Default:
Other;
Break;
}
}
Printf ("blank = % d, digit = % d, other = % d \ n", blank, digit, other );
Return 0;
}
[Example 3-7]
# Include <stdio. h>
Int main (void)
{
Int number;
Printf ("Enter a number :");
Scanf ("% d", & number );
If (number % 2 = 0)
{
Printf ("The number is even. \ n ");
}
Else
{
Printf ("The number is odd. \ n ");
}
Return 0;
}
[Example 3-8]
# Include <stdio. h>
Int main (void)
{
Int number;
Printf ("Enter a number :");
Scanf ("% d", & number );
If (number <0)
{
Number =-number;
}
Printf ("The absolute value is % d. \ n", number );
Return 0;
}
[Example 3-9]
# Include <stdio. h>
Int main (void)
{
Int count, I, n;
Double grade, total;
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 ("Grade average = %. 2f \ n", total/n );
Printf ("Number of failures = % d \ n", count );
Return 0;
}