By GokiSoft.com|
21:27 23/12/2021|
Lập Trình C
[Video] Kiểm tra giữa môn - assignment - 60 phút - Khóa học lập trình C - C2110L
Kiểm tra giữa môn - assignment - 60 phút - Khóa học lập trình C
#include <stdio.h>
#include <stdlib.h>
#include <string.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 N, mod;
long int total = 0;
printf("\nNhap vao so nguyen N = ");
scanf("%d", &N);
//N = 123 -> N % 10 -> 3 + total, N = N / 10 = 12 ===> Loop
while(N > 0) {
mod = N % 10;
total += mod;
//total += N % 10;
N = N / 10;
}
printf("\nTong = %d", total);
//Bai 3:
//Giai phap -> luu so nguyen cuc to -> 3957434658734658734658347563485645896754897923648
char s[100];
int i, length, value;
printf("\nNhap so nguyen bat ky: ");
fflush(stdin);fflush(stdout);
gets(s);
length = strlen(s);
//So nguyen chia het cho 3: Tong cac so cua -> cung chia het cho 3
//12, 123, 27, 21, ...
total = 0;
for(i=0;i<length;i++) {
/***
Ep kieu '1' (char) -> 1 (int), '2' (char) -> 2 (int), '0' (char) -> 0 (int), ...
ASCCI: '0' -> 48, '1' -> 49, '2' -> 50, '3' -> 51, '4' -> 52, '5' -> 53, ...
value = s[i](char) ('5') = s[i] - '0' = '5' - '0' = 53 - 48 = 5 (int)
*/
value = s[i] - '0';//'1' -> 49, '0' -> 48 : '1' - '0' = 1 (int)
total += value;
}
printf("\nTotal = %d", total);
if(total % 3 == 0) {
printf("\nSo nguyen %s chia het cho 3", s);
} else {
printf("\nSo nguyen %s khong chia het cho 3", s);
}
return 0;
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)