在 C 語言程式中列印 1/n 的前 k 位數字,其中 n 是一個正整數
輸入數字 N,使得 1/N 返回生成的十進位制輸出,直到指定的限制。
使用浮點數很容易,但挑戰是不使用它們。
輸入 - 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
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP