Thursday, November 27, 2014

C program to write 'N' numbers into a file “numbers.txt” and the display only the numbers which are divisible by 7 from the file

/*............................................................................................................................................
File Name: rec25.c
Program : Write a program to write 'N' numbers into a file “numbers.txt” and the display only the numbers which are divisible by 7 from the file(Use fscanf() and fprintf() functions for reading and writing)
Author : 141740
Date : 26-11-2014
..............................................................................................................................................*/
#include<stdio.h>
#include<stdlib.h>
main()
{
FILE *fp;
int n,i,a[10];
system("clear");
printf("Program to find the divisible of 7\n");
if((fp=fopen("numbers.txt","w"))==NULL){
printf("Cannot create file...!\n");
exit(1);
}
printf("Enter the value of n:\n");
scanf("%d",&n);
printf("Enter number into the file:\n");
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
fprintf(fp,"%d\t",a[i]);
}
fclose(fp);
if((fp=fopen("numbers.txt","r"))==NULL){
printf("Cannot open file\n");
exit(1);
}
printf("The numbers which are divisible by 7 in the file is:\n");
for(i=1;i<=n;i++){
fscanf(fp,"%d",&a[i]);
if((a[i]%7)==0){
printf("%d\t",a[i]);
}
}
fclose(fp);
}


OUTPUT:-
Program to find the divisible of 7
Enter the value of n:
4
Enter number into the file:
7
14
23
66
The numbers which are divisible by 7 in the file is:
7 14 [141740@localhost ~]$

output2

Program to find the divisible of 7
Enter the value of n:
3
Enter number into the file:
7
77
99
The numbers which are divisible by 7 in the file is:
7 77 [141740@localhost ~]$

No comments:

Post a Comment