163k views
2 votes
Imagine that 10 int values are labeled byposition: 1, 2, 3, etc. Write a program that reads 10integers and tracks how many of them have the same value as theirposition. That is, ifthe first number read is 1, or the third number is 3, that countsas a match, and the output would be the number of matches (from 0to 10). Use a singleif statement and a loop.

User Murolack
by
4.9k points

1 Answer

5 votes

Answer:

#include<iostream>

using namespace std;

//main function

int main(){

//initialization

int count_Number=1,a1,match_Numbers=0;

//loop run 10 times

do{

//print

cout<<"Enter the number: ";

//read the value enter by user

cin>>a1;

//check for match

if(a1==count_Number){

match_Numbers++;

}

count_Number++;

}while(count_Number <= 10);

//display the output

cout<<"The number of matches is: "<<match_Numbers<<endl;

return 0;

}

Step-by-step explanation:

Include the library iostream for using the input/output instruction.

create the main function and declare the variables.

take the do-while loop which has a special property, the statement in the do-while execute first and then check the condition.

In the do-while, print the message by using the cout instruction and then store the value enter by the user into the variable.

then, check the value enter by the user is match the position or not. If the condition true, then count the matches and also update the position count.

this process continues until the position count is less than or equal 10. if condition false the loop terminates and then, display the output on the screen.

User Deep Patel
by
5.7k points