/*==CHUONG TRINH PHAN TICH SO NGUYEN THANH CAC THUA SO NGUYEN TO==*/ //Chuong trinh viet theo 2 cach: //1. Su dung ham khong de quy //2. Su dung ham de quy #include<stdio.h> //khai bao khuon mau ham void thuaSoNguyenTo(int); void thuaSoNguyenTo_DQ(int); main() { int n; printf("Nhap n: "); scanf("%d",&n); printf("\n%d = ",n); thuaSoNguyenTo_DQ(n); printf("\n%d = ",n); thuaSoNguyenTo(n); } //ham duoc viet theo kieu khong de quy void thuaSoNguyenTo(int n) { while(n>1) { //tim so tu nhien i>=2 nho nhat ma n chia het cho i int i = 2; while(n%i) i++; //voi i tim duoc, in ra thua so i, gan n = n/i printf(" %d ",i); n = n/i; //neu i chua phai la thua so cuoi cung (n>1) ... //... in them dau nhan vao sau thua so i if(n>1) printf("x"); } } //ham duoc viet theo kieu de quy void thuaSoNguyenTo_DQ(int n) { //neu n = 1, khong lam gi ca //neu n > 1 if(n>1) { //tim so tu nhien i>=2 nho nhat ma n chia het cho i int i = 2; while(n%i) i++; printf(" %d ", i); //neu i chua phai la thua so cuoi cung (n!=i) ... //... in them dau nhan vao sau thua so i if(n!=i) printf("x"); //goi ham de quy voi tham so la n/i thuaSoNguyenTo_DQ(n/i); } }
Tags:
lập trình c