Write a proper C program that will keep appending a set of 5 numbers to a file (
ID: 3825005 • Letter: W
Question
Write a proper C program that will keep appending a set of 5 numbers to a file (numbers.dat) based on the number user enters as it runs. The program should keep asking the user for a number. If the number is zero, the program should stop.
The set of 5 numbers should be multiples of the entered number.
Example of the program run
Please enter a number (0 to terminate) : 3
Please enter a number (0 to terminate) : 5
Please enter a number (0 to terminate) : 2
Please enter a number (0 to terminate) : 0
Good bye.
The resulting numbers.dat file should contain the following
3 6 9 12 15
5 10 15 20 25
2 4 6 8 10
Explanation / Answer
//C program to print set of five numbers to a file
#include <stdio.h>
int main(void) {
int n=1,i=1;
int number;
FILE *file;
//Give your file location
file = fopen("numbers.dat","w");
if (file == NULL)
{
printf("Error opening file! ");
exit(1);
}
while(n==1){
printf("Please enter a number (0 to terminate):");
scanf("%d",&number);
if(number==0){
fclose(file);
n=0;
break;
}
//If number input is not zero , print in into the file
fprintf(file," ");
for(i=1;i<=5;i++){
fprintf(file ,"%d ",number*i);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.