Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22167

How does the cout statement affect the O/P of the code written?

$
0
0
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

int main() {
    int t;
    double n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        double x;
        for(int i=1;i<=10000;i++)
        {
            x=n*i;
            if(x==ceilf(x))
            {
                cout<<i<<endl;
                break;
            }
        }
    }
    return 0;
}

For I/P:

3
5
2.98
3.16

O/P:

1

If my code is:

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

int main() {
    int t;
    double n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        double x;
        for(int i=1;i<=10000;i++)
        {
            x=n*i;
            cout<<"";//only this statement is added;
            if(x==ceilf(x))
            {
                cout<<i<<endl;
                break;
            }
        }
    }
    return 0;
}

For the same input O/P is:

1
50
25

The only extra line added in 2nd code is: cout<<"";

Can anyone please help in finding why there is such a difference in output just because of the cout statement added in the 2nd code?


Viewing all articles
Browse latest Browse all 22167

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>