Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.42 KB | None | 0 0
  1.  
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<math.h>
  5. #include<stdlib.h>
  6. #define PI 3.14159265
  7. #define KEY "Enter the calculator Operation you want to do:"
  8. void addition();
  9. void subtraction();
  10. void multiplication();
  11. void division();
  12. void modulus();
  13. void power();
  14. int factorial();
  15. void calculator_operations();
  16. void root();
  17. void exponent();
  18. void logten();
  19. void sine();
  20. void cosine();
  21. void tangent();
  22. void secant();
  23. void cosec();
  24. int main()
  25. {
  26.     system("color 4E");
  27.     printf("\t\t\t\t===========================================================\n");
  28.     printf("\t\t\t\t::=======================================================::\n");
  29.     printf("\t\t\t\t::             PROJECT NAME:SIMPLE CULCULATOR            ::\n");
  30.     printf("\t\t\t\t::                  Istaiq Ahmed-ID:19105008             ::\n");
  31.     printf("\t\t\t\t::                  Farhan Labib-ID:19105025             ::\n");
  32.     printf("\t\t\t\t::=======================================================::\n");
  33.     printf("\t\t\t\t===========================================================\n");
  34.     printf("press any key to clear screen:");
  35.     char ch;
  36.     fflush(stdin);
  37.     scanf("%c",&ch);
  38.     if(ch=='c')
  39.     {
  40.         system("cls");
  41.     }
  42.     system("COLOR 70");
  43.     int X=1;
  44.     char Calc_oprn;
  45.     calculator_operations();
  46.  
  47.     while(X)
  48.     {
  49.         printf("\n");
  50.         printf("%s ", KEY);
  51.         Calc_oprn=getche();
  52.  
  53.         switch(Calc_oprn)
  54.         {
  55.         case '+':
  56.             addition();
  57.             break;
  58.  
  59.         case '-':
  60.             subtraction();
  61.             break;
  62.  
  63.         case '*':
  64.             multiplication();
  65.             break;
  66.  
  67.         case '/':
  68.             division();
  69.             break;
  70.  
  71.         case '?':
  72.             modulus();
  73.             break;
  74.  
  75.         case '!':
  76.             factorial();
  77.             break;
  78.  
  79.         case '^':
  80.             power();
  81.             break;
  82.         case 'H':
  83.         case 'h':
  84.             calculator_operations();
  85.             break;
  86.         case 'r':
  87.             root();
  88.             break;
  89.         case 'e':
  90.             exponent();
  91.             break;
  92.         case 'l':
  93.             logten();
  94.             break;
  95.         case 's':
  96.             sine();
  97.             break;
  98.         case 'k':
  99.             cosine();
  100.             break;
  101.         case 't':
  102.             tangent();
  103.             break;
  104.         case 'n':
  105.             cotangent();
  106.             break;
  107.         case 'a':
  108.             secant();
  109.             break;
  110.         case 'b':
  111.             cosec();
  112.             break;
  113.  
  114.         case 'Q':
  115.         case 'q':
  116.             exit(0);
  117.             break;
  118.         case 'c':
  119.         case 'C':
  120.             system("cls");
  121.             calculator_operations();
  122.             break;
  123.  
  124.         default :
  125.             system("cls");
  126.  
  127.             printf("\n**********You have entered unavailable option");
  128.             printf("***********\n");
  129.             printf("\n*****Please Enter any one of below available ");
  130.             printf("options****\n");
  131.             calculator_operations();
  132.         }
  133.     }
  134. }
  135.  
  136. //Function Definitions
  137.  
  138. void calculator_operations()
  139. {
  140.  
  141.     printf("\n             Welcome to C calculator \n\n");
  142.  
  143.     printf("***** Press 'Q' or 'q' to quit ");
  144.     printf("the program ********\n");
  145.     printf("*** Press 'H' or 'h' to display ");
  146.     printf("below options *****\n\n");
  147.     printf("Enter 'C' or 'c' to clear the screen and");
  148.     printf(" display available option \n\n");
  149.  
  150.     printf("Enter + symbol for Addition \n");
  151.     printf("Enter - symbol for Subtraction \n");
  152.     printf("Enter * symbol for Multiplication \n");
  153.     printf("Enter / symbol for Division \n");
  154.     printf("Enter ? symbol for Modulus\n");
  155.     printf("Enter ^ symbol for Power \n");
  156.     printf("Enter ! symbol for Factorial \n");
  157.     printf("Enter r symbol for square root \n");
  158.     printf("Enter e symbol for exponent \n");
  159.     printf("Enter l symbol for logten \n");
  160.     printf("Enter s symbol for sine \n");
  161.     printf("Enter k symbol for cosine \n");
  162.     printf("Enter t symbol for tangent \n");
  163.     printf("Enter n symbol for cotangent \n");
  164.     printf("Enter a symbol for secant \n");
  165.     printf("Enter b symbol for cosec \n");
  166.  
  167. }
  168.  
  169. void addition()
  170. {
  171.     int n, total=0, k=0, number;
  172.     printf("\nEnter the number of elements you want to add:");
  173.     scanf("%d",&n);
  174.     printf("Please enter %d numbers one by one: \n",n);
  175.     while(k<n)
  176.     {
  177.         scanf("%d",&number);
  178.         total=total+number;
  179.         k=k+1;
  180.     }
  181.     printf("Sum of %d numbers = %d \n",n,total);
  182. }
  183.  
  184. void subtraction()
  185. {
  186.     int a, b, c = 0;
  187.     printf("\nPlease enter first number  : ");
  188.     scanf("%d", &a);
  189.     printf("Please enter second number : ");
  190.     scanf("%d", &b);
  191.     c = a - b;
  192.     printf("\n%d - %d = %d\n", a, b, c);
  193. }
  194.  
  195. void multiplication()
  196. {
  197.     int a, b, mul=0;
  198.     printf("\nPlease enter first numb   : ");
  199.     scanf("%d", &a);
  200.     printf("Please enter second number: ");
  201.     scanf("%d", &b);
  202.     mul=a*b;
  203.     printf("\nMultiplication of entered numbers = %d\n",mul);
  204. }
  205.  
  206. void division()
  207. {
  208.     int a, b, d=0;
  209.     printf("\nPlease enter first number  : ");
  210.     scanf("%d", &a);
  211.     printf("Please enter second number : ");
  212.     scanf("%d", &b);
  213.     d=a/b;
  214.     printf("\nDivision of entered numbers=%d\n",d);
  215. }
  216.  
  217. void modulus()
  218. {
  219.     int a, b, d=0;
  220.     printf("\nPlease enter first number   : ");
  221.     scanf("%d", &a);
  222.     printf("Please enter second number  : ");
  223.     scanf("%d", &b);
  224.     d=a%b;
  225.     printf("\nModulus of entered numbers = %d\n",d);
  226. }
  227.  
  228. void power()
  229. {
  230.     double a,num, p;
  231.     printf("\nEnter two numbers to find the power \n");
  232.     printf("number: ");
  233.     scanf("%lf",&a);
  234.  
  235.     printf("power : ");
  236.     scanf("%lf",&num);
  237.  
  238.     p=pow(a,num);
  239.  
  240.     printf("\n%lf to the power %lf = %lf \n",a,num,p);
  241. }
  242.  
  243. int factorial()
  244. {
  245.     int i,fact=1,num;
  246.  
  247.     printf("\nEnter a number to find factorial : ");
  248.     scanf("%d",&num);
  249.  
  250.     if (num<0)
  251.     {
  252.         printf("\nPlease enter a positive number to");
  253.         printf(" find factorial and try again. \n");
  254.         printf("\nFactorial can't be found for negative");
  255.         printf(" values. It can be only positive or 0  \n");
  256.         return 1;
  257.     }
  258.  
  259.     for(i=1; i<=num; i++)
  260.         fact=fact*i;
  261.     printf("\n");
  262.     printf("Factorial of entered number %d is:%d\n",num,fact);
  263.     return 0;
  264. }
  265. void root()
  266. {
  267.     double first;
  268.     printf("\nPlease Enter Only One Value: ");
  269.     scanf("%lf",&first);
  270.     printf("sprt(%.1lf) = %.1lf", first,sqrt(first));
  271. }
  272. void exponent()
  273. {
  274.     float x;
  275.     printf("\nEnter one number to find exponent: ");
  276.     scanf("%f",&x);
  277.     printf("%.2f",exp(x));
  278. }
  279. void logten()
  280. {
  281.     double x;
  282.     printf("\nEnter one number to find logten: ");
  283.     scanf("%lf",&x);
  284.     printf("%.2lf",log10(x));
  285. }
  286. void sine()
  287. {
  288.     double x,d,val;
  289.     printf("\nEnter one number to find sin: ");
  290.     scanf("%lf",&x);
  291.     if(x==90||x==180||x==0)
  292.     {
  293.         val = PI / 180;
  294.         d=sin(x*val);
  295.         printf("%.2lf",round(d));
  296.  
  297.     }
  298.     else
  299.     {
  300.         val = PI / 180;
  301.         d=sin(x*val);
  302.         printf("%.2lf",d);
  303.     }
  304.  
  305. }
  306. void cosine()
  307. {
  308.     double x,d,val;
  309.     printf("\nEnter one number to find cos: ");
  310.     scanf("%lf",&x);
  311.     val = PI / 180;
  312.     d=cos(x*val);
  313.     printf("%.2lf",d);
  314. }
  315. void tangent()
  316. {
  317.     double x,d,val;
  318.     printf("\nEnter one number to find tan: ");
  319.     scanf("%lf",&x);
  320.     if(x==90||x==270)
  321.     {
  322.         printf("Math ERROR!");
  323.     }
  324.     else
  325.     {
  326.         val = PI / 180;
  327.         d=tan(x*val);
  328.         printf("%.2lf",d);
  329.     }
  330.  
  331. }
  332. void cotangent()
  333. {
  334.     double x,d,val;
  335.     printf("\nEnter one number to find cot: ");
  336.     scanf("%lf",&x);
  337.     if(x==0||x==180||x==360)
  338.     {
  339.         printf("Math ERROR!");
  340.     }
  341.     else
  342.     {
  343.         val = PI / 180;
  344.         d=tan(x*val);
  345.         printf("%.2lf",1/d);
  346.     }
  347. }
  348. void secant()
  349. {
  350.     double x,d,val;
  351.     printf("\nEnter one number to find sec: ");
  352.     scanf("%lf",&x);
  353.     if(x==90||x==270)
  354.     {
  355.         printf("Math ERROR!");
  356.     }
  357.     else
  358.     {
  359.         val = PI / 180;
  360.         d=cos(x*val);
  361.         printf("%.2lf",1/d);
  362.     }
  363. }
  364. void cosec()
  365. {
  366.     double x,d,val;
  367.     printf("\nEnter one number to find cosec: ");
  368.     scanf("%lf",&x);
  369.     if(x==0||x==180||x==360)
  370.     {
  371.         printf("Math ERROR!");
  372.     }
  373.     else
  374.     {
  375.         val = PI / 180;
  376.         d=sin(x*val);
  377.         printf("%.2lf",1/d);
  378.     }
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement