Last active
August 29, 2020 05:52
-
-
Save sainak/643b27982897790d1bc36709879b2c2f to your computer and use it in GitHub Desktop.
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
//#include<conio.h> it's not a standard library avoid it at all costs | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
// function to clear screen | |
//https://www.sololearn.com/Discuss/1752486/what-function-i-should-use-for-clear-screen-in-c-on-codeblocks | |
//https://code.sololearn.com/ck9435XF9iYU/?ref=app#c | |
#ifdef __unix__ | |
void clearScreen() | |
{ | |
printf( "%s", "\033[2J\033[1;1H" ); | |
} | |
#else | |
#ifdef _WIN32 | |
#include <windows.h> | |
void setCursor( COORD cursor ) | |
{ | |
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cursor ); | |
} | |
void clearScreen( ) | |
{ | |
// Get the console handle | |
HANDLE console = GetStdHandle( STD_OUTPUT_HANDLE ); | |
CONSOLE_SCREEN_BUFFER_INFO info; | |
// Get console screen buffer info | |
GetConsoleScreenBufferInfo( console, &info ); | |
COORD topLeft = { 0, 0 }; | |
// Required as the function writes the number of cells written even if we don't care | |
DWORD written; | |
// Sets the upper bytes. '\0' also works I guess | |
FillConsoleOutputCharacter( console, ' ', info.dwSize.X * info.dwSize.Y, topLeft, &written ); | |
// Sets the lower bytes. | |
FillConsoleOutputAttribute( console, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE, info.dwSize.X * info.dwSize.Y, topLeft, &written ); | |
setCursor( topLeft ); | |
} | |
#endif | |
#endif | |
// Saviour from infinite loops | |
void clearBuffer() | |
{ | |
int c = getchar(); | |
while (c != '\n' && c != EOF) | |
c = getchar(); | |
} | |
void inputErr() | |
{ | |
clearBuffer(); | |
printf("\033[1;31m"); | |
printf("\t\tERROR: Check before SMASHING enter"); | |
printf("\033[0m"); | |
} | |
void showMenu() | |
{ | |
printf("\n\n\n"); | |
printf("\t\t╭————[ Numerical Operations ]————╮\n"); | |
printf("\t\t│ 1. Addition │\n"); | |
printf("\t\t│ 2. Prime Search │\n"); | |
printf("\t\t│ 3. Area of Circle │\n"); | |
printf("\t\t│ 4. Divisor Finder │\n"); | |
printf("\t\t│ 5. Exponential │\n"); | |
printf("\t\t│ 6. Factorial │\n"); | |
printf("\t\t│ 7. Fibonacci │\n"); | |
printf("\t\t│ 8. Matrix Generator │\n"); | |
printf("\t\t│ 9. Pascal's Triangle │\n"); | |
printf("\t\t│ 10. Odd Even Columns │\n"); | |
printf("\t\t│ 0. EXIT │\n"); | |
printf("\t\t╰————————————————————————————————╯\n"); | |
//ref: https://en.wikipedia.org/wiki/Box-drawing_character | |
} | |
void afterResult() | |
{ | |
printf("\n\t\tPress enter to continue..."); | |
getchar(); | |
clearBuffer(); | |
clearScreen(); | |
} | |
void addition() | |
{ | |
long input, sum = 0; | |
printf("\n\n\n"); | |
printf("\t\tAddition ==============================\n"); | |
printf("\t\tEnter 0 to stop\n\t\tyou can also use -ve int (its not a bug)\n"); | |
printf("\t\t\n"); | |
for (int i = 0; i <= 30 && input != 0; i++) | |
{ | |
printf("\t\tEnter Number: "); | |
if (scanf("%ld", &input)) | |
; | |
else | |
{ | |
clearBuffer(); | |
inputErr(); | |
break; | |
} | |
printf("\033[A"); | |
printf("\033[A"); | |
sum = sum + input; | |
printf("\t\t+ %ld \n", input); | |
printf("\t\t= %ld \n", sum); | |
} | |
afterResult(); | |
} | |
void prime() | |
{ | |
int flag = 1; | |
long a, i, j; | |
printf("\n\n\n"); | |
printf("\t\tPrime Search ===============\n\n"); | |
printf("\t\tEnter Number: "); | |
if (scanf("%ld", &a)) | |
for (i = 2; i <= a; i++) | |
{ | |
flag = 1; | |
for (j = 2; j <= i / 2; j++) | |
{ | |
if (i % j == 0) | |
{ | |
flag = 0; | |
break; | |
} | |
} | |
// flag = 1 means i is prime | |
// and flag = 0 means i is not prime | |
if (flag == 1) | |
printf("\t\t%ld \n", i); | |
} | |
else | |
{ | |
inputErr(); | |
} | |
afterResult(); | |
} | |
void circle() | |
{ | |
int r; | |
long area; | |
printf("\n\n\n"); | |
printf("\t\tArea of Circle =====================\n\n"); | |
printf("\t\tEnter Radius: "); | |
if (scanf("%d", &r)) | |
{ | |
area = 3.1471 * r * r; | |
printf("\t\tArea of the circle is %ld", area); | |
} | |
else | |
{ | |
inputErr(); | |
} | |
afterResult(); | |
} | |
void divisor() | |
{ | |
long long a, i; | |
printf("\n\n\n"); | |
printf("\t\tDivisor Finder ================\n\n"); | |
printf("\t\tEnter Number: "); | |
if (scanf("%lld", &a)) | |
{ | |
printf("\t\tDivisors of %lld are:\n", a); | |
for (i = 1; i <= a; i++) | |
{ | |
if (a % i == 0) | |
{ | |
printf("\t\t%lld\n", i); | |
} | |
} | |
} | |
else | |
{ | |
inputErr(); | |
} | |
afterResult(); | |
} | |
void exponential() | |
{ | |
int a; | |
float b; | |
printf("\n\n\n"); | |
printf("\t\tExponential ===========================\n\n"); | |
printf("\t\tEnter Base: "); | |
scanf("%d", &a); | |
printf("\t\tEnter Exponential: "); | |
scanf("%f", &b); | |
printf("\t\tProcessing...\n"); | |
printf("\033[A"); | |
printf("\t\t%d ^ %lf = %f \n", a, b, pow(a, b)); | |
afterResult(); | |
} | |
void factorial() | |
{ | |
int input, i; | |
long double fact = 1; | |
printf("\n\n\n"); | |
printf("\t\tFactorial ===========================\n\n"); | |
printf("\t\tEnter number : "); | |
if (scanf("%d", &input)) | |
{ | |
printf("\t\tProcessing...\n"); | |
printf("\033[A"); | |
if (input < 0) | |
{ | |
printf("\t\tFactorial of negative numbers does not exists!"); | |
} | |
else | |
{ | |
for (i = 1; i <= input; i++) | |
{ | |
fact = fact * i; | |
} | |
printf("\t\t%d! = %Lg ", input, fact); | |
} | |
} | |
else | |
{ | |
inputErr(); | |
} | |
afterResult(); | |
} | |
void fibonacci() | |
{ | |
int a; | |
unsigned long long t1 = 0, t2 = 1, temp; | |
printf("\n\n\n"); | |
printf("\t\tFibonacci ===========================\n\n"); | |
printf("\t\tEnter number: "); | |
if (scanf("%d", &a)) | |
{ | |
for (int i = 1; t1 <= a; ++i) | |
{ | |
printf("\t\t%llu\n", t1); | |
temp = t1 + t2; | |
t1 = t2; | |
t2 = temp; | |
} | |
} | |
else | |
{ | |
inputErr(); | |
} | |
afterResult(); | |
} | |
void matrix() | |
{ | |
int s, i, j; | |
printf("\n\n\n"); | |
printf("\t\tMatrix Generator =====================\n\n"); | |
printf("\t\tEnter Size: "); | |
if (scanf("%d", &s)) | |
{ | |
printf("\n"); | |
for (i = 0; i < s; i++) | |
{ | |
printf("\t\t"); | |
for (j = 0; j < s; j++) | |
{ | |
printf("%2d ", i + j); | |
} | |
printf("\n"); | |
} | |
if (s > 25) | |
{ | |
printf("\n\t\tWhat did you expect\n"); | |
printf("\t\tEnter a value <26 to see a proper output\n"); | |
} | |
} | |
else | |
{ | |
inputErr(); | |
} | |
afterResult(); | |
} | |
//ref: https://www.programiz.com/c-programming/examples/pyramid-pattern | |
void triangle() | |
{ | |
int rows, coef = 1, space, i, j; | |
printf("\n\n\n"); | |
printf("\t\tPascal's Triangle =====================\n\n"); | |
printf("\t\tEnter Size: "); | |
if (scanf("%d", &rows)) | |
{ | |
for (i = 0; i < rows; i++) | |
{ | |
printf("\t\t"); | |
for (space = 2; space <= rows - i; space++) | |
printf(" "); | |
for (j = 0; j <= i; j++) | |
{ | |
if (j == 0 || i == 0) | |
coef = 1; | |
else | |
coef = coef * (i - j + 1) / j; | |
printf("%4d", coef); | |
} | |
printf("\n"); | |
} | |
if (rows > 25) | |
{ | |
printf("\n\t\tWhat did you expect\n"); | |
printf("\t\tEnter a value <26 to see a proper output\n"); | |
} | |
} | |
else | |
{ | |
inputErr(); | |
} | |
afterResult(); | |
} | |
void OddEven() | |
{ | |
int end, i; | |
printf("\n\n\n"); | |
printf("\t\tOdd Even Columns =====================\n\n"); | |
printf("\t\tEnter Size: "); | |
if (scanf("%d", &end)) | |
{ | |
printf("\t\t EVEN ODD\n"); | |
for (i = 0; i < end; i++) | |
{ | |
if (i % 2 == 0) | |
{ | |
printf("\t\t%6d ", i); | |
} | |
else | |
{ | |
printf("%6d\n", i); | |
} | |
} | |
if (end % 2 == 0) | |
{ | |
printf("\t\t%6d< %6d\n", i, i + 1); | |
} | |
else | |
{ | |
printf("%6d<\n", i); | |
} | |
} | |
else | |
{ | |
inputErr(); | |
} | |
afterResult(); | |
} | |
void main() | |
{ | |
int choice = 1; | |
while (1) | |
{ | |
clearScreen(); | |
showMenu(); | |
if (choice >= 0 && choice < 11) | |
{ | |
printf("\t\t Enter Choice: "); | |
} | |
else | |
{ | |
printf("\033[1;31m"); | |
printf("\t\t Check before SMASHING enter: "); | |
printf("\033[0m"); | |
} | |
if (scanf("%d", &choice)) | |
; | |
else | |
{ | |
clearBuffer(); | |
choice = 99; | |
} | |
clearScreen(); | |
switch (choice) | |
{ | |
case 0: | |
exit(0); | |
break; | |
case 1: | |
addition(); | |
break; | |
case 2: | |
prime(); | |
break; | |
case 3: | |
circle(); | |
break; | |
case 4: | |
divisor(); | |
break; | |
case 5: | |
exponential(); | |
break; | |
case 6: | |
factorial(); | |
break; | |
case 7: | |
fibonacci(); | |
break; | |
case 8: | |
matrix(); | |
break; | |
case 9: | |
triangle(); | |
break; | |
case 10: | |
OddEven(); | |
break; | |
case 75 - 6: | |
afterResult(); | |
break; | |
default: | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment