C Language Basic Programming Exercise

Keywords: Programming

1. Output odd numbers between 1 and 100

int main() {
	for (int i = 1; i <= 100; i+=2)
	{
		printf("%d ", i);
	}
	return 0;
}

2 Determine whether two arrays have the same elements

int main() {
	int arr1[] = { 1,25,53,65,55 };
	int arr2[] = { 12,5,36,4,2 };
	int sz = sizeof(arr1) / sizeof(arr1[0]);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		for (int j = 0; j < sz; j++)
		{
			if (arr1[i] == arr2[j])
			{
				printf("Eureka");
				return 0;
			}
		}
	}
	if (i == sz)
	{
		printf("Can't find");
	}
	return 0;
}

3. Solution of quadratic equation of one variable

int main() {
	//Univariate quadratic equation ax^2+bx+c = 0
	int a, b, c = 0;
	printf("Please input the quadratic equation of one variable. a b c");
	scanf("%d%d%d", &a, &b, &c);
	if (b*b-4*a*c==0)
	{
		printf("There are two equal real roots: x1=x2=%d\n", -b / (2*a));
	}
	else if(b*b-4*a*c<0)
	{
		printf("unsolvable\n");
	}
	else
	{
		printf("x1=%d,x2=%d\n", (-b + sqrt(b*b - 4 * a*c)) / (2 * a), (-b - sqrt(b*b - 4 * a*c)) / (2 * a));
	}
	system("pause");
	return 0;
}

4 Write code to demonstrate that multiple characters move from both ends to converge in the middle

int main() {
	char arr1[] = "##########s";
	char arr2[] = "Xu Zi Xin!s";
	
	int left = 0;
	int right = strlen(arr1)-1;

	while (left<=right)
	{
		arr1[left] = arr2[left];
		arr1[right] = arr2[right];
		system("cls");
		printf("%s\n", arr1);
		left++;
		right--;
		Sleep(1000);
	}
	system("pause");
	return 0;
}

 

5. Code implementation to simulate user landing scenarios, and only three landings. (Only three passwords are allowed. If the password is correct, the login will be successful. If all three passwords are entered incorrectly, the program will exit.)

int main() {
	int i = 0;
	char password[10] = { 0 };
	for (i = 0; i < 3; i++)
	{
		printf("Please enter your password:");
		scanf("%s", password);
		if (strcmp(password,"123456")==0)
		{
			break;
		}
		else
		{
			printf("password error");
		}
	}
	if (i==3)
	{
		printf("three times input password error, exit program");
	}
	else
	{
		printf("successful landing");
	}
	system("pause");
	return 0;
}

6. Exchange the contents in Array A with those in Array B (the same size as the array)

int main() {
	int arr1[] = { 1,2,3,4,5,6 };
	int arr2[] = { 5,5,5,5,5,5 };
	int sz = sizeof(arr1) / sizeof(arr1[0]);
	for (int i = 0; i < sz; i++)
	{
		int temp = arr1[i];
		arr1[i] = arr2[i];
		arr2[i] = temp;
	}
	system("pause");
	return 0;
}

7. Calculate the value of 1/1-1/2+1/3-1/4+1/5...+1/99-1/100

int main() {
	double sum = 0;
	int flag = 1;
	for (int i = 1; i <= 100; i++)
	{
		sum += ((1.0 / i) * flag);
		flag = -flag;
	}
	printf("%f", sum);
	system("pause");
	return 0;
}
int main() {
	double sum =0,temp = 0;
	int i = 0;
	for (i = 1; i <= 100; i++)
	{
		temp = pow(-1, i + 1);
		sum += (1.0 / i)*temp;
	}
	printf("%f", sum);
	system("pause");
	return 0;
}

8. Write programs to count how many times the number 9 appears in all integers from 1 to 100.

int main() {
	int i = 0;
	int count = 0;
	for (i = 0; i <= 100; i++) {
		// Ninety places are nine.     
		if (i%10==9||i/10==9)
		{
			count++;
			printf("%d ", i);
		}
	}
	printf("\ncount = %d", count);
	system("pause");
	return 0;
}

9. Exchange the positions of two numbers without using intermediate variables

int main() {
	int a = 10;
	int b = 20;
	printf("Before exchange: a=%d; b=%d", a, b);
	a = a^b;
	b = a^b;
	a = a^b;
	printf("After the exchange: a=%d; b=%d", a, b);
	system("pause");
	return 0;
}

10. Find the greatest common factor and the smallest common multiple.

int main() {
	int a, b = 0;
	scanf("%d %d", &a, &b);
	int x = a;
	int y = b;
	int temp = 0;
	while (a/b)
	{
		temp = a / b;
		a = b;
		b = temp;
	}
	printf("The greatest common factor is:%d\n", a);
	printf("The minimum common multiple is:%d\n", x*y / a);
	system("pause");
	return 0;
}

11. Output the following pattern on the screen:

    *    

   ***   

  *****

   ***

    *

int main() {
	int i = 0;
	int n = 0;
	scanf("%d", &n);//n is half as high
	//  *
	// ***
	//*****
	// ***
	//  * 
	for (i = 0; i < n; i++)
	{
		int j;
		for (j = 0; j < n - i; j++)
		{
			printf(" ");
		}
		for (j = 0; j < i*2+1; j++) {
			printf("*");
		}
		printf("\n");
	}
	for (i = 0; i < n-1; i++)
	{
		for (int j = 0; j <= i+1; j++) {
			printf(" ");
		}
		for (int j = 0; j <(n-i-1)*2-1; j++) {
			printf("*");
		}
		printf("\n");
	}
	system("pause");
	return 0;
}

12. Finding out the number of all daffodils between 0 and 999 and exporting the number of daffodils is a three-digit number whose cube is equal to the number itself.

If 153 = 1 + 5 + 3, 153 is the number of daffodils

int main() {
	int i;
	for (i = 0; i < 999; i++)
	{
		int count = 1;
	    //1. Judging the number of digits
		int temp = i;
		while (temp>9)
		{
			count++;
			temp /= 10;
		}
		int sum = 0;
		//2. Is the number of daffodils counted?
		temp = i;
		while (temp)
		{
			sum += pow(temp % 10, count);
			temp = temp / 10;
		}
		if (i == sum)
		{
			printf("%d ", i);
		}
	}
	system("pause");
	return 0;
}

13. Find the sum of the first five terms of Sn = a + a a + a a a + a a a a a a a + a a a a a a a, where a is a number such as 2 + 22 + 222 + 2222 + 2222222

int main() {
	int a = 0;
	int n = 0;
	scanf("%d %d", &a, &n);
	int sum = 0;
	int temp = 0;
	for (int i = 0; i < n; i++)
	{
		
		temp = temp * 10 + a;
		sum += temp;
	}
	printf("%d", sum);
	system("pause");
	return 0;
}

14. Write a program that reads C source code from standard input and verifies that all curly braces appear in pairs.

int main() {
	
	char ch = 0;
	int count = 0;
	while ((ch = getchar())!= EOF)//EOF  end of file
	{
		if (ch=='{')
		{
			count++;
		}
		else if(ch == '}'&&count==0)
		{
			count--;
			break;
		}
		else if(ch=='}')
		{
			count--;
		}
	}
	if (count==0)
	{
		printf("matching\n");
	}
	else
	{
		printf("Mismatch\n");
	}
	system("pause");
	return 0;
}

15. Calculate the factorial of n

int main() {
	int n = 0;
	scanf("%d", &n);
	int i;
	int temp = 1;
	for (i = 1; i <= n; i++)
	{
		temp *= i;
	}
	printf("%d", temp);
	system("pause");
	return 0;
}

16. Calculate 1! + 2! + 3! ... +10!

int main() {
	int n = 0;
	//scanf("%d", &n);
	int sum = 0;
	int temp = 1;
	for (int i = 1; i <= 3; i++)
	{
		temp *= i;
		sum += temp;
	}
	printf("%d", sum);
	system("pause");
	return 0;
}

17. Find a specific number n in an ordered array

Write int binsearch(int x,int v[],int n);

Function: Find x in an array of V [0]<=v[1]<=[v2]<=...<=v[n-1]

