Ask coding questions

← Back to all posts
something wrong about my code.

I am looking to find The Ohm Law by using v = i*r. but when I run my code the v still 0 as I defined. Any thought for it ?
#include <iostream>
using namespace std;

int main() {
int v = 0;
int i = 0;
int r = 0;
v = i * r;
cout << "Please input the resistance: " ;
cin >> r ;
cout << "Please input the current: ";
cin >> i ;
cout << " Your voltage is:" << v << endl;
return 0;

}

Commentshotnewtop
khainguyen1707 (0)

For example you will take like 500 * 0.018 = 9 right ? However with my code, I put a same number but it does not show right.

[deleted]

@khainguyen1707 It's because you're trying to multiply `0 * 0`, when you should be doing `500 * 0.018` instead.

Zavexeon (237)

Try changing `v = i * r` to `int v = i * r`.

I'm not sure if it'll work but v should be an integer.

[deleted]

Use the formula `v=i*r` after taking input making it something like

``````cin >> i;
v = i * r;
cout << "Your voltage is: " << v << endl;``````

Please upvote my reply
If you find it useful.