Advertisement
tampurus

Unit 1.2 Arithmetic operation on floating point numbers

Mar 13th, 2022 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     float f1,f2,ans;
  6.     printf("Enter two floating point numbers ");
  7.     scanf("%f %f",&f1,&f2);
  8.     int e1 , e2;
  9.     printf("Enter two integer numbers ");
  10.     scanf("%d %d",&e1,&e2);
  11.     // printf("\n%d %d\n",e1,e2);
  12.     printf("\nEnter operator 1 = +, 2 = -, 3 = *, 4 = / ");
  13.     int  c ;
  14.     scanf("%d",&c);
  15.     if (e1>e2){
  16.         while(e1!=e2){
  17.             f2/=10;
  18.             e2++;
  19.         }
  20.     }
  21.     else if (e1<e2){
  22.         while(e1!=e2){
  23.             f1/=10;
  24.             e1++;
  25.         }
  26.     }
  27.     printf("\n%f %d %f %d \n",f1,e1,f2,e2);
  28.     if(c==1) ans = f1+f2;
  29.     else if (c==2)  ans = f1-f2; // here
  30.     else if (c==3)  ans = f1*f2;
  31.     else if (c==4)  ans = f1/f2;
  32.     else {
  33.         printf("Wrong choice");
  34.         return 0;
  35.     }
  36.     int e=e1;
  37.     if (ans>=1){
  38.         while(ans>=1){
  39.             ans/=10;
  40.             e++;
  41.         }
  42.        
  43.     }
  44.     else if (ans<=0.1){
  45.         while(ans<=0.1){
  46.             ans*=10;
  47.             e--;
  48.         }
  49.     }
  50.    
  51.     printf("Result %f X 10^%d",ans,e);
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement