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 BT1911

- 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 

Liên kết rút gọn:

https://gokisoft.com/1911

Bình luận

avatar
Nguyễn Nguyệt [community,C2010G]
2020-12-21 03:29:48



#include <stdio.h>

#include <stdlib.h>

int main(void) {
	int *p, i, sum=0;
	p = (int *) malloc(10 * sizeof(int));
	for(i=0;i<10;i++) {
		printf("\nNhap p[%d] = ", i);
		scanf("%d", p + i);
		if (p[i]%2==0) {
			sum+=p[i];
			}
		}		
		printf("\nSum div 2: %d\n", sum);
	}


avatar
Đỗ Minh Tâm [community,C2010G]
2020-12-21 03:29:38



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

int main(void){
	int *p,i;
	int sum=0;
	p = (int *) malloc(10 * sizeof(int));
	for(i=0;i<10;i++) {
		printf("\nNhap pt[%d] = ", i);
		scanf("%d", p+i);
		if (p[i]%2==0){
			sum+=p[i];
		}
	}
	printf("\nsum: %d",sum);
}


avatar
Dao Thu Huyen [C2010G]
2020-12-21 03:29:02



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

int main(void){
	int *p,i,sum=0;
	p= (int *) malloc(10 * sizeof(int));
	for(i=0;i<10;i++) {
		printf("\nNhap p[%d] = ", i);
		scanf("%d",p+i);
		if(p[i]%2==0){
			sum+=p[i];

		}
	
}
		printf("Tong bang=%d",sum);
}


avatar
Đỗ Minh Tâm [community,C2010G]
2020-12-21 03:27:43

#include<stdio.h>

#include<stdlib.h>


int main(void){

int *p,i;

int sum=0;

p = (int *) malloc(10 * sizeof(int));

for(i=0;i<10;i++) {

printf("\nNhap pt[%d] = ", i);

scanf("%d", p+i);

if (p[i]%2==0){

sum+=p[i];

}

}

printf("\nsum: %d",sum);

}

avatar
Nguyễn Hữu Hiếu [T2008A]
2020-10-01 14:38:07



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


avatar
Nguyễn Anh Vũ [T2008A]
2020-09-22 16:19:26



#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[]) {
	//bat dau code;
	int *p, i, n=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+1);
	
	}
	for(i=0, i<10, i++){
		    if(p[i]%2=0){	
		       	n=n+p[i];
		       	}	
	  }printf("\nTong cac so chia het cho 2 = %d", n);
	//ket thuc code;
	 return 0;
	 }


avatar
Trần Văn Lâm [T2008A]
2020-09-22 10:48:46



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


avatar
nguyễn Sử [T2008A]
2020-09-22 06:24:15



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

int main(int argc, char *argv[]) {
int p[10], t[20], i;
int *p;
p = &p[10];

for (i=0 ;i<=10-1;i++){
	printf("\nNhap p[%d]", i);
	scanf("%d", &p[i]);
	
for (i=0;i<=10-1;i++){
	
	if (i%2==0){
	S = S + p[i];
}
printf("\ntong=%d", s);



	return 0;
}


avatar
Trần Thị Khánh Huyền [T2008A]
2020-09-22 03:07:47



#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 t[5], i, sum;
	printf("\nNhap phan tu cua mang t");
	for(i=0;i<10;i++) {
		printf("\nNhap t[%d] = ", i);
		scanf("%d", t + i);
	}
	int *k;
	k = t;
	sum=0;
	for(i=0;i<10;i++) {
		printf("\nNhap k[%d] = %d", i, k[i]);
		if (k[i]%2==0)
		sum = sum +k[i];}
	printf("dap so tong can tinh la: %d", sum);
	
	return 0;
}


avatar
vuong huu phu [T2008A]
2020-09-21 14:59:09



#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 a[10];
	int *p;
	p= &a[10];
	int i,t;
	t=0;
	
	for (i=0 ;i<=10-1;i++){
		printf("\n nhap a[%d] ",i);
		scanf("%d",&a[i]);	}
		
	for (i=0;i<=10-1;i++){
	if (i%2==0){
		t=t+i;
	}	
	}printf ("\n tong cac so chia het cho 2 %d ",t);
	return 0;
}