C Programming Question Consider this data sequence 3 11 5 5
C Programming Question
Consider this data sequence: \"3 11 5 5 5 2 4 6 6 7 3 -8\". Any value that is the same as the immediately preceding value is considered a CONSECUTIVE DUPLICATE. In this example, there are three such consecutive duplicates: the 2nd and 3rd 5s and the second 6. Note that the last 3 is not a consecutive duplicate because it was preceded by a 7.
Write some code that uses a loop to read such a sequence of non-negativeintegers, terminated by a negative number. When the code exits the loop it should print the number of consecutive duplicates encountered. In the above case, that value would be 3.
Solution
Please find the required program along with its output. Please see the comments against each line to understand the step.
#include <stdio.h>
int main()
{
int prev=-1,curr, count=0;
printf(\"Enter numbers(press negative number to exit):\ \"); //read the first number
scanf(\"%d\",&curr);
while(curr >= 0){ //iterate the loop untill the user input number is non negative
if(curr == prev) //if the current user input number is equal to previous number
count++; //increment the count
prev = curr; //set assign current number to previous number
scanf(\"%d\",&curr); //read the next number
}
printf(\"\ Consecutive duplicate count = %d\ \",count); //print the result Consecutive duplicate count
return 0;
}
-----------------------------------------------------------
OUTPUT:
Enter numbers(press negative number to exit):
3
11
5
5
5
2
4
6
6
7
3
-8
Consecutive duplicate count = 3
