Saturday 19 October 2013

Basic question -2 (make a simple calculator)

Make a simple calculator .which can do operATION like addition,subtraction, division,multiplication. using Function.


there is another challenge for beginners that there is an error in program (given below) provided for above question.
that is if we divide two no. if answer coming out must be float, if it is not perfectly divisible.
BUT answer print  on screen is integer .

find out error ..
but before that solve this basic problem by yourselves.

C progam :



 //beginer2cs.blogspot.com  
 #include<stdio.h>  
 #include<stdlib.h>  
 float sum(float a,float b);  
 float diff(float a,float b);  
 float prod(float a,float b);  
 float divis(int a, int b);  
 main()  
 {  
    float m,a,b;  
    int n;  
    printf("inter no between 1 to 4\n 1 for sum\n 2 for diff\n 3 for prod\n 4 for div \n any other no to exit ");  
    while(1)  
    {  
    printf("\n enter ur choice :");  
    scanf("%d",&n);  
    if(n>4 || n<1) // condition to exit program  
    {  
     exit(0);  
    }  
    printf("\n inter a :" );  
    scanf("%f",&a);  
    printf("\n inter b :" );  
    scanf("%f",&b);  
    switch(n)  
    {  
     case 1:  
     m=sum(a,b);  
     break;  
    case 2:  
     m=diff(a,b);  
     break;  
    case 3:  
     m=prod(a,b);  
     break;  
    case 4:  
     m=divis(a,b);  
     break;  
    }  
   printf("\n ans is =%f\n ",m);  
   }  
 }  
 float sum(float a,float b)  
 {  
 return(a+b);  
 }  
 float diff(float a,float b)  
 {  
  return(a-b);  
 }  
 float prod(float a, float b)  
 {  
  return(a*b);  
 }  
 float divis(int a, int b)  
 {  
 return(a/b);  
 }  

No comments:

Post a Comment

THANKS FOR UR GREAT COMMENT

Blogger Widgets