ป้อนหมายเลข N เพื่อให้ 1/N จะส่งคืนผลลัพธ์ที่สร้างเป็นทศนิยมที่ระบุจนถึงขีดจำกัด
เป็นเรื่องง่ายสำหรับตัวเลข Floating Point แต่ความท้าทายคือไม่ต้องใช้มัน
ป้อนข้อมูล − n=5 k=5
ผลผลิต − 20000
หมายความว่าถ้า n=5 และ k=5 มากกว่าหลังจากการหาร 1/5 ผลลัพธ์ควรแสดงจนถึงทศนิยม 5 จุด
อัลกอริทึม
Start Step 1 -> Declare int variable n to 9 and k to 7 and remain to 1 and i Step 2-> Loop for i to 0 and i<k and i++ Print ((10*remain)/n) Remain = (10*remain)%n Step 3-> end Loop For Stop
ตัวอย่าง
#include<stdio.h>
int main() {
int n = 9, k = 7, remain=1,i ; // taking n for 1/n and k for decimal values
printf("first %d digits of %d are : ",k,n);
for(i=0;i<k;i++) {
printf("%d",((10 * remain) / n));
remain = (10*remain) % n;
}
return 0;
} ผลลัพธ์
หากเรารันโปรแกรมด้านบน มันจะสร้างผลลัพธ์ต่อไปนี้
first 7 digits of 9 are : 1111111