0% found this document useful (0 votes)
3 views

simple calculator (1)

Uploaded by

ranof69993
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

simple calculator (1)

Uploaded by

ranof69993
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Week-4.

Write a program to create a simple calculator Application using React JS

import React, { useState } from 'react';

import './App.css';

function App() {

const [input, setInput] = useState(''); // State to store input

// Function to handle button click

const handleClick = (value) => {

setInput(input + value); // Append clicked value to input

};

// Function to clear input

const handleClear = () => {

setInput('');

};

// Function to evaluate the result

const handleEqual = () => {

try {

setInput(eval(input).toString()); // Evaluate expression and convert result to string

} catch (error) {

setInput('Error'); // Show 'Error' if the expression is invalid

};

return (

<div className="App">
<h1>Simple Calculator</h1>

<div className="calculator">

<input type="text" value={input} readOnly />

<div className="buttons">

{/* Calculator buttons */}

<button onClick={() => handleClick('1')}>1</button>

<button onClick={() => handleClick('2')}>2</button>

<button onClick={() => handleClick('3')}>3</button>

<button onClick={() => handleClick('+')}>+</button>

<button onClick={() => handleClick('4')}>4</button>

<button onClick={() => handleClick('5')}>5</button>

<button onClick={() => handleClick('6')}>6</button>

<button onClick={() => handleClick('-')}>-</button>

<button onClick={() => handleClick('7')}>7</button>

<button onClick={() => handleClick('8')}>8</button>

<button onClick={() => handleClick('9')}>9</button>

<button onClick={() => handleClick('*')}>*</button>

<button onClick={() => handleClick('0')}>0</button>

<button onClick={handleClear}>C</button>

<button onClick={handleEqual}>=</button>

<button onClick={() => handleClick('/')}>/</button>

</div>

</div>

</div>

);
}

export default App;

App.css file

.App {

text-align: center;

margin-top: 50px;

.calculator {

display: inline-block;

border: 2px solid #000;

padding: 20px;

border-radius: 10px;

background-color: #f4f4f4;

input {

width: 200px;

height: 40px;

font-size: 20px;

text-align: right;

margin-bottom: 10px;

padding: 5px;
}

.buttons {

display: grid;

grid-template-columns: repeat(4, 50px);

grid-gap: 10px;

button {

padding: 20px;

font-size: 20px;

cursor: pointer;

button:hover {

background-color: #ddd;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy