# Chapter 1

### Exercise 1.2

```#include <stdio.h>
#include <stdlib.h>

main(){
int this_number, divisor, not_prime;
int last_prime;

this_number = 3;
last_prime = 3;

printf("1, 3 is a prime pair\n");

while(this_number < 10000){
divisor = this_number / 2;
not_prime = 0;
while(divisor > 1){
if(this_number % divisor == 0){
not_prime = 1;
divisor = 0;
}
else
divisor = divisor-1;
}

if(not_prime == 0){
if(this_number == last_prime+2)
printf("%d, %d is a prime pair\n",
last_prime, this_number);
last_prime = this_number;
}
this_number = this_number + 1;
}
exit(EXIT_SUCCESS);
}```

### Exercise 1.3

```#include <stdio.h>
#include <stdlib.h>

main(){
printf("Type in a string: ");
printf("The value was: %d\n", getnum());
exit(EXIT_SUCCESS);
}

getnum(){
int c, value;;

value = 0;
c = getchar();
while(c != '\n'){
value = 10*value + c - '0';
c = getchar();
}
return (value);
}```

### Exercise 1.4

```#include <stdio.h>
#include <stdlib.h>

/* array size */
#define NUMBER  10

main(){
int arr[NUMBER], count, lo, hi;

count = 0;
while(count < NUMBER){
printf("Type in a string: ");
arr[count] = getnum();
count = count+1;
}
lo = 0;
while(lo < NUMBER-1){
hi = lo+1;
while(hi < NUMBER){
int tmp;
if(arr[lo] > arr[hi]){
tmp = arr[lo];
arr[lo] = arr[hi];
arr[hi] = tmp;
}
hi = hi + 1;
}
lo = lo + 1;
}
/* now print them */
count = 0;
while(count < NUMBER){
printf("%d\n", arr[count]);
count = count+1;
}
exit(EXIT_SUCCESS);
}

getnum(){
int c, value;;

value = 0;
c = getchar();
while(c != '\n'){
value = 10*value + c - '0';
c = getchar();
}
return (value);
}```

### Exercise 1.5

```#include <stdio.h>
#include <stdlib.h>

/*
* To print an int in binary, hex, decimal,
* we build an array of characters and print it out
* in order.
* The values are found least significant digit first,
* and printed most significant digit first.
*/
#define NDIG    32      /* assume max no. of digits */

int getnum(void);

main(){
int val, i, count;
char chars[NDIG];

i = getnum();

/* print in binary */
val = i;
count = 0;
do{
chars[count] = val % 2;
val = val / 2;
count = count + 1;
}while(val);
count = count - 1; /* just incremented above */

while(count >= 0){
printf("%d", chars[count]);
count = count - 1;
}
printf("\n");

/* print in decimal */
val = i;
count = 0;
do{
chars[count] = val % 10;
val = val / 10;
count = count + 1;
}while(val);
count = count - 1; /* just incremented above */

while(count >= 0){
printf("%d", chars[count]);
count = count - 1;
}
printf("\n");

/* print in hex */
val = i;
count = 0;
do{
chars[count] = val % 16;
val = val / 16;
count = count + 1;
}while(val);
count = count - 1; /* just incremented above */

while(count >= 0){
if(chars[count] < 10)
printf("%d", chars[count]);
else{
/* assume 'A' - 'F' consecutive */
chars[count] = chars[count]-10+'A';
printf("%c", chars[count]);
}
count = count - 1;
}
printf("\n");
exit(EXIT_SUCCESS);
}

getnum(){
int c, value;;

value = 0;
c = getchar();
while(c != '\n'){
value = 10*value + c - '0';
c = getchar();
}
return (value);
}```