using Linux 1. (100 pts) Write a program that reads an integer k from user and f
ID: 642240 • Letter: U
Question
using Linux
Explanation / Answer
//C program that prompt for a input k
//value and opens a file assign4.txt
//that contains integer values
//and checks if the values are divisible by k
//value and prints the number of values are
//divisible by k
#include<stdio.h>
#include<conio.h> // for getch function
#include <stdlib.h> //for FILE pointer
int main()
{
//intialize k value
int k=0;
//read number from file
int number;
//count value
int divisibleCount=0;
FILE *fp;
//open file for reading input
fp=fopen("assign4.txt","r");
//check if file exists or not
if(fp==NULL)
{
printf("File doesnot exist. Terminating program");
printf("Press any key to exit");
getch();
}
printf("Enter k ");
//read k value
scanf("%d",&k);
//check if the file pointer at the end of the file
while(fscanf(fp, "%d", &number) != EOF)
{
//check divisiblity of number with k
if(number%k==0)
//increment the count
divisibleCount++;
}
//print number of values are divisible by k
printf("%d number are divisble by %d",divisibleCount,k);
getch();
return 0;
}
-----------------------------------------------------------------------------------------------
Sample output:
assign.txt file
90
23
74
83
output:
Enter k
10
1 number are divisble by 10
Hope this helps you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.