EX01

EX01

#include <stdio.h>

int main(int argc, char const *argv[]) {
	printf("This is my first program!");
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	printf("This is my second program!");
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	printf("1 ");
	printf("2 ");
	printf("3 ");
	printf("4 ");
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	printf("1 2 3 4 ");
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	printf("%d %d %d %d", 1, 2, 3, 4);
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	printf("The number of minutes in 15 days is %d.", 15 * 24 * 60);
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	printf("180 minutes equal to %d hours.", 180 / 60);
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	printf("174 minutes: %d hours and %d minutes", 174 / 60, 174 % 60);
	return 0;
}

EX02

EX02

#include <stdio.h>

int main(int argc, char const *argv[]) {
	printf("Net price is 889 $, sales tax rate is 6%.\nSo the total purchase price is %f $.", 889 * 1.06);
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	float radius = 0, pi = 3.14159;
	printf("Please input radius:\n");
	scanf("%f", &radius);
	printf("The diameter of this circle is %f\n", radius * 2);
	printf("The circumference of this circle is %f\n", 2 * pi * radius);
	printf("The area of this circle is %f\n", pi * radius * radius);
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	float startingBalance, interestrate;
	printf("Interest calculation programnStarting balance ?\n");
	scanf("%f", &startingBalance);
	printf("Annual interest rate percntage ?\n");
	scanf("%f", &interestrate);
	printf("Balance after one year: %f", startingBalance * (1 + interestrate / 100));
	return 0;
}

EX03

EX03

#include <stdio.h>

int main(int argc, char const *argv[]) {
	int grade = 0;
	printf("Input student's grade:\n");
	scanf("%d", &grade);
	if (grade >= 0 && grade < 60) {
		printf("Failed!");
	} else if (grade >= 60 && grade <= 100) {
		printf("Passed!");
	} else {
		printf("Error!");
	}
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	int num = 0;
	printf("Please input an integer number:\n");
	scanf("%d", &num);
	if (num < 0) {
		num = -num;
	}
	if (num % 2 == 1) {
		printf("Odd number!");
	} else {
		printf("Even number!");
	}
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	int a = 0, b = 0, c = 0;
	printf("Input three integer numbers.\nThe first:\n");
	scanf("%d", &a);
	printf("The second:\n");
	scanf("%d", &b);
	printf("The third:\n");
	scanf("%d", &c);
	a = a > b ? a : b;
	a = a > c ? a : c;
	printf("The largest number is %d", a);
	return 0;
}

EX04

EX04

#include <stdio.h>

int main(int argc, char const *argv[]) {
	int day, month, year;
	printf("This program help you to transfer date.\n");
	while (true) {
		printf("Please input date: (For example: 1996 08 16)\n");
		scanf("%d %d %d", &year, &month, &day);
		if (year < 0 || month < 0 || month >12 || day < 0 || day >31) {
			printf("You have input an wrong date!\n");
			continue;
		}
		if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) {
			printf("You have input an wrong date!\n");
			continue;
		}
		if (month == 2) {
			if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
				if (day > 29) {
					printf("You have input an wrong date!\n");
					continue;
				}
			}
			if (day > 28) {
				printf("You have input an wrong date!\n");
				continue;
			}
		}
		break;
	}
	switch (day) {
		case 1:
		case 21:
		case 31:
			printf("%dst ", day);
			break;
		case 2:
		case 22:
			printf("%dnd ", day);
			break;
		case 3:
		case 23:
			printf("%drd ", day);
			break;
		default:
			printf("%dth ", day);
	}
	switch (month) {
		case 1:
			printf("January ");
			break;
		case 2:
			printf("February ");
			break;
		case 3:
			printf("March ");
			break;
		case 4:
			printf("April ");
			break;
		case 5:
			printf("May ");
			break;
		case 6:
			printf("June ");
			break;
		case 7:
			printf("July ");
			break;
		case 8:
			printf("August ");
			break;
		case 9:
			printf("September ");
			break;
		case 10:
			printf("October ");
			break;
		case 11:
			printf("Novenber ");
			break;
		case 12:
			printf("December ");
	}
	printf("%d", year);
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	int total = 0, i = 2;
	printf("This program calculates 2+4+6+8+...+30.\n");
	while (i <= 30) {
		total += i;
		i += 2;
	}
	printf("The total is %d\n", total);
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	float sales = 0;
	while (true) {
		printf("Enter sales in pound(enter -1 to end):\n");
		scanf("%f", &sales);
		if(sales < 0) {
			break;
		} else {
			printf("Salary is %.2f\n", 200 + sales * 0.09);
		}
	}
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	int pronum, quantity;
	float value;
	printf("Product Number    Retail Price(in pounds)\n             1    2.98\n             2    4.50\n             3    9.98\n             4    4.49\n             5    6.87\n");
	while (true) {
		printf("Please type in Product number and the quantity sold for one day: (-1 to end)\n");
		scanf("%d", &pronum);
		if (pronum < 0) {
			break;
		}
		scanf("%d", &quantity);
		switch (pronum) {
			case 1:
				value += 2.98 * quantity;
				break;
			case 2:
				value += 4.50 * quantity;
				break;
			case 3:
				value += 9.98 * quantity;
				break;
			case 4:
				value += 4.49 * quantity;
				break;
			case 5:
				value += 6.87 * quantity;
				break;
			default:
				printf("You have typed in wrong data!\n");
			}
	}
	printf("Total is %f", value);
	return 0;
}

