Chapt 2
2.3
1) What's it
gonna print?
answer1 is the interger 2
answer2 is the integer 5
2)
…and this time?
The remainder of 9 divided by 4 is 1
The remainder of 17 divided by 3 is
2
3)
Write a program to do this and that
/*Show off order of operations*/
#include <stdio.h>
void main(void)
{
printf("The value of 3.0 multiplied by 5.0 is
%f, which is a float.",
3.0*5.0);
printf("\nThe
value of 7.1 multiplied by 8.3 then subtract 2.2 is %f,
which is a float.",
7.1*8.3-2.2);
printf("\nThe
value of 3.2 divided by the product of
6.1 and 5 is %f
which is a
float.", 3.2/(6.1*5));
}
4) Some more of this and that
/*Show
off order of operations*/
#include <stdio.h>
void main(void)
{
printf("The value of 15 divided by 4 is %d, which is an integer.",
15/4);
printf("\nThe
remainder or modulus of 4 into 15 is %d", 15%4);
printf("\nThe
value of the product of 6 and 4 subtracted from the product of 5 and 3 is %d.",(5*3)-(6*4));
}
5)Find the errors
a) ('%d," 15)
("%d",15);
b)("%f",33);
("%d"'33);
c)("%5d"'
526.768);
("%5f", 526.768);
d)
("a b c",26,15,18);
("a b c 26 15 18");
e)
("%3.6f", 47);
("%2.1f", 47);
f)
("53.6", 526.788);
('%3.3f", 526.788);
g)
("526.768, 33, "%f %d");
("%3.3f", 526.768);
('%d", 33);
2.4
3) Declaring variables
a)int
count;
b)float
grade;
c)double
yield;
d)char
initial;
7) What does this program mean, line by line?
A&B
int num1;
This
variable is an integer (whole number)
int
num2;
This
variable is an integer (whole number)
int
total;
This
variable is an integer (whole number)
num1 =
25;
This
variable now equals 25
num2 =
35:
This
variable now equals 35
total =
num1 + num2;
This
variable adds the integers 1 and 2
Printf("The
total of %d and %d is %d\n.",num1, num2, total);
This
prints on screen "The total of 25
and 35 is 60.
9)
#include
<stdio.h>
Void main(void)
{
int
length, width, perimeter
length =
16
width =
18
perimeter
= 2*length +2*width
printf("Twice
the length, which is %d plus twice the width, %d is
the perimeter,%d.", length, width, perimeter);
}