Question

Write a program that reads an integer k from user and finds the number of elements...

Write a program that reads an integer k from user and
finds the number of elements that are divisible by k in the file
assignment4.txt. A number n is divisible by k if n = kx for some
integer x > 0. You can use the mod operator % to test for
divisibility. The file assign4.txt has integer values in the range
[0,100 ]. You should use end-of-file controlled loop for this
problem.
Sample execution is given below. Assume the data in the
assignment4.txt is
90
23
74
81
80
The output to be displayed on screen for the above file is
Enter k
10
2 numbers are divisible by 10

90
23
74
81
80
85
100
92
86
77
68
89

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#include <iostream>

#include <fstream>

using namespace std;

int main()

{

int k,a,count=0; // variable declaration

cout<<"Enter the value of k: ";

cin>>k;


ifstream infile; // input file declaration



infile.open("assignment4.txt"); // Syntax for open the file

while (infile >> a) // read the file content

{

if(a%k==0)

count++;

}

infile.close(); // Atlast close the file

cout<<count<<" numbers are divisible by "<<k<<endl;



return 0;



}

Output:

clang version 7.0.0-3-ubuntu0.18.04.1 (tags/RELEASF 700/final) Enter the value of k: 10 3 numbers are divisible by 10

Add a comment
Answer #2
Write a program that asks to enter 2 lntegers of 4 digits and displays the different digits X and *if they are the same if the user enter a number which is not of four digits then a convenient is displayed For example if the user enters 1234 1432 Then the output should be : *X*X
answered by: abbas gerro
Add a comment
Know the answer?
Add Answer to:
Write a program that reads an integer k from user and finds the number of elements...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • this program is in C. Write a program that computes the number of elements in an...

    this program is in C. Write a program that computes the number of elements in an array divisible by a user specified number. Declare an integer array of size 7 and read the array elements from the user. Then, read a number k from the user and compute the number of elements in the array divisible by k. Consider the following example. 3 elements in this array are divisible by 2 ({2,2,4}). Sample execution of the program for this array...

  • I need it in JAVA Write a program that randomly populates an array of size 100,...

    I need it in JAVA Write a program that randomly populates an array of size 100, sorts it, and then finds the median. The random numbers must be from 0-99 and all integer values. Also since there is an even set of numbers the median is found by taking the mean of the two middle numbers (In other words the 50th and 51st). You have to code your own sorting method. You may not use a built in java sorter....

  • use simple C++ please Exercise 3: Write a program that reads a positive number and checks...

    use simple C++ please Exercise 3: Write a program that reads a positive number and checks if the number can be expressed as a sum of two prime numbers. If yes, write all possible ways of expressing the number as sum of primes. Note: n is a prime if it is divisible only by 1 and itself, hence not divisible by any integer in the range from 2 to sqrt(n), both inclusive. Write a function is Prime that take a...

  • 2. Write a program that reads N integer numbers of array from the user and then...

    2. Write a program that reads N integer numbers of array from the user and then displays the sum, max number and the numbers of the array use four subroutines ( Read_Array(), Calculate_Sum(), Find_Max() and Display_Array()). Here is sample output Please enter number of elements: 4 Please enter the required numbers: 50 100 150 20 The entered numbers are: 50 100 150 20 Sum is : 320 Max is 150 by Mpsi ,sum and max to call subroutine and retrieve...

  • Write a Python program (question2.py) that reads from a file called “input.txt” numbers in [1,39] separated...

    Write a Python program (question2.py) that reads from a file called “input.txt” numbers in [1,39] separated in by commas. The numbers are in [1-99]. The program will then convert each number to a possible Roman Numeral equivalent, and print it on the screen. Remember, I is 1, V is 5, X is 10 For example, if the input is: 23, 11 the output is: XXIII, XI. ROMAN NUMERALS CHART 1 TO 100 69 LXIX 11 2 11 3 III 4...

  • Write a program that reads a file containing an arbitrary number of text integers that are...

    Write a program that reads a file containing an arbitrary number of text integers that are in the range 0 to 99 and counts how many fall into each of eleven ranges. The ranges are 0-9, 10-19, 20-29,..., 90-99 and an eleventh range for "out of range." Each range will correspond to a cell of an array of ints. Find the correct cell for each input integer using integer division. After reading in the file and counting the integers, write...

  • Please help me with this program.You are to write a C++ program that will read in...

    Please help me with this program.You are to write a C++ program that will read in up to 15 students with student information and grades. Your program will compute an average and print out certain reports. Format: The information for each student is on separate lines of input. The first data will be the student�s ID number, next line is the students name, next the students classification, and the last line are the 10 grades where the last grade is...

  • In Java, using only recursion. Ask the user for a list of integers. Display the greatest...

    In Java, using only recursion. Ask the user for a list of integers. Display the greatest number in the sequence. Allow any non-integer to end the input. DO NOT USE LOOPS. We are currently learning recursion and can only use recursion. Please provide notes so to better understand. Please describe how inputting a string will end the input section and then go on to execute what number is greater within the entire user input. -------------------------------------------------------------------------------- Standard Input                 5 20...

  • in C++ You are to define a structure containing the id number, three test scores, and...

    in C++ You are to define a structure containing the id number, three test scores, and their average, for each student in a class of 20. The file pr2data.txt is included and contains the data. The program is to include the following: A function that reads 20 records of data from the file, finds the average for each student and prints each student’s record. A function that lists the ids and averages for all the students whose average is above...

  • Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate...

    Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate the number of test scores the user enters; have scores fall into a normal distribution for grades -Display all of the generated scores - no more than 10 per line -Calculate and display the average of all scores -Find and display the number of scores above the overall average (previous output) -Find and display the letter grade that corresponds to the average above (overall...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT