Write a function named countMultiples, which takes in a given integer n, counts
ID: 3582966 • Letter: W
Question
Write a function named countMultiples, which takes in a given integer n, counts how many non-zero numbers in the input are a multiple of n, and returns this count. The function reads the numbers entered user. The user enters a sequence whole numbers ending with zero. Different users may enter different number of numbers. Write a driver program which repeatedly prompts the user to enter a non-zero whole number reads the number, calls countMultiples, and prints the result until the user decides to quit the program. No comments needed.Explanation / Answer
Both of the solutions are working as per complete requirements. Do see it. :)
The answer for the 1st part is:
C-Program :
#include<stdio.h>
#define SIZE 100
int countMultiples(int n)
{
int i=0, enteredno;
int count=0;
int no[SIZE] ={0};
printf(" Good. Now enter numbers, press 0 to terminate in between. ");
do
{
scanf("%d", &enteredno);
if(enteredno != 0)
no[i++] = enteredno;
else
break;
}
while(i<SIZE);
for(i=0; no[i]!=NULL; i++)
{
if(no[i] % n == 0)
count++;
}
return count;
}
The answer for the 2nd part is: (This is the complete code along with above mentioned code)
C-Program:
#include<stdio.h>
#define SIZE 100
int countMultiples(int n)
{
int i=0, enteredno;
int count=0;
int no[SIZE] ={0};
printf(" Good. Now enter numbers, press 0 to terminate in between. ");
do
{
scanf("%d", &enteredno);
if(enteredno != 0)
no[i++] = enteredno;
else
break;
}
while(i<SIZE);
for(i=0; no[i]!=NULL; i++)
{
if(no[i] % n == 0)
count++;
}
return count;
}
void main()
{
int n, result;
char choice;
do
{
printf("Enter a whole - number: ");
scanf("%d", &n);
result = countMultiples(n);
printf(" Required count is : %d", result);
printf(" Do you want to continue? (y/n) Enter your choice: ");
scanf(" %c",&choice);
}
while(choice=='Y' || choice=='y');
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.