int binSearch(int arr[], int x, int sz) {
	int left = 0;
	int right = sz-1;
	int mid = 0;
	while (left<=right)
	{
		mid = left + (right-left) / 2;
		if (arr[mid]>x)
		{
			right = mid - 1;
		}
		else if(arr[mid]<x)
		{
			left = mid + 1;
		}
		else
		{
			return mid;
		}
	}
	return -1;
}
int main() {
	int arr[] = { 1,2,3,4,5,6,7,8,9 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	int x = 0;
	scanf("%d", &x);
	int temp = binSearch(arr, x, sz);
	if (-1 == temp)
	{
		printf("Can't find");
	}
	else
	{
		printf("Eureka:%d", temp);
	}
	system("pause");
	return 0;
	/*int x = 5;
	int left = 0;
	int right = sz - 1;
	int mid = 0;
	while (left <= right)
	{
		mid = left + (right - left) / 2;
		if (arr[mid]>x)
		{
			right = mid - 1;
		}
		else if (arr[mid]<x)
		{
			left = mid + 1;
		}
		else
		{
			printf("Find: Subscript:% d ", mid;"
			break;
		}
	}
	if (left>right)
	{
		printf("Can't find "";
	}*/

}

18. Complete the guessing game.

#include<stdlib.h>
void menu() {
	printf("********************\n");
	printf("*******Guess the number*******\n");
	printf("***1.play  0.exit***\n");
	printf("********************\n");
}
void game() {
	int input = 0;
	int num = rand() % 100 + 1;
	printf("%d\n", num);
	while (1)
	{
		printf("Guess the number:");
		scanf("%d", &input);
		if (input>num)
		{
			printf("Guess big.\n");
		}
		else if(input<num)
		{
			printf("Guess it's small.\n");
		}
		else
		{
			printf("true\n");
			break;
		}
	}
}
int main() {
	srand(time(NULL));
	int input = 0;
	do
	{
		menu();
		printf("Please choose:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("Quit game\n");
			break;
		default:
			printf("Selection error\n");
			break;
		}
	} while (input);
	return 0;
}

19. Write a program that receives keyboard characters all the time. If it's lowercase, it outputs the corresponding uppercase letter. If it's uppercase, it outputs the corresponding lowercase letter.

int main() {
	int ch = 0;
	while ((ch = getchar()) != EOF)
	{
		if (ch >= 'A'&&ch <= 'Z')
		{
			putchar(ch + ('a' - 'A'));
		}
		if (ch >= 'a'&&ch <= 'z')
		{
			putchar(ch - ('a' - 'A'));
		}
	}
	system("pause");
	return 0;
}

20. Code implementation: find an integer stored in memory binary number of 1.

int main() {
	int num = -1;
	int i = 0;
	int count = 0;
	//for (i = 0; i < 32; i++)
	//{
	//	if (((num >> i) & 1) == 1) {
	//		count++;
	//	}
	//}
	// 13    12
	//1101 & 1100 1100
	//            1011   1000 
	//
	while (num)
	{
		num = num&(num - 1);
		count++;
	}
	printf("%d ", count);
	system("pause");
	return 0;
}

21. Code implementation: change the first 1 of 00000000 00000 00000 000000011 to 0;

int main() {
	int n = 11;//1011
	printf("%d", n&(n - 1));
	system("pause");
	return 0;
}

22. Obtain all even and odd digits in a binary sequence and output the binary sequence, respectively.

int main() {
	int n = 11;
	// 00000000 00000000 00000000 00001011
	//Odd digit
	for (int i = 30; i >= 0; i-=2)
	{
		printf("%d", (n >> i) & 1);
	}
	//Even digit
	printf("\n");
	for (int i = 31; i >= 0; i-=2)
	{
		printf("%d", (n >> i) & 1);
	}
	system("pause");
	return 0;
}

23. Output each bit of an integer

int print(int n) {
	if (n>9)
	{
		print(n/10);
	}
	printf("%d", n % 10);
}
int main() {
	int n = 1234;
	print(1234);
	system("pause");
	return 0;
}

24. Programming: How many bits are different in the binary expressions of two int (32) bit integers m and n: for example, 1997 2299 output 7

int count_one_bit(int n) {
	int count = 0;
	while (n)
	{
		count++;
		n = n&(n - 1);
	}
	return count;
}
int main() {
	int n = 2299;
	int m = 1999;
	int temp = n^m;
	printf("%d", count_one_bit(temp));
	system("pause");
	return 0;
}

25. Implement a function, print the multiplication formula table, specify the number of rows and columns of the formula table, input 9, output 9*9 formula table, output 12, output 12*12 multiplication formula table.

void print(int n) {
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			printf("%d*%d=%2d ", j, i, i*j);
		}
		printf("\n");
	}
}
int main() {
	int n = 0;
	scanf("%d", &n);
	print(n);
	system("pause");
	return 0;
}

