Sei sulla pagina 1di 15

Answers to "Programming in C" by Kochan 3rd Edition

Chapter 3

Answer 1 - N/A (Just type in, compile and run this chapter's example programs as described in Q)

Answer 2 -
#include <stdio.h>
#include <conio.h>

int main ( void )


{
printf ("1. In C, lowercase letters are significant.\n\n");
printf ("2. main is where program execution begins.\n\n");
printf ("3. Opening and closing braces enclose program statements in a routine.\n\n");
printf ("4. All program statements must be terminated by a semicolon.\n\n");

getch();
return 0;
}

Answer 3 - Console Output: Testing.......1...2..3

Answer 4 -
#include <stdio.h>
#include <conio.h>

int main (void)


{
int a = 87, b = 15, sum = a - b;

printf ("%i - %i = %i",a,b,sum);


getch();
return 0;
}

Answer 5-
#include <stdio.h>

int main (Void) <---should be lower case v in void.


( <---should be { not (.
INT sum; <---INT should be lower case.
/* COMPUTE RESULT <---missing */ comment container end.
sum = 25 + 37 - 19 <---missing ; at end of statement.
/* DISPLAY RESULTS // <---// is not proper comment container end.
printf ("The answer is %i\n" sum); <---missing , after quotations

return 0;
}

Answer 6 - Console Output: The result is 95


Chapter 4

Answer 1 - N/A (Type in, compile and run the sample programs within the chapter).

Answer 2 - A. char - reserved command word within C language.


B. 6_05 - variable names can't begin with a number.
C. A$ - (ed. note- The book states that $ is not a valid character, however A$ worked as a variable
with my compiler. All variables in this exercise were tested.)
D. All others labels are proper variable names.

Answer 3- A. 0x10.5 - Decimals are not permitted in hexadecimal constants.


B. 0X0G1 - G is not an acceptable hexadecimal character. (A-F only)
C. 98.7U - Unsigned variable must be an integer (no decimals)
D. 17777s - There is no "s" variable definer suffix. (u, l, f are acceptable)
E. 0996 - Beginning 0 signifies octal constant value, 9 is not within acceptable range (0-7).
F. 1.2Fe-7 - F does not belong in expression.
G. 15,000 - the comma seperates 15 and 000. (15000 is proper expression)

Answer 4 - (ed. - I programmed this to result in a precise measurement of C using a float and %f
variable format. The result is -2.777778. Using a normal int and %i variable format would result in
-2.)

#include <stdio.h>
#include <conio.h>

int main (void)


{

float C; int F = 27;

C = (F - 32) / 1.8 ;
printf ("%i degrees F = %f degrees C\n",F ,C);

getch();
return 0;
}

Answer 5 - Console Output: d = d

Answer 6 - (ed. - In this program I break the formula down to its roots and simplify accordingly,
each step displayed in the console.)

#include <stdio.h>
#include <conio.h>

int main (void)


{
float x=2.55, a,b,c,d,sum;
a = x*x*x;
b = x*x;
c = a*3;
d = 5*b;
sum = 3*a - 5*b + 6;

printf ("x = %g\n\n",x);


printf ("3x^3 - 5x^2 + 6 = %g\n\n",sum );
printf ("3(2.55*2.55*2.55)-5(2.55*2.55) + 6 = %g\n\n",sum );
printf ("3*%g - 5*%g + 6 = %g\n\n",a,b,sum);
printf ("%g - %g + 6 = %g\n",c,d,sum);

getch();
return 0;
}

Answer 7 - (ed. - Once again, I go in depth and simplify)

#include <stdio.h>
#include <conio.h>

int main (void)


{
float a,b,c,d,e,f,sum;

sum = (3.31e8 * 2.01e7) / (7.16e6 + 2.01e8);


a = 3.31e8, b = 2.01e7, c = 7.16e6, d = 2.01e8;
e = a*b, f = c+d;

printf ("(3.31 x 10^8 x 2.01 x 10^7) / (7.16 x 10^6 + 2.01 x 10^8) = %e\n\n",sum);

printf ("(3.31e8 x 2.01e7) / (7.16e6 + 2.01e8) = %e\n\n",sum );

printf ("(%e x %e) / (%e + %e) = %e\n\n",a,b,c,d,sum );


printf ("%e / %e = %e\n\n",e,f,sum);

getch();
return 0;
}

Answer 8 -
#include <stdio.h>
#include <conio.h>

int main (void)


{
int i, j;

i = 365 ,j = 7 ;
printf ("i = %i, j = %i, Next Largest Multiple = %i\n\n",i,j,i + j - i % j);

i = 12258 ,j = 23 ;
printf ("i = %i, j = %i, Next Largest Multiple = %i\n\n",i,j,i + j - i % j);

i = 996 ,j = 4 ;
printf ("i = %i, j = %i, Next Largest Multiple = %i\n\n",i,j,i + j - i % j);
getch();
return 0;
}

Chapter 5

Answer 1 - N/A (Type in, compile and run each example program in the chapter.)

Answer 2 -

#include <stdio.h>
#include <conio.h>

int main (void)


{

int n=1;

printf ("Table That Displays N and N^2 from 1 to 10\n\n");


printf (" N N^2\n");
printf ("========\n");

while ( n <= 10 ) {
printf ("%2i %3i\n", n,n*n);
++n;
}

getch();
return 0;
}

Answer 3 -

#include <stdio.h>
#include <conio.h>

int main (void)


{

int n=5, triangularNumber=0;

printf ("Table That Displays Every Fifth Triangle Number (TN) From 5 to 50\n\n");
printf (" Root TN\n");
printf ("===============\n");

while ( n <= 50 ) {
triangularNumber = n * (n + 1) / 2;
printf (" %2i %4i\n", n,triangularNumber);
n=n+5;
}

getch();
return 0;
}

Answer 4 -

#include <stdio.h>
#include <conio.h>

int main (void)


{
//variables are named
int n, n_temp, secondary_counter, f, f_temp;

//header
printf ("Table That Displays First 10 Factorals\n\n");
printf (" Root Factoral\n");
printf ("=================\n");

n = 1;

//initial counter that also counts root


while ( n <= 10 ) {

//temporary variables are defined


n_temp = n;
f = n;
secondary_counter = 1;

//secondary routine limited by n (root)


while (secondary_counter <n){
//f_temp calculates first factor
f_temp = f * (n_temp -1);
//it gets transferred to f
f = f_temp;
//n_temp factor is reduced -1
n_temp = n_temp - 1;
//secondary counter increases +1
++secondary_counter;
}

printf (" %2i %7i\n",n,f);


++n;
}

printf ("\n");
getch();
return 0;
}

Answer 5-

#include <stdio.h>
#include <conio.h>

int main (void)

{
int n, tn;

printf("TABLE OF POWERS OF TWO\n\n");


printf(" n 2 to the n\n");
printf("--- ---------------\n");

tn=1;

for ( n=0 ; n<=10; ++n){

printf("%2i %i\n",n,tn); tn*=2;

getch();
return 0;
}

Answer 6 - Left column becomes left-justified.

Answer 7 - Using a decimal point before the field width specification (%.2i) assures the variable
will fill at least 2 digits when displayed. (i.e. - 2 = 02, 9 = 09)

Answer 8 -

#include <stdio.h>
#include <conio.h>

int main (void)


{
int n, number, triangularNumber, counter, how_many;

printf ("How many Triangular numbers do you want to calculate?\n\n");


scanf ("%i", &how_many );
printf ("\n");
for ( counter = 1; counter <= how_many; ++counter ) {
printf ("What triangular number do you want? ");
scanf ("%i", &number);
triangularNumber = 0;
for ( n = 1; n <= number; ++n )
triangularNumber += n;
printf ("Triangular number %i is %i\n\n", number, triangularNumber);
}

getch();
return 0;
}

Answer 9

Program 5.2

#include <stdio.h>
#include <conio.h>

int main (void)


{
int n, triangularNumber;

triangularNumber = 0;
n=1;

while (n <= 200) {


triangularNumber = triangularNumber + n;
(n = n + 1 );
}

printf ("The 200th triangular number is %i\n", triangularNumber);

getch();
return 0;
}

Program 5.3

#include <stdio.h>
#include <conio.h>

int main (void)


{
int n, triangularNumber;

printf ("TABLE OF TRIANGULAR NUMBERS\n\n");


printf (" n Sum from 1 to n\n");
printf ("--- ---------------\n");

triangularNumber = 0;
n=1;

while ( n <= 10) {


triangularNumber += n;
printf (" %i %i\n", n, triangularNumber);
++n;
}

getch();
return 0;
}

Program 5.4

#include <stdio.h>
#include <conio.h>

int main (void)


{
int n, number, triangularNumber;

printf ("What triangular number do you want? ");


scanf ("%i", &number);

triangularNumber = 0;
n=1;
while ( n <= number ) {
triangularNumber += n;
++n;
}

printf ("Triangular number %i is %i\n", number, triangularNumber);

getch();
return 0;
}

Program 5.5

#include <stdio.h>
#include <conio.h>

int main (void)


{
int n, number, triangularNumber, counter;

counter = 1;
while ( counter <= 5) {
printf ("What triangular number do you want? ");
scanf ("%i", &number);

triangularNumber = 0;

n = 1;

while ( n <= number ) {


triangularNumber += n;
++n;
}

printf ("Triangular number %i is %i\n\n", number, triangularNumber);


++counter;
}

getch();
return 0;
}

Answer 10 - Typing a negative number into program 5.8 results in 0.

Answer 11

#include <stdio.h>
#include <conio.h>

int main (void)


{
int number, right_digit, total = 0;

printf ("Enter a number and I will add its digits!\n\n");


scanf ("%i", &number);

do {
right_digit = number % 10;
total = right_digit + total;
number = number / 10;
}
while ( number != 0 );

printf ("\nThe sum of your digits is %i\n", total);

getch();
return 0;
}
Chapter 6

(note - a few example programs throughout the chapter use "operator" as a variable name, however
my compiler reads "operator" as a reserved function. To work around that, I instead used "o" as a
replacement variable name throughout the chapter)

Answer 1 - N/A (Type in, compile and run each example program in the chapter.)

Answer 2 -

#include <stdio.h>
#include <conio.h>

int main (void)


{
int value1, value2,;
int rem;

printf("Test to determine if the first number is evenly divisible into the second.\n\n");

printf ("Type in the first number, then press enter: ");


scanf ("%i", &value1);

printf ("\nType in the second number, then press enter: ");


scanf ("%i", &value2);

rem = value2 % value1;

if ( rem ==0 )
printf ("\n%i is evenly divisible by %i", value1, value2);
else
printf ("\n%i is NOT evenly divisible by %i", value1, value2);

printf ("\n");

getch ();
return 0;
}

Answer 3 -

#include <stdio.h>
#include <conio.h>

int main (void)


{
int value1, value2,;

printf("Test that divides the first number by the second.\n\n");

printf ("Type in the first number, then press enter: ");


scanf ("%i", &value1);

printf ("\nType in the second number, then press enter: ");


scanf ("%i", &value2);

if (value2 != 0)
printf ("\n%i divided by %i = %.3f ", value1, value2, (float) value1 / value2);
else
printf ("\nCANNOT DIVIDE BY ZERO");

getch ();
return 0;
}

Answer 4 -

(note - This exercise states to use "E" as the end program operator. One problem with this is that
using E after a number also initiates the exponent variable handler. To make sure that "E" properly
triggers it's respective function and closes the program, either the spacebar or enter key must be
entered between the number and the "E" to separate the number from the "E")

#include <stdio.h>
#include <conio.h>

int main (void)


{
int loop;
float value, sum = 0;
char o;

printf ("Simple Printing Calculator Program\n\n");


printf ("KEY: + Add - Subtract * Multiply / Divide\n");
printf (" S-Set Accumulator E-End Program\n\n");
printf ("Enter a number and an operator, then press enter\n\n");
printf ("Type in your expression: ");

for ( loop=0 ; loop<=1000; ++loop){

scanf ("%f %c", &value, &o);

if ( o == '+' ){
sum = sum + value;
printf ("\n%.2f\n",sum);
}

else if ( o == '-' ){
sum = sum - value;
printf ("\n%.2f\n",sum);
}

else if ( o == '*' ){
sum = sum * value;
printf ("\n%.2f\n",sum);
}

else if ( o == 'S' ){
sum = value;
printf ("\n%.2f\n",sum);
}

else if ( o == 'E' ){
printf ("\nFinal Value: %.2f\n\nGoodbye!",sum);
loop = loop + 1000;
}

else if ( o == '/' )
if ( value == 0 ){
loop = loop + 1000;
printf ("\nDivision by zero.\n");
}
else{
sum = sum / value;
printf ("\n%.2f\n", sum);
}
else{
loop = loop + 1000;
printf ("\nUnknown operator.\n");
}

}
getch ();
return 0;
}

Answer 5

#include <stdio.h>
#include <conio.h>

int main (void)


{
int flag, number, right_digit;

flag = 0;

printf ("Enter your number.\n");


scanf ("%i", &number);

if (number < 0){


number = -number;
flag = 1;
}
do {
right_digit = number % 10;
printf ("%i", right_digit);
number = number / 10;
}

while ( number != 0 );

if (flag != 1)
printf ("\n");
else
printf ("-\n");

getch();
return 0;
}

Answer 6 -

#include <stdio.h>
#include <conio.h>

int main (void)


{
int number, flipper, left_digit, pointer;

printf("Number to Word Generator\n\n");


printf("Enter a number (8-length max) and I will speak it for you!\n\n");
printf ("Enter your number.\n\n");

scanf ("%i", &number);


printf ("\n");

if ( number == 0)
printf ("Zero");

// Set Specefic Loop Maximum Limit at 8 digits


if ( number <=99999999 && number >= -99999999 ){
// Set negative entries to absolute value
if ( number < 0){
number = -number;
printf ("Negative ");
}

//Pointer Doubles as Loop counter and digit identifier


//Locates digits by using remainder calculation
for ( pointer=100000000 ; pointer !=1 ; pointer = pointer / 10){

//Starting with max possible (8th) character,


//If statement focuses the pointer onto the proper digit length.
//If 'number' variable length is less than required pointer
//value, pointer is divided by 10 to check 7th digit, then
//6th, and so on until proper length is found.
if (pointer <= (number*10)){
//the remainder must be reduced to a single digit
left_digit = (number % pointer)/(pointer/10);

switch (left_digit)
{
case 1:
printf ("One ");
break;
case 2:
printf ("Two ");
break;
case 3:
printf ("Three ");
break;
case 4:
printf ("Four ");
break;
case 5:
printf ("Five ");
break;
case 6:
printf ("Six ");
break;
case 7:
printf ("Seven ");
break;
case 8:
printf ("Eight ");
break;
case 9:
printf ("Nine ");
break;
case 0:
printf ("Zero ");
break;

default:
printf ("Unknown operator.\n");
break;
}
}
}
}
else
printf ("Number is not within range!!");

getch();
return 0;
}

Answer 7 - (note - I wasn't exactly sure what the author is asking here. I think my modification in
part 1 of the exercise cramps the effect of part 2. In other words, my adjustments to both for loops
in repsonse to part 1 do satisfy that argument, but in such a way that renders the argument in part 2
to be imparied. Therefor, after hours of expiramentation, I decided to skip the second request as I
believe my initial adjustments do well to simplify the process while retaining the same result.)

#include <stdio.h>
#include <conio.h>
#include <stdbool.h>

int main (void)


{
int p, d = 3;
bool isPrime;

printf ("2 \n\n");

for ( p = 3; p <= 50; p = p + 2 ) {

isPrime = true;

for (d = 3; d < p; d = d + 2)

if ( p % d == 0 )
isPrime = false;

if ( isPrime != false )
printf ("%i \n", p);
}
printf ("\n");

getch ();
return 0;
}

Potrebbero piacerti anche