Write a C program that reads from the keyboard a natural number n with up to 9 digits and creates the text file data.out containing the number n and all its non-zero prefixes, in a single line, separated by a space, in order decreasing in value. Example: for n = 10305 the data file.out will contain the numbers: 10305 1030 103 10 1.
Program in C++:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
unsigned long n;
cin>>n;
//I declare the file f and open it for data writing
ofstream fis("file.out");
while(n!=0)
{
//write the number in the file, then delete the last digit
fis<<n<<'\n';
n=n/10;
}
fis.close();
return 0;
}
That's what i made in C:
#include <stdio.h>
#include <string.h>
int main()
{
int n;
scanf("%d",&n);
FILE *f;
f=fopen("date.out","w");
while(n!=0)
{
printf(n);
f=fopen("date.out", "r");
n=n/10;
}
fclose(f);
return 0;
}