By GokiSoft.com| 20:05 13/09/2023|
Lập Trình C

[Share Code] Tìm hiểu if, else, switch, while trong C - C2307L

#main.c

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	//Bai 1:
	int a;
	printf("\nNhap a = ");
	scanf("%d", &a);
	printf("\na^2 = %d, a^3 = %d, a^4 = %d", a * a, a * a * a, a * a * a * a);
	
	//Bai 2
	int day, month, year;
	printf("\nNhap ngay(dd): ");
	scanf("%d", &day);
	printf("\nNhap thang(mm): ");
	scanf("%d", &month);
	printf("\nNhap nam(yyyy): ");
	scanf("%d", &year);
	
	printf("\nKet qua: %d/%d/%d", day, month, year);
	
	//Bai 3
	float x, y;
	printf("\nNhap x = ");
	scanf("%f", &x);
	printf("\nNhap y = ");
	scanf("%f", &y);
	printf("\nx + y = %f", x + y);
	printf("\nx - y = %f", x - y);
	printf("\nx / y = %f", x / y);
	printf("\nx * y = %f", x * y);
	
	return 0;
}

#main.c

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	/**
	int num;
	printf("\nNhap num = ");
	scanf("%d", &num);
	int r;
	r = num % 2;
	
	if(r == 0) {
		//Neu dung -> xu ly code o day
		printf("\nNumber is even");
	} else {
		//Neu bieu thuc if ma sai -> chay vao else
		printf("\nNumber is odd");
	}
//	if(r != 0) {
//		printf("\nNumber is odd");
//	}
	
	//Vi du 2
	int yearsWithUs, bizDone;
	printf("\nNhap YearsWithUs = ");
	scanf("%d", &yearsWithUs);
	printf("\nNhap bizDone = ");
	scanf("%d", &bizDone);
	
	if(yearsWithUs >= 10) {
		//Yes
		if(bizDone > 5000000) {
			printf("\nClassified as an MVS");
		} else {
			printf("\nA little more effort required");
		}
	} else {
		printf("\nA little more effort required");
	}
	
	//Vi du 3:
//	int cnt = 0;
//	if(cnt < 1000) {
//		printf("\nScooby");
//		cnt++;
//		
//		if(cnt < 1000) {
//			printf("\nScooby");
//			cnt++;
//			
//			if(cnt < 1000) {
//				printf("\nScooby");
//				cnt++;
//			}
//		}
//	}

	int x;
	printf("\nNhap x = ");
	scanf("%d", &x);
	
	if(x % 7 == 0) {
		printf("\nX chia het cho 7");
	} else {
		if(x % 2 == 0) {
			printf("\nX la so chan");
		} else {
			printf("\nX la so le");
		}
	}
	
	//Chu y: x = 14 -> thi xu ly dong lenh nao???
	if(x % 7 == 0) {
		printf("\nX chia het cho 7");
	} else if(x % 2 == 0) {
		printf("\nX la so chan");
	} else {
		printf("\nX la so le");
	}
	
	//Vi du:
	//Nhap y -> ky tu
	//		y = 'A' -> Hello AA
	//		y = 'B' -> Hello BB
	//		y = 'C' -> Hello CC
	//		khac 	-> Hello OKOK
	char y;
	printf("\nNhap y = ");
	fflush(stdin); fflush(stdout);
	scanf("%c", &y);
	
//	if(y == 'A') {
//		printf("\nHello AA");
//	} else if(y == 'B') {
//		printf("\nHello BB");
//	} else if(y == 'C') {
//		printf("\nHello CC");
//	} else {
//		printf("\nHello OKOK");
//	}
	
	//Trong TH: y -> so nguyen (int, short int, long int, ...), char
	//Voi TH nhu tren chung ta co the su dung cach sau
//	switch(y) {
//		case 'A':
//			printf("\nHello AA");
//			break;
//		case 'B':
//			printf("\nHello BB");
//			break;
//		case 'C':
//			printf("\nHello CC");
//			break;
//		default:
//			printf("\nHello OKOK");
//			break;
//	}
	
	//Luu y: break -> sau tu case
	switch(y) {
		case 'A':
			printf("\nHello AA");
		case 'B':
			printf("\nHello BB");
		case 'C':
			printf("\nHello CC");
		default:
			printf("\nHello OKOK");
	}
	*/
//	int cnt = 0;
//	
//	while(cnt < 2) {
//		printf("\nScooby");
//		cnt+=10;
//	}
//	printf("\ncnt = %d", cnt);
	
//	int k = 0;
//	while(k < 10) {
//		printf("\nXin chao");
//		k++;
//	}
//	printf("\nK = %d", k);
	
//	do {
//		printf("\nXin chao");
//		k++;
//	} while(k < 10);
//	printf("\nK = %d", k);

	int k = 0;
	while(1) {
		printf("\nOKOK");
		k++;
		if(k % 3 == 0) { 
			k = 0;
		}
		if(k % 3 == 0) {
			break;
		}
	}
	
	return 0;
}



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó