Home > Back-end >  i am getting the output for this is as 0112 and the output that i am expected to get is 2
i am getting the output for this is as 0112 and the output that i am expected to get is 2

Time:09-30

i am getting the output for this is as 0112. Please help me in telling why am i getting that output when the the input is:- 4 1 2 2 4 5 6 6 10 and the output that i am expected to get is 2.

Tina and Rahul have 1 toy each. They are entering into an amusement park but it is not allowed to take the toys inside, so they have to keep it in the boxes provided by the park management. They want to keep the toys together in one box. There are N boxes in total, at this moment there are t i toys present in i t h box and the maximum capacity of the box is denoted by c i . Rahul and Tina want to know in how many boxes can they keep their toys such that both the toys are in the same box.

Input format The first line of the input contains integer N , denoting the count of boxes. Each of the next N lines contains two integers t i and c i denoting the number of toys present in the i t h box and the maximum capacity of that box.

'''

#include <bits/stdc  .h>
using namespace std;

int main()
{
  int n,x,y,i,count=0,z;
  cin>>n;
  for(i=0;i<n;i  )
  {
    cin>>x>>y;
    z=y-x;
    if(z>=2)
    {
      count  ;
    }
    cout<<count;
    
  }
  
  return 0;
}

'''

CodePudding user response:

 // put count outside the for loop.
 //thanks.

for( ) {

}
cout << count << endl;

CodePudding user response:

You need to print the count after the for loop finishes because it prints with each pass of the for loop:

#include <iostream>

using namespace std;

int main()
{
  int n, x, y, z, count=0;

  cout << "Input value for n: ";
  cin >> n;

  for(int i = 0; i < n; i  )
  {
      cout << "\ninput value for x" << i   1 << ": ";
      cin >> x;
  
      cout << "intput value for y" << i   1 << ": ";
      cin >> y;
  
      z = y-x;
  
      if( z >= 2)
        count  ;
  }

  cout << "\ncount = " << count;

  return 0;
}
  • Related