- Code:
#include<stdio.h>
#include<conio.h>
void menu(void)
{
printf("\n1. Nhap N, M");
printf("\n2. Hien thi chu nhat");
printf("\n3. Tinh tong nguyen to");
printf("\n4. Tinh tong cot cuoi");
printf("\n5. Tinh tong hang cuoi");
printf("\n6. Thoat");
}
void Nhap(int *N, int *M)
{
printf("\nNhap N: ");
scanf("%d",N);
while(*N<=0)
{
printf("\nNhap lai N: ");
scanf("%d",N);
}
printf("\nNhap M: ");
scanf("%d",M);
while(*M<=*N)
{
printf("\nNhap lai M:");
scanf("%d",M);
}
}
void Hienthi(int N, int M)
{
int i;
for(i=1; i<=M; i++)
if(i%N!=0)
printf("%4d",i);
else
printf("%4d\n\n",i);
}
int xdnt(int i)
{
int flag=0, j;
for(j=2; j<=i/2; j++)
if(i%j==0)
{
flag=1;
break;
}
return flag;
}
int Tongnt(int M)
{
int i, j, flag=0, tong=0;
for(i=2; i<=M; i++)
{
flag=xdnt(i);
if(flag==0)
tong+=i;
}
return tong;
}
int Tongcot(int N, int M)
{
int i, tongcot=0;
for(i=1; i<=M; i++)
if(i%N==0)
tongcot+=i;
return tongcot;
}
int TongHangCuoiCung(int N, int M)
{
int i, j = M % N, tong = 0;
for(i = M; i > (M-j); i--)
tong+= i;
printf("\n Tong cac so o hang cuoi la: %d",tong);
}
void main()
{
int N,M, chon, tong=0;
//clrscr();
menu();
do
{
printf("\nMoi ban chon mot muc: ");
scanf("%d",&chon);
switch(chon)
{
case 1:
Nhap(&N, &M);
break;
case 2:
Hienthi(N,M);
break;
case 3:
tong=Tongnt(M);
printf("\nTong cac so nguyen to la: %d",tong);
break;
case 4:
printf("\nTong cac so o cot cuoi la: %d",Tongcot(N,M));
break;
case 5:
TongHangCuoiCung(N, M);
break;
case 6: exit(0);
}
}while(chon!=6);
getch();
}
Được sửa bởi Vy Thanh Định ngày 3/12/2009, 21:34; sửa lần 1. (Reason for editing : bỏ code vào thẻ code)