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

Bài toán pointer đầu tiên - Hello pointer - Lập trình C

- Khai báo con trỏ *p

- Cấp phát mảng p gồm 10 phần tử

- Nhập các phần tử trong mảng

- Tính tổng các số chia hết cho 2 trong mảng p 

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

5

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

Nguyên Phấn Đông [community]
Nguyên Phấn Đông

2020-09-21 14:47:52



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

int main(){
	int n=10,t[10],i;
	int *p=t;
	for(i=0;i<10;i++){
		printf("p[%d]:", i);
		scanf("%d", &p[i]);
	}
	int s=0;
	for(i=0;i<10;i++){
		if(t[i]%2==0){
			s=s+t[i];
		}
	}
	printf("\nTong=%d", s);
	return 0;
}



Do Trung Duc [T2008A]
Do Trung Duc

2020-09-21 14:24:58



#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 *p, S, i;
	
	p = (int*) calloc (10 , sizeof(int));
	S = 0;
	for(i=0;i<10;i++){
		printf("Nhap phan tu p[%d] = ", i);
		scanf("%d", &p[i]);
	
	}

	for(i=0;i<10;i++){
	if(p[i] % 2 ==0){
		S = S + p[i];
	}
	}
	printf("Tong cac so chia het cho 2 trong mang p la S = %d ", S);
	
	
	return 0;
}



An Văn Minh [T2008A]
An Văn Minh

2020-09-21 12:34:55



#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(){
	int N=10;
	int t[N],i;
	int *p=t;
	for(i=0;i<N;i++){
		printf("\np[%d]:", i);
		scanf("%d", &p[i]);
	}
	int s=0;
	for(i=0;i<N;i++){
		if(i%2==0){
			s=s+i;
		}
	}
	printf("\nTong=%d", s);
	return 0;
}



hainguyen [T2008A]
hainguyen

2020-09-21 11:26:13



#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 *p, i;
	p = (int *) malloc (10 * sizeof(int));
	for(i=0; i<10; i++){
		printf("\nNhap p[%d] = ", i);
		scanf("%d", p + i);
	}
	int num = 0;
	for(i=0; i<10; i++){
		if(p[i]%2 == 0){
			printf("\n%d", p[i]);
			num = num + p[i];
		}
	}
	printf("\nTong cac so chia het cho 2 la: %d", num);

	return 0;
}



Triệu Văn Lăng [T2008A]
Triệu Văn Lăng

2020-09-21 10:10:33



#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 *p, i;
	p= (int*) malloc (10*sizeof(int));
	for(i=0;i<10;i++) {
		printf("\nnhap p[%d]=",i);
		scanf("%d",p+i);
	}   
    
    int sum=0;
    for(i=0;i<10;i++) {
    	if(p[i]%2==0) {
	       sum=sum+p[i];
	  }
	}
    printf("\ntong cac so chia het cho 2 la: %d",sum);
    
    
	return 0;
}



Đặng Trần Nhật Minh [T2008A]
Đặng Trần Nhật Minh

2020-09-21 09:42:00



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

int main() {

	int *p, sum = 0;
	p = (int *)malloc(10 * sizeof(int));
	
	for (int i = 0; i < 10; i++) {
		
		printf("Nhap a[%d] = ", i);
		scanf("%d", p + i);
		if (*(p + i) % 2 == 0) sum += *(p + i);
		
	} 
	
	printf("SUM = %d", sum);
	
	return 0;

}



Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2020-09-21 09:15:47



#include<stdio.h>
#include<stdlib.h>
int main(){
	int *p,i,tong=0;
	p=(int)malloc(10 * sizeof(int));
	printf("\nNhap gia tri cac phan tu:");
	for(i=0;i<10;i++){
		printf("\np[%d]= ",i);
		scanf("%d",p+i);
	}
	for(i=0;i<10;i++){
		if(p[i] % 2==0){
			tong=tong+p[i];
		}
	}
	printf("\nTong cac phan tu chan la %d",tong);
}



Nguyễn Xuân Mai [T2008A]
Nguyễn Xuân Mai

2020-09-21 09:06:39



#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 *p,i,tong=0;
	p = (int *) malloc (10 * sizeof(int));
	printf("\nNhap phan tu cua mang p");
	for(i=0;i<10;i++){
		printf("\nNhap p[%d] = ", i);
		//scanf("%d", &p[i]);
		scanf("%d", p + i);
	}
	for(i=0;i<10;i++){
		if(p[i]%2==0){
			tong=tong+p[i];
		}
	}printf("Tong cac so chia het cho 2 = %d ",tong);	
	
	return 0;
}