Skip to content

Commit

Permalink
calculator demo in c
Browse files Browse the repository at this point in the history
  • Loading branch information
2023rahabu committed Aug 3, 2023
1 parent 8098a19 commit 2e2bd83
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
31 changes: 31 additions & 0 deletions rahab/0-calculator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "main.h"

/* Function to check if the operator is valid*/
int isValidOperator(char operator)
{
return operator == '+' || operator == '-' || operator == '*' || operator == '/';
}

/*Function to add two numbers */
float addition(float num1, float num2) {
return num1 + num2;
}

/*Function to subtract two numbers*/
float subtraction(float num1, float num2) {
return num1 - num2;
}

/*Function to multiply two numbers */
float multiplication(float num1, float num2) {
return num1 * num2;
}

/*Function to divide two numbers */
float division(float num1, float num2) {
if (num2 == 0) {
printf("Error: Cannot divide by zero!\n");
return 0;
}
return num1 / num2;
}
25 changes: 25 additions & 0 deletions rahab/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# collaborating on Calculator Demo project in c programming
The Demo will run like a calculator which will handle 4 operation
.Addition
.Substraction
.Multiplication
.Division

#Program contain 4 functions
.Addition function
.Multiplication function
.Substraction function
.Division function

#Calculator is interactive
.It takes in two numbers from the user
.It takes in an arithmetic operator for the four math operation
.Perform arthmetic operationbased on user choice
.Then it's prints result when user entered valid operator
.When user enter wrong arthmetic operation other than division, addition,multiplication and addition
.program warn user then print an error message.

#The program allow user to use calculator many time
.After performing one operation user will be asked if he/she want to contiue
.if user select y(for yes) or Y(for capital Yes)user can keep using the calculator
.if user select other character or typed anything rather than Y or y the program will end.
56 changes: 56 additions & 0 deletions rahab/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <stdio.h>
#include "main.h"

int main()
{
char operator;
float num1, num2, result;
char choice;

printf("Welcome to the Four Basic Operations Calculator!\n");

do
{
printf("Enter the first number: ");
scanf("%f", &num1);

printf("Enter an arithmetic operator (+, -, *, /): ");
scanf(" %c", &operator);

printf("Enter the second number: ");
scanf("%f", &num2);

if (isValidOperator(operator))
{
switch (operator)
{
case '+':
result = addition(num1, num2);
break;
case '-':
result = subtraction(num1, num2);
break;
case '*':
result = multiplication(num1, num2);
break;
case '/':
result = division(num1, num2);
break;
}

printf("Result: %.2f\n", result);
}
else
{
printf("Error: Invalid arithmetic operator!\n");
}

printf("Do you wish to perform another operation? (y/n): ");
scanf(" %c", &choice);
}
while (choice == 'y' || choice == 'Y');

printf("Thank you for using the calculator. Goodbye!\n");

return 0;
}
12 changes: 12 additions & 0 deletions rahab/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef MAIN_H
#define MAIN_H

#include <stdio.h>

// Function prototypes
float addition(float num1, float num2);
float subtraction(float num1, float num2);
float multiplication(float num1, float num2);
float division(float num1, float num2);
int isValidOperator(char operator);
#endif // MAIN_H

0 comments on commit 2e2bd83

Please sign in to comment.