Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how I get the 7.375 answer

+1 vote
asked Feb 8, 2018 by anonymous
int a,b,c,d,e;
 a=3;
 b=4;
 c=(a%b)*6;
 d=c/b;
 e=(a+b+c+d)/4;
 cout<<e<<endl;

2 Answers

0 votes
answered Feb 11, 2018 by anonymous
which compiler you will use.
commented Feb 11, 2018 by anonymous
if you are using int then it will be only show that 7
and float then 7.some thing
0 votes
answered 4 days ago by Shaurya (430 points)

Why your original code doesn’t give 7.375 and how to get it

Problem: all your variables are int, so every division uses integer division (it truncates the fractional part). That makes d = c / b evaluate to 4 (not 4.5), and then

e=3+4+18+44=294=7

so cout << e prints 7.

To get 7.375 you must perform floating‑point division for the step that produces 4.5 and for the final average. Convert the divisor or numerator (or the result variable) to a floating type (double or float) so C++ does real division.

Minimal corrected code (simple, clear fix)

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int a = 3;
    int b = 4;
    int c = (a % b) * 6;        // (3 % 4) = 3 -> c = 18

    double d = static_cast<double>(c) / b; // 18 / 4.0 = 4.5
    double e = (a + b + c + d) / 4.0;      // (3 + 4 + 18 + 4.5) / 4 = 7.375

    cout << fixed << setprecision(3) << e << endl; // prints 7.375
    return 0;
}

Key points

  • Integer division truncates: 18 / 4 → 4.

  • Floating division required: 18 / 4.0 → 4.5.

  • Use double (or float) for d and e, or cast operands with static_cast<double>(...).

  • Use 4.0 (not 4) when dividing to force floating‑point arithmetic.

That change yields the desired result 7.375

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...