EX05

EX05

#include <stdio.h>
#include <math.h>

int main(int argc, char const *argv[]) {
	int i = 1;
	do {
		printf("%4.0f\n", pow(2, i));
		i++;
	} while(i < 11);
	return 0;
}
#include <stdio.h>
#include <math.h>

int main(int argc, char const *argv[]) {
	for (int i = 0; i < 11; i++) {
		printf("%4.0f\n", pow(2, i));
	}
	return 0;
}
#include <stdio.h>
#include <math.h>

int main(int argc, char const *argv[]) {
	int num = 0, odd = 0, i = 0;
	double pi = 0;
	printf("This program calculates constant π by the infinite series:nπ=4-4/3+4/5-4/7+4/9-4/11...\nHow many terms do you want to calculate?\n");
	scanf("%d", &num);
	do {
		odd = 2 * i + 1;
		pi = pi + 4 / (pow(-1, i) * odd);
		i++;
	} while(i < num);
	printf("Result is %f\n", pi);
	return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[]) {
	for (int i = 4; abs(i) <= 4; i--) {
		for (int j = 1; j <= abs(i); j++) {
			printf(" ");
		}
		for (int j = 1; j <= 9 - abs(i) * 2; j++) {
			printf("*");
		}
		printf("\n");
	}
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	for (int i = 1; i < 10; i++) {
		for (int j = 1; j <= i; j++) {
			printf("%d*%d=%2d", i, j, i * j);
		}
		printf("\n");
	}
	return 0;
}

EX06

EX06

#include <stdio.h>

void main(int argc, char const *argv[]) {
	int iNter = 0, round_to_nearest(float);
	float num = 0;
	while (true) {
		printf("This program will round the number you have input to the nearest integer.(Input 0 to exit)\nPlease input:\n");
		scanf("%f", &num);
		if (num == 0) {
			printf("Bye!");
			exit(0);
		} else if (num < 0) {
			num = -num;
			iNter = round_to_nearest(num);
			printf("%d\n", -iNter);
		} else {
			iNter = round_to_nearest(num);
			printf("%d\n", iNter);
		}
	}
}

int round_to_nearest(float n) {
	return (int) (n + 0.5);
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	float larger(float, float);
	float a, b, c, d, e, f, total;
	printf("This program will ask you for three pairs of numbers, and will help you adds the larger of each pair.\nPlease input the first pair:(e.g. 9.99 10.01)\n");
	scanf("%f %f", &a, &b);
	printf("The second pair:\n");
	scanf("%f %f", &c, &d);
	printf("The third pair:\n");
	scanf("%f %f", &e, &f);
	total = larger(a, b) + larger(c, d) + larger(e, f);
	printf("Total is %f", total);
	return 0;
}

float larger(float x, float y){
	return (x > y ? x : y);
}
#include <stdio.h>
#include <math.h>

int main(int argc, char const *argv[]) {
	float time1, time2, time3, fee1, fee2, fee3, calcCharge(float);
	printf("This program help you calculate the parking charges for each of 3 customers.\nPlease input how many hours the first customer have parked car:(hours)\n");
	scanf("%f", &time1);
	printf("The second:\n");
	scanf("%f", &time2);
	printf("The third:\n");
	scanf("%f", &time3);
	if (time1 <= 0 || time2 <= 0 || time3 <= 0 || time1 >24 || time2 > 24 || time3 > 24) {
		printf("You have input wrong data! Please open this program again.\n");
	} else {
		fee1 = calcCharge(time1);
		fee2 = calcCharge(time2);
		fee3 = calcCharge(time3);
		printf("Car   Hours Charge\n");
		printf("1%10.1f%7.2f\n", time1, fee1);
		printf("2%10.1f%7.2f\n", time2, fee2);
		printf("3%10.1f%7.2f\n", time3, fee3);
		printf("TOTAL%6.1f%7.2f", time1 + time2 + time3, fee1 + fee2 + fee3);
	}
	return 0;
}

