In programming, it is often necessary to find the number of integers divisible by a certain number. In this program, we will write a C program that prompts the user to enter a list of integers, counts the number of integers that are divisible by 10, and prints the result.
Problem Statement
The problem statement is to write a C program that prompts the user to enter a list of integers, counts the number of integers that are divisible by 10, and prints the result.
Solution
Here is the C program to find the number of integers divisible by 10
#include <stdio.h> int main() { int n, count = 0; printf("Enter the number of integers: "); scanf("%d", &n); int numbers[n]; for (int i = 0; i < n; i++) { printf("Enter integer #%d: ", i + 1); scanf("%d", &numbers[i]); if (numbers[i] % 10 == 0) { count++; } } printf("\nThe number of integers divisible by 10 is: %d\n", count); return 0; }
Output
Explanation
First, we include the stdio.h
header file which is used for input/output functions. Then, we define the main()
function as the starting point of our program.
We declare an integer variable n
which will be used to store the number of integers entered by the user. We also declare an integer variable count
which will be used to count the number of integers divisible by 10.
Next, we prompt the user to enter the number of integers using the printf()
function. The scanf()
function is used to read in the number entered by the user. The first parameter to scanf()
is the format string “%d” which specifies that the input should be read in as an integer. The second parameter is the address of the n
variable where the input should be stored.
We then declare an array numbers
of size n
to store the integers entered by the user.
We use a for
loop to iterate through the array and prompt the user to enter each integer. The scanf()
function is used to read in the integer entered by the user. The first parameter to scanf()
is the format string “%d” which specifies that the input should be read in as an integer. The second parameter is the address of the numbers[i]
variable where the input should be stored.
We then use an if
statement to determine whether the integer is divisible by 10. If the integer is divisible by 10, we increment the count
variable by 1.
Finally, we print the number of integers divisible by 10 using the printf()
function.
Conclusion
In this tutorial, we learned how to write a C program to find the number of integers divisible by 10. We used a for
loop to iterate through the array of integers entered by the user, and we used an if
statement to determine whether each integer is divisible by 10. We then incremented the count
variable for each integer divisible by 10 and printed the result using the printf()
function.