write a c program to check three consecutive numbers 5s.
Share
write a c program to check three consecutive numbers 5s.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Answer:
Certainly! Here's a simple C program to check if there are three consecutive occurrences of the number 5 in an array:
```c
#include <stdio.h>
int main() {
// Define an array with numbers
int numbers[] = {2, 5, 5, 5, 7, 8, 5, 4, 1};
// Variable to count consecutive 5s
int consecutiveCount = 0;
// Iterate through the array
for (int i = 0; i < sizeof(numbers) / sizeof(numbers[0]); i++) {
// Check if the current element is 5
if (numbers[i] == 5) {
consecutiveCount++;
// Check if three consecutive 5s are found
if (consecutiveCount == 3) {
printf("Three consecutive 5s found starting at index %d.\n", i - 2);
return 0; // Exit the program
}
} else {
consecutiveCount = 0; // Reset the count if a non-5 is encountered
}
}
// If no three consecutive 5s are found
printf("No three consecutive 5s found.\n");
return 0;
}
```
This program defines an array of numbers and checks for three consecutive occurrences of the number 5. Adjust the array as needed for your specific use case.