float calcCharge(float hours) {
	float charge;
	hours = (float) ceil(hours);
	charge = (hours + 1) * 0.5;
	if (charge < 2) {
		return 2;
	}
	if (charge > 10) {
		return 10;
	}
	return charge;
}

EX07

EX07

#include <stdio.h>

int main(int argc, char const *argv[]) {
	int num, evenNum(int);
	printf("Please input an integer: (Crtl+Z to stop)\n");
	while (scanf("%d", &num) != EOF) {
		if (evenNum(num)) {
			printf("%d is an even number!\n", num);
		}
		printf("Next integer:\n");
	}
	return 0;
}

int evenNum(int a) {
	return (a + 1) % 2;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	int a ,b, integerPower1(int, int);
	printf("Please input base and exponent:\n");
	scanf("%d %d", &a, &b);
	printf("Result is %d", integerPower1(a, b));
	return 0;
}

int integerPower1(int a, int b) {
	int sum = 1;
	for (int i = 1; i <= b; i++) {
		sum *= a;
	}
	return sum;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	int a, b, integerPower2(int, int);
	printf("Please input base and exponent:\n");
	scanf("%d %d", &a, &b);
	printf("Result is %d", integerPower2(a, b));
	return 0;
}

int integerPower2(int m, int n) {
	if (!n) {
		return 1;
	} else {
		return m * integerPower2(m, n - 1);
	}
}

EX08

EX08

#include <stdio.h>

int main(int argc, char const *argv[]) {
	int num[10];
	float avg = 0;
	for (int i = 0; i < 10; i++) {
		printf("Please input an integer:\n");
		scanf("%d", &num[i]);
	}
	for (int i = 0; i < 10; i++) {
		avg += num[i];
	}
	printf("Average is %f", avg / 10);
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	int a[2][5], max1, max2, min1, min2, max, min;
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 5; j++) {
			scanf("%d", &a[i][j]);
		}
	}
	max1 = min1 = a[0][0];
	for (int j = 0; j < 5; j++) {
		max1 = max1 > a[0][j] ? max1 : a[0][j];
		min1 = min1 < a[0][j] ? min1 : a[0][j];
	}
	max2  = min2 = a[1][0];
	for (int j = 0; j < 5; j++) {
		max2 = max2 > a[1][j] ? max2 : a[1][j];
		min2 = min2 < a[1][j] ? min2 : a[1][j];
	}
	max = max1 > max2 ? max1 : max2;
	min = min1 < min2 ? min1 : min2;
	printf("The maximum in row 1 is %d\n", max1);
	printf("The minimun in row i is %d\n", min1);
	printf("The maximum in row 2 is %d\n", max2);
	printf("The minimun in row 2 is %d\n", min2);
	printf("The maximun is %dn", max);
	printf("The minimun is %dn", min);
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	int i = 0, number[20];
	do {
		printf("Please type in an integer between 10 and 100 inclusive:\n");
		scanf("%d", &number[i]);
		if (number[i] >= 10 && number[i] <= 100) {
			if (i == 0) {
				i++;
				continue;
			}
			for (int j = 0; j < i; j++) {
				if (number[i] == number[j]) {
					printf("Same number: %d\n", number[i]);
					break;
				}
			}
		} else {
			continue;
		}
		i++;
	} while(i < 11);
	return 0;
}

EX09

EX09

#include <stdio.h>

int main(int argc, char const *argv[]) {
	int dist[5];
	char towns[5][81], linein[81];
	for (int i = 0; i < 5; i++) {
		printf("Please type in the town's name and its distance to London:\n");
		gets(linein);
		sscanf(linein, "%s %d", towns[i], &dist[i]);
	}
	for (int i = 0; i < 5; i++) {
		if (dist[i] < 100) {
			printf("%s to London is less than 100 miles.\n", towns[i]);
		}
	}
	return 0;
}
#include <stdio.h>
#include <string.h>

