Skip to content

Instantly share code, notes, and snippets.

@Vatsal596
Created September 4, 2023 05:43
Show Gist options
  • Save Vatsal596/805ae2db2660785ef48d8371038d1f7d to your computer and use it in GitHub Desktop.
Save Vatsal596/805ae2db2660785ef48d8371038d1f7d to your computer and use it in GitHub Desktop.
import { useState } from "react";
function Calc() {
const [n1, setn1] = useState(0)
const [n2, setn2] = useState(0)
const [result, setresult] = useState(0)
const myn1 = (e) => {
setn1(parseFloat(e.target.value))
}
const myn2 = (e) => {
setn2(parseFloat(e.target.value))
}
const add = () => {
setresult(n1+n2);
}
const sub = () => {
setresult(n1-n2);
}
const divide = () => {
setresult(n1/n2);
}
const multiple = () => {
setresult(n1*n2);
}
return (
<div>
<h1>Calculator</h1>
Enter a 1st number: <input type="number" onChange={myn1}/><br/>
Enter a 2nd number: <input type="number" onChange={myn2}/><br/>
<button onClick={add}>+</button>
<button onClick={sub}>-</button>
<button onClick={divide}>/</button>
<button onClick={multiple}>*</button>
<br/>
<h2>Result: {result}</h2>
</div>
);
}
export default Calc;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment