Home > Backend Development > C++ > body text

Translate the following into Chinese: Print the first k decimal places of 1/n in a C program, where n is a positive integer

PHPz
Release: 2023-08-28 19:29:03
forward
1282 people have browsed it

Translate the following into Chinese: Print the first k decimal places of 1/n in a C program, where n is a positive integer

Enter a number N such that 1/N will return the output generated in the form specified in decimal until the limit is reached.

It's easy to use floating point numbers, but the challenge is not to use them.

Input− n=5 k=5

Output− 20000

This means that if n=5 and k = 5 divided by 1/5 the output should be displayed to 5 decimal places. The Chinese translation of

Algorithm

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
Copy after login

Example

is:

Example

#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;
}
Copy after login

Output

If we run the above program, it will The following output is generated.

first 7 digits of 9 are : 1111111
Copy after login

The above is the detailed content of Translate the following into Chinese: Print the first k decimal places of 1/n in a C program, where n is a positive integer. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!