chapter5
This commit is contained in:
parent
991478f774
commit
785c2b0e50
23
DeitelC/Chapter5/currencyConversion.c
Normal file
23
DeitelC/Chapter5/currencyConversion.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void usdToEuro(double);
|
||||||
|
void usdToYen(double);
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
double usd = 1;
|
||||||
|
usdToEuro(usd);
|
||||||
|
usdToYen(usd);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void usdToEuro(double usd) {
|
||||||
|
double euro = usd * 0.92;
|
||||||
|
printf("%3s\t%s\n", "USD", "EURO");
|
||||||
|
printf("%3.2f\t%3.2f\n", usd, euro);
|
||||||
|
}
|
||||||
|
|
||||||
|
void usdToYen(double usd) {
|
||||||
|
double yen = usd * 118.87;
|
||||||
|
printf("%3s\t%s\n", "USD", "YEN");
|
||||||
|
printf("%3.2f\t%3.2f\n", usd, yen);
|
||||||
|
}
|
16
DeitelC/Chapter5/hypotenuse.c
Normal file
16
DeitelC/Chapter5/hypotenuse.c
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
double hypotenuse (double side1, double side2);
|
||||||
|
|
||||||
|
int main (void) {
|
||||||
|
double side1, side2;
|
||||||
|
printf("Enter side 1 and side 2\n");
|
||||||
|
scanf("%lf%lf", &side1, &side2);
|
||||||
|
printf("%3s%3.2lf\n", "Hypotenuse: ", hypotenuse(side1, side2));
|
||||||
|
}
|
||||||
|
|
||||||
|
double hypotenuse (double side1, double side2) {
|
||||||
|
double hypo = (side1 * side1) + (side2 * side2);
|
||||||
|
return sqrt(hypo);
|
||||||
|
}
|
11
DeitelC/Chapter5/isEven.c
Normal file
11
DeitelC/Chapter5/isEven.c
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int isEven (int num);
|
||||||
|
|
||||||
|
int isEven (int num) {
|
||||||
|
if (num % 2 == 0) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
7
DeitelC/Chapter5/isEvenCheck.c
Normal file
7
DeitelC/Chapter5/isEvenCheck.c
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "isEven.c"
|
||||||
|
|
||||||
|
int main (void) {
|
||||||
|
int a = isEven(2);
|
||||||
|
printf("%d\n", a);
|
||||||
|
}
|
14
DeitelC/Chapter5/randExample.c
Normal file
14
DeitelC/Chapter5/randExample.c
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main (void) {
|
||||||
|
printf("%d\n", 1 + rand() % 6);
|
||||||
|
printf("%d\n", 100 + rand() % 900);
|
||||||
|
printf("%d\n", rand() % 19);
|
||||||
|
printf("%d\n", 1000 + rand() % 1222);
|
||||||
|
printf("%d\n", -1 + rand() % 2);
|
||||||
|
printf("%d\n", -3 + rand() % 15);
|
||||||
|
printf("%d\n", 3 * (1 + rand() % 10));
|
||||||
|
printf("%d\n", 3 + 2 * (rand() % 9));
|
||||||
|
printf("%d\n", 3 + 5 * (rand() % 7));
|
||||||
|
}
|
13
DeitelC/Chapter5/rectangleOfAnyChar.c
Normal file
13
DeitelC/Chapter5/rectangleOfAnyChar.c
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void rectangleOfAnyChar (int side1, int side2, char a);
|
||||||
|
|
||||||
|
|
||||||
|
void rectangleOfAnyChar (int side1, int side2, char a) {
|
||||||
|
for(int i = 0; i < side1; i++) {
|
||||||
|
for(int j = 0; j < side2; j++) {
|
||||||
|
printf("%c", a);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
13
DeitelC/Chapter5/rectangleOfAsteriks.c
Normal file
13
DeitelC/Chapter5/rectangleOfAsteriks.c
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void rectangleOfAsteriks(int side1, int side2);
|
||||||
|
|
||||||
|
void rectangleOfAsteriks(int side1, int side2)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < side1; i++) {
|
||||||
|
for (int j = 0; j < side2; j++) {
|
||||||
|
printf("*");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
13
DeitelC/Chapter5/rectangleOfAsteriks.c~
Normal file
13
DeitelC/Chapter5/rectangleOfAsteriks.c~
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void rectangleOfAsteriks (int side1, int side2);
|
||||||
|
|
||||||
|
|
||||||
|
void rectangleOfAsteriks (int side1, int side2) {
|
||||||
|
for(int i = 0; i < side1; i++) {
|
||||||
|
for(int j = 0; j < side2; j++) {
|
||||||
|
printf("*");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
6
DeitelC/Chapter5/rectangleOfAsteriksDemo.c
Normal file
6
DeitelC/Chapter5/rectangleOfAsteriksDemo.c
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "rectangleOfAsteriks.c"
|
||||||
|
|
||||||
|
int main (void) {
|
||||||
|
rectangleOfAsteriks(4,5);
|
||||||
|
}
|
6
DeitelC/Chapter5/rectangleOfDemo.c
Normal file
6
DeitelC/Chapter5/rectangleOfDemo.c
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "rectangleOfAnyChar.c"
|
||||||
|
|
||||||
|
int main (void) {
|
||||||
|
rectangleOfAnyChar(4,5,'@');
|
||||||
|
}
|
@ -1,10 +1,38 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
|
int roundToInteger(double number);
|
||||||
|
int roundToTenths(double number);
|
||||||
|
int roundToHundreths(double number);
|
||||||
|
int roundToThousandths(double number);
|
||||||
|
|
||||||
|
|
||||||
int main (void) {
|
int main (void) {
|
||||||
double num;
|
double num;
|
||||||
printf("%s\n", "Enter a number!");
|
printf("%s\n", "Enter a number!");
|
||||||
scanf("%lf", &num);
|
scanf("%lf", &num);
|
||||||
int y = floor(num + .5);
|
printf("%3s\t%3lf\t%3s\t%3d\n", "Orginal num:", num, "Rounded: ", roundToInteger(num));
|
||||||
printf("%3s\t%3lf\t%3s\t%3d\n", "Orginal num:", num, "Rounded: ", y);
|
printf("%3s\t%3lf\t%3s\t%3d\n", "Orginal num:", num, "Rounded: ", roundToTenths(num));
|
||||||
|
printf("%3s\t%3lf\t%3s\t%3d\n", "Orginal num:", num, "Rounded: ", roundToHundreths(num));
|
||||||
|
printf("%3s\t%3lf\t%3s\t%3d\n", "Orginal num:", num, "Rounded: ", roundToThousandths(num));
|
||||||
|
}
|
||||||
|
|
||||||
|
int roundToInteger(double number) {
|
||||||
|
int y = floor(number + .5);
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
int roundToTenths(double number) {
|
||||||
|
int y = floor(number * 10 + .5) / 10;
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
int roundToHundreths(double number) {
|
||||||
|
int y = floor(number * 100 + .5) / 100;
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
int roundToThousandths(double number) {
|
||||||
|
int y = floor(number * 1000 + .5) / 1000;
|
||||||
|
return y;
|
||||||
}
|
}
|
||||||
|
23
DeitelC/Chapter5/separatingDigits.c
Normal file
23
DeitelC/Chapter5/separatingDigits.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
puts("Enter integer");
|
||||||
|
int scanV;
|
||||||
|
scanf("%d", &scanV);
|
||||||
|
int rem = 10000;
|
||||||
|
int firstD = 1;
|
||||||
|
|
||||||
|
if(scanV < 32768 && scanV >= 1) {
|
||||||
|
while(rem >= 1) {
|
||||||
|
int digit = scanV / rem;
|
||||||
|
if(!firstD || digit) {
|
||||||
|
printf("%d ", digit);
|
||||||
|
firstD = 0;
|
||||||
|
scanV = scanV % rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
rem /= 10;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
24
DeitelC/Chapter5/sidesOfTriangle.c
Normal file
24
DeitelC/Chapter5/sidesOfTriangle.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
double sidesOfTriangle (double s1, double s2, double s3);
|
||||||
|
|
||||||
|
int main (void) {
|
||||||
|
double s1, s2, s3;
|
||||||
|
printf("%s\n","Enter sides of triangle");
|
||||||
|
scanf("%lf%lf%lf", &s1, &s2, &s3);
|
||||||
|
double area = sidesOfTriangle(s1, s2, s3);
|
||||||
|
if(area >= 0) {
|
||||||
|
printf("%3s%lf\n", "Area: ", area);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
double sidesOfTriangle (double s1, double s2, double s3) {
|
||||||
|
double perimeter = s1 + s2 + s3;
|
||||||
|
double u = perimeter / 2;
|
||||||
|
if ((s1 < s2 + s3) && (s2 < s1 + s3) && (s3 < s1 + s2)) {
|
||||||
|
return sqrt(u * (u - s1) * (u - s2) * (u - s3));
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
26
DeitelC/Chapter5/struck12.c
Normal file
26
DeitelC/Chapter5/struck12.c
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void struck12(int, int, int);
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
puts("Enter time in 3 integer!(H,M,Sec)");
|
||||||
|
int time1, time2, time3 = 0;
|
||||||
|
|
||||||
|
scanf("%d%d%d", &time1, &time2, &time3);
|
||||||
|
struck12(time1, time2, time3);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void struck12(int i1, int i2, int i3) {
|
||||||
|
int total = (i1*3600) + (i2*60) + i3;
|
||||||
|
|
||||||
|
if(total > 43200) {
|
||||||
|
total -= 43200;
|
||||||
|
}
|
||||||
|
int rem = 43200 - total;
|
||||||
|
int sec = rem % 60;
|
||||||
|
int h = rem / 3600;
|
||||||
|
int m = (rem - (h * 3600) - sec) / 60;
|
||||||
|
printf("%s%d%s%d%s%d%s\n", "Struck 12: ", h, " Hours, ", m, " Min, ", sec, " Secs left. ");
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user