Lập trình C: Đề 8
Câu 1:
1. void Prime(int a);
2. float Dt_hcn(float a, float b);
3. void Sum_odd(int a, int b);
4. void Min_max(int N, int a[]);
Câu 2:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void nhap(int *N)
{
do
{
printf("\nN = ");
scanf("%d",N);
if(*N==0)
exit(0);
else if(!(*N>0 && *N%2!=0 && *N<=25))
printf("\nYeu cau nhap lai N!");
}while(!(*N>0 && *N%2!=0 && *N<=25));
}
void hienthi(int N)
{
int i, j, k;
for(i=0; i<N/2+1; i++)
{
for(j=0; j<N-2*i; j++)
printf("*");
puts("\n");
if(N-2*i==1)
break;
for(k=0; k<=i; k++)
printf(" ");
}
for(i=1; i<N/2+1; i++)
{
for(k=0; k<N/2-i; k++)
printf(" ");
for(j=0; j<=2*i; j++)
printf("*");
puts("\n");
}
}
void main()
{
int N;
clrscr();
nhap(&N);
hienthi(N);
getch();
}
Câu 3:
#include <stdio.h>
#include <conio.h>
void menu(void)
{
printf("\n1. Input N");
printf("\n2. Display \"Double columns\" N");
printf("\n3. Calculate total of all elements of even rows in \"Double columns\" N");
printf("\n4. Check N is perfect number");
printf("\n5. Exit");
}
void nhap(int *N)
{
printf("\nN = ");
scanf("%d",N);
while(*N<=0 || *N>20 || *N%2!=0) // N phai la so chan (even)
{
printf("\nRe enter N = ");
scanf("%d",N);
}
}
void hienthi(int N)
{
int i;
for(i=1; i<=N/2; i++)
printf("%d \t %d\n\n",i,i+N/2);
}
void tong(int N)
{
int i, tong=0;
for(i=1; i<=N/2; i++)
if(i%2==0)
tong+=i+(i+N/2);
printf("\nTong gia tri o cac hang chan la: %d",tong);
}
void kiemtra(int N)
{
int i, tongus=0; // tong cac uoc so
for(i=1; i<=N/2; i++)
if(N%i==0)
tongus+=i;
if(tongus==N)
printf("\n%d la so hoan hao",N);
else
printf("\n%d khong phai la so hoan hao",N);
}
void main()
{
int N, chon;
clrscr();
while(1) {
menu();
printf("\nMoi ban chon 1 muc: ");
scanf("%d",&chon);
switch(chon)
{
case 1: nhap(&N); break;
case 2: hienthi(N); break;
case 3: tong(N); break;
case 4: kiemtra(N); break;
case 5: return;
}
}
getch();
}