int main(int argc, char const *argv[]) {
	int eofs;
	char linein[81];
	printf("Type in a string:\n");
	gets(linein);
	for (int i = 0; linein[i] != '\0'; i++) {
		eofs = i - 1;
	}
	for (int i = eofs; i >= 0; i--) {
		if (linein[i] == ' ') {
			for (int j = i + 1; linein[j] != '\0' && linein[j] != ' '; j++) {
				printf("%c", linein[j]);
			}
			printf(" ");
		}
	}
	for (int i = 0; linein[i] != '\0' && linein[i] != ' '; i++) {
		printf("%c", linein[i]);
	}
	printf("\n");
	for (int i = 0; linein[i] != '\0'; i++) {
		if (linein[i] == ' ') {
			for (int j = i - 1; j >= 0 && linein[j] != ' '; j--) {
				printf("%c", linein[j]);
			}
			printf(" ");
		}
	}
	for (int j = i - 1; j >= 0 && linein[j] != ' '; j--) {
		printf("%c", linein[j]);
	}
	printf("\n");
	for (int i = eofs; i >= 0; i--) {
		printf("%c", linein[i]);
	}
	return 0;
}

EX10

EX10

#include <stdio.h>

int main(int argc, char const *argv[]) {
	int num[5], *p;
	for (int i = 0; i < 5; i++) {
		printf("Please type in an integer:\n");
		scanf("%d", &num[i]);
	}
	printf("First:\n");
	for (int i = 4; i >= 0; i--) {
		printf("%d\n", num[i]);
	}
	printf("Second:\n");
	for (int i = 0; i >= 0; i--) {
		printf("%d\n", *(num + i));
	}
	p = num;
	printf("Third:\n");
	for (int i = 0; i >= 0; i--) {
		printf("%d\n", p[i]);
	}
	printf("Fourth:\n");
	for (int i = 4; i >= 0; i--) {
		printf("%d\n", *(p + i));
	}
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	float num[8], sum;
	for (int i = 0; i < 8; i++) {
		printf("Please type in a float number:\n");
		scanf("%f", &num[i]);
	}
	for (int i = 1; i < 8; i += 2) {
		printf("%d: %f\n", i + 1, *(num + i));
	}
	for (int i = 0; i < 8; i += 2) {
		sum += *(num + i);
	}
	printf("Sum is %f", sum);
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	int num[6], maxmin(int[], int);
	for (int i = 0; i < 6; i++) {
		printf("Please type in an integer:\n");
		scanf("%d", &num[i]);
	}
	maxmin(num, 6);
	return 0;
}

int maxmin(int number[], int n) {
	int max, min;
	max = min = number[0];
	for (int i = 1; i < n; i++) {
		max = max > number[i] ? max : number[i];
		min = min < number[i] ? min : number[i];
	}
	printf("max = %d\nmin = %d", max, min);
	return 0;
}

EX11

EX11

#include <stdio.h>

int main(int argc, char const *argv[]) {
	float annualSalary[5], monthlySalary[5];
	char name[5][81], linein[81];
	FILE *fp;
	for (int i = 0; i < 5; i++) {
		printf("Please type in employee's name and annual salary:\n");
		gets(linein);
		sscanf(linein, "%s %f", &name[i], &annualSalary[i]);
	}
	fp = fopen("salary.txt", "a+");
	fprintf(fp, "No.	Employee's Name	Annual Salary	Monthly payment after tax\n");
	for (int i = 0; i < 5; i++) {
		monthlySalary[i] = annualSalary[i] / 12 * 0.8;
		fprintf(fp, "%2d	 %15s	%13.3f	%25.3f\n", i + 1, name[i], annualSalary[i], monthlySalary[i]);
	}
	fclose(fp);
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	int distance[20];
	char line1[20][81], linein[81];
	FILE *fp;
	for(int i = 0; i < 20; i++) {
		printf("Please type in No.%2d town's name and the distance from London:\n", i + 1);
		gets(linein);
		sscanf(linein, "%s %d", &line1[i], &distance[i]);
	}
	fp = fopen("data.txt", "a+");
	fprintf(fp, "No.	Town's Name	Distance from London\n");
	for(int i = 0; i < 20; i++) {
		fprintf(fp, "%2d	 %11s	%20d\n", i + 1, line1[i], distance[i]);
	}
	fclose(fp);
	return 0;
}
#include <stdio.h>

int main(int argc, char const *argv[]) {
	int j, distance[20];
	char line1[20][81];
	FILE *input, *output;
	input = fopen("data.txt", "r");
	output = fopen("newdata.txt", "a+");
	fseek(input, 42, SEEK_SET);
	fprintf(output, "No.	Town's Name	Distance from London\n");
	for(int i = 0; i < 20; i++) {
		fscanf(input, "%2d	 %11s	%20d", &j, &line1[i], &distance[i]);
		if(distance[i] < 100) {
			fprintf(output, "%2d	 %11s	%20d\n", i + 1, line1[i], distance[i]);
		}
	}
	fclose(input);
	fclose(output);
	return 0;
}