Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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>
getch();
return 0;
}
Answer 4 -
#include <stdio.h>
#include <conio.h>
Answer 5-
#include <stdio.h>
return 0;
}
Answer 1 - N/A (Type in, compile and run the sample programs within the chapter).
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>
C = (F - 32) / 1.8 ;
printf ("%i degrees F = %f degrees C\n",F ,C);
getch();
return 0;
}
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>
getch();
return 0;
}
#include <stdio.h>
#include <conio.h>
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);
getch();
return 0;
}
Answer 8 -
#include <stdio.h>
#include <conio.h>
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 n=1;
while ( n <= 10 ) {
printf ("%2i %3i\n", n,n*n);
++n;
}
getch();
return 0;
}
Answer 3 -
#include <stdio.h>
#include <conio.h>
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>
//header
printf ("Table That Displays First 10 Factorals\n\n");
printf (" Root Factoral\n");
printf ("=================\n");
n = 1;
printf ("\n");
getch();
return 0;
}
Answer 5-
#include <stdio.h>
#include <conio.h>
{
int n, tn;
tn=1;
getch();
return 0;
}
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>
getch();
return 0;
}
Answer 9
Program 5.2
#include <stdio.h>
#include <conio.h>
triangularNumber = 0;
n=1;
getch();
return 0;
}
Program 5.3
#include <stdio.h>
#include <conio.h>
triangularNumber = 0;
n=1;
getch();
return 0;
}
Program 5.4
#include <stdio.h>
#include <conio.h>
triangularNumber = 0;
n=1;
while ( n <= number ) {
triangularNumber += n;
++n;
}
getch();
return 0;
}
Program 5.5
#include <stdio.h>
#include <conio.h>
counter = 1;
while ( counter <= 5) {
printf ("What triangular number do you want? ");
scanf ("%i", &number);
triangularNumber = 0;
n = 1;
getch();
return 0;
}
Answer 11
#include <stdio.h>
#include <conio.h>
do {
right_digit = number % 10;
total = right_digit + total;
number = number / 10;
}
while ( number != 0 );
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>
printf("Test to determine if the first number is evenly divisible into the second.\n\n");
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>
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>
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>
flag = 0;
while ( number != 0 );
if (flag != 1)
printf ("\n");
else
printf ("-\n");
getch();
return 0;
}
Answer 6 -
#include <stdio.h>
#include <conio.h>
if ( number == 0)
printf ("Zero");
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>
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;
}