-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
my first commit into the repo with all my files
- Loading branch information
1 parent
4222b01
commit 75425a8
Showing
5 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.exe | ||
.vscode |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#include "main.h" /* including main header file */ | ||
#include <stdio.h> /* Including the standard input & output header file */ | ||
#include <string.h> /* Including the string function header file */ | ||
|
||
/** | ||
* main - a basic claculator that collects two numbers and an operator then perform a | ||
* mathimathecal operation according to the operator given and prints result | ||
* | ||
* Return: 0 (Success) | ||
*/ | ||
int main(void) | ||
{ | ||
/* Initialise variables */ | ||
double num1, num2; | ||
char operator; | ||
double result; | ||
char name; | ||
|
||
/* message */ | ||
printf("Hello\n"); | ||
|
||
/* ask for user_name */ | ||
printf("Please enter a username: "); | ||
|
||
/* collects user_name */ | ||
scanf("%s", &name); | ||
|
||
/* welcome message and about calculator */ | ||
printf("Welcome %s to Samuel's calculator\n", &name); | ||
|
||
printf("This calculator takes in two numbers and an arithmetic operator\n"); | ||
printf("These are the arithmetic operators\n"); | ||
printf("For Addition: +\n"); | ||
printf("For Subtraction: -\n"); | ||
printf("for Multiplication: *\n"); | ||
printf("for Division: /\n"); | ||
|
||
/* asks for first number */ | ||
printf("Enter your first number: "); | ||
|
||
/* collects first number */ | ||
if (scanf("%lf", &num1) != 1) | ||
{ | ||
/* if a charcter other than a number is inputed */ | ||
printf("ERROR: Invalid input\n"); | ||
printf("Recompile program again\n"); | ||
return (1); | ||
} | ||
|
||
/* otherwise */ | ||
else | ||
{ | ||
/* asks for second number */ | ||
printf("Enter your second number: "); | ||
|
||
if (scanf("%lf", &num2) != 1) | ||
{ | ||
/* if a charcter other than a number is inputed */ | ||
printf("ERROR: Invalid input\n"); | ||
printf("Recompile program again\n"); | ||
return (1); | ||
} | ||
} | ||
|
||
/* asks for operator */ | ||
printf("Enter operator: "); | ||
|
||
/* collects operator */ | ||
scanf(" %c", &operator); | ||
|
||
/* performing the arithmethic operation */ | ||
switch (operator) | ||
{ | ||
case '+': | ||
result = num1 + num2; | ||
break; | ||
|
||
case '-': | ||
result = num1 - num2; | ||
break; | ||
|
||
case '*': | ||
result = num1 * num2; | ||
break; | ||
|
||
case '/': | ||
/* if sencond number is 0, a number can't be devided by 0 */ | ||
if (num2 != 0) | ||
{ | ||
result = num1 / num2; | ||
} | ||
else | ||
{ | ||
printf("ERROR: you can't devide by 0\n"); | ||
return (1); | ||
} | ||
break; | ||
|
||
default: | ||
printf("please input the right operator\n"); | ||
printf("Recompile again\n"); | ||
} | ||
|
||
/* output result of operation */ | ||
printf("Result = %.2f\n", result); | ||
|
||
/* thanking message and end of program */ | ||
printf("Thank you for using this basic calculator app\n"); | ||
printf("Recompile to use again\n"); | ||
|
||
|
||
return (0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef MAIN_H | ||
#define MAIN_H | ||
|
||
double add(int num1, int num2); | ||
double subtract(double num1, double num2); | ||
double muliply(double num1, double num2); | ||
double divide(double num1, double num2); | ||
|
||
#endif /* MAIN_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# ALX SE Algorithmia Demo Project | ||
## Basic Calculator | ||
|
||
The Basic Calculator C implementation is a simple command-line calculator that allows users to perform basic arithmetic operations, such as addition, subtraction, multiplication, and division. The calculator takes user input and displays the result of the specified operation. | ||
|
||
## AIM | ||
|
||
This project aims to provide an easy-to-understand example of how to build a basic calculator using C. It is a great starting point for people learning C programming and looking to practice fundamental concepts, such as functions, control flow, and user input. | ||
|
||
## Operations | ||
|
||
This are the following operations that can be performed with this calculator: | ||
|
||
- Addition: Perform addition of two numbers. | ||
- Subtraction: Perform subtraction of two numbers. | ||
- Multiplication: Perform multiplication of two numbers. | ||
- Division: Perform division of two numbers. | ||
|
||
## Usage | ||
|
||
Using this program run the following commands in your terminal: | ||
|
||
* Clone the Demo-Project repository using this command `git clone <link to Demo-Project repository> ` | ||
* Change Directory to `samuelejalonibu` using this command `cd samuelejalonibu` | ||
* Compile the program using C compiler, use this command ` gcc *.c -o calculator` | ||
* Run the compiled executable using this command `./calculator` | ||
* Enter two numbers and an arithmetic operator when asked | ||
* Then you'll see your result | ||
|
||
## Example | ||
|
||
Here is an example of how to use the calculator: | ||
|
||
``` | ||
Welcome to the Basic Calculator! | ||
Choose an operation: | ||
1. Addition | ||
2. Subtraction | ||
3. Multiplication | ||
4. Division | ||
Enter your operator: + | ||
Enter the first number: 10 | ||
Enter the second number: 20 | ||
Result: 10 + 20 = 30 | ||
``` | ||
|
||
Feel free to try out different operations and numbers to explore the calculator's functionality. | ||
|
||
--- | ||
|
||
Happy calculating and keep exploring C programming! |