26. Exchange of Two Numbers Using Functions

void swap(int *a, int *b) {
	int temp = *a;
	*a = *b;
	*b = temp;
}
int main() {
	int a = 10;
	int b = 20;
	swap(&a, &b);
	printf("a = %d; b = %d;", a, b);
	system("pasue");
	return 0;
}

27. Implement a function to determine whether year is a wet year or not?

int isLeapyear(int year) {
	if (((year%4==0)&&(year%100!=0))||(year%400==0))
	{
		return 1;
	}
	return 2;
}
int main() {
	int year = 0;
	printf("Please enter a year:");
	scanf("%d", &year);
	if (1 == isLeapyear(year))
	{
		printf("true");
	}
	else
	{
		printf("false");
	}
	system("pause");
	return 0;
}

28 Create an array, initialize the init() implementation function, empty the empty() empty the array, and reverse() function complete the inversion of the array.  

Requirements: Design the parameters of the function, return value

    

void init(int arr[], int sz, int temp) {
	for (int i = 0; i < sz; i++)
	{
		arr[i] = temp;
	}
}
void empty(int arr[],int sz) {
	for (int i = 0; i < sz; i++)
	{
		arr[i] = 0;
	}
}
int print(int arr[],int sz) {
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	return sz;
}
void reserve(int arr[], int sz) {
	int left = 0;
	int right = sz - 1;
	while (left<right)
	{
		int temp = arr[left];
		arr[left] = arr[right];
		arr[right] = temp;
		left++;
		right--;
	}
}
int main() {
	int arr[] = { 1,2,3,4,5,6,7,8,9 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	//init(arr,sz,1);
	reserve(arr, sz);
	print(arr, sz);
	system("pause");
	return 0;
}

29. Implement a function to determine whether a number is a prime?

int isPrime(int num) {
	for (int i = 2; i < sqrt(num); i++)
	{
		if (num%i==0)
		{
			return 0;
		}
		return 1;
	}
}
int main() {
	int num = 0;
	scanf("%d", &num);
	int ret = isPrime(num);
	if (1==ret)
	{
		printf("true");
	}
	else
	{
		printf("false");
	}
	system("pause");
	return 0;
}

30. Programming strcpy

char* mystrcpy(char *dest,const char *scr) {
	char *temp = dest;
	assert(dest != NULL);
	assert(scr != NULL);
	while (*dest++=*scr++)
	{
		;
	}
	return temp;
}
int main() {
	char arr[] = { 0 };
	
	mystrcpy(arr, "abcdef");
	system("pause");
	return 0;
}

31. Find prime numbers between 100 and 200

int main() {
	//Find prime numbers between 100 and 200
	for (int i = 101; i <= 200; i+=2)
	{
		int j = 0;
		for (j = 2; j <= sqrt(i); j++)
		{
			if (i%j==0)
			{
				break;
			}
		}
		if (j > sqrt(i))
		{
			printf("%d ", i);
		}
	}
	system("pause");
	return 0;
}

32. Print 9*9 multiplication table

int main() {
	for (int i = 1; i <= 9; i++)
	{
		for (int j = 1; j <= i; j++) {
			printf("%d * %d = %2d ", i, j, i*j);
		}
		printf("\n");
	}
	system("pause");
	return 0;
}

 

33. Leap years between 1000 and 2000

int main() {
	for (int i = 1000; i <= 2000; i++)
	{
		if ((i%4==0&&i%100!=0)||(i%400==0))
		{
			printf("%d ", i);
		}
	}
	system("pause");
	return 0;
}

 

Posted by elie on Wed, 30 Jan 2019 02:36:15 -0800