Showing posts with label Calculator_using_if_else. Show all posts
Showing posts with label Calculator_using_if_else. Show all posts

C program to make Area calculator

 In this post ,I will let you know how to make  Area calculator. So, without any further delay let's get started .

Code:

#include <stdio.h>
int main()
{
    char Shape;
    float radius, side, length, breadth, base, height;
    float areaC, areaS, areaR, areaT;
    printf("Enter shape symbol for which you want Area(Circle=C,Square=S,Rectangle=R,Triangle=T)\n");
    scanf("%c", &Shape);
    if (Shape == 'c' || Shape == 'C')
    {printf("Enter radius of circle\n");
        scanf("%f", &radius);
        areaC = 3.14 * radius * radius;
        printf("Area of circle=%f\n", areaC);
    }
    else if (Shape == 'S' || Shape == 's')
    {printf("Enter side of Square\n");
        scanf("%f", &side);
        areaS = side * side;
        printf("Area of square=%f\n", areaS);
    }
    else if (Shape == 'R' || Shape == 'r')
    {printf("Enter Length and Breadth of Rectangle \n");
        scanf("%f %f", &length, &breadth);
        areaR = length * breadth;
        printf("Area of Rectangle=%f\n", areaR);
    }
    else if (Shape == 'T' || Shape == 't')
    {printf("Enter Base and Height of Triangle\n");
    scanf("%f %f", &base, &height);
        areaT = 0.5 * base * height;
        printf("Area of Triangle=%f\n", areaT);
    }
    return 0;

Logic:

Now let us first understand how algorithm for this .Here we make calculator only for four Shapes  i.e. Circle ,Square ,Rectangle and Triangle .

For that we must know formula of all these .Below are formula for these:

    Circle:  Area=pi*radius*radius  ,pi=3.14

     Square: Area=Side*Side 

      Rectangle: Area=Length*Breadth

      Triangle : Area=1/2*Base *Height

Know more about if-else.  After this its time to understand each and everything about this program ,so that you don't left any doubt after reading this .First we declare variable Shape of type character  and some float variables radius for circle ,side for square ,length and breadth for Rectangle and for Triangle base and height. And other variables areaC for Circle ,areaS for square ,areaR for rectangle and areaT for triangle of type float . Now we ask user to enter just symbol of shape C for Circle and R for rectangle , S for Square  and T for Triangle . And then we use if (Shape =='C' || Shape =='c') ,that means we check  if it is equal to C (capital letter ) or in c(small letter) then execute it for Circle .Here || is OR operator  means if any of them is true then execute code .Now continue it printf() inside if gets executed and ask for Radius from user .Then he enters radius and program work and print area of Circle. And this work for all Square , Rectangle and Triangle. 
Output:                                                                                                                                                                                                                   


Here you can see output ,it works as  we think .It take symbol of Shape and then ask for Requirements to calculate its area. And give us correct output . 

                            I hope you might  find this helpful  in your work.

                                                         Thanks for reading 

C program to make Calculator

In this I will tell you how to make a simple calculator just by using if-else in c language .First I tell you about if-else condition .You can easily understand it let us consider an example if(age>18) Then you can ride bike . This condition you have to meet in order to ride bike similarly in programming .If condition is  true then you should do this other wise check for next condition .Using this we can also design calculator 

Code:

#include <stdio.h>

int main()
{
    float number1, number2;
    char sign;
    printf("Enter number1\n");
    scanf("%f", &number1);
    printf("Enter number second\n");
    scanf("%f", &number2);
     printf("Enter operator(*,-,+,/)\n");
    scanf(" %c", &sign);

    if (sign == '+')
    {
        printf("\nSum : %.2f", number1 + number2);
    }
    else if (sign == '-')
    {
        printf("\nDifferece: %.2f", number1 - number2);
    }
    else if (sign == '*')
    {
        printf("\nMultiply: %.2f", number1 * number2);
    }
    else if (sign == '/')
    {
        printf("\nDivision : %.2f", number1 / number2);
    }
    return 0;
}

Logic:

Now I will tell you what I do here .First I define three variables number1,number2 of type float and sign is of type character .If you does not know what variable is you can check my previous post where I do brief coverage of this topic .You can check here some brief Idea .Now back to this then I take value 

of sign variable which can be(*,+,-,/) these are basic operator in c language. Then I ask user to enter number1 and number2.Then using if condition I check for every operator user can enter. Then If match is found then perform that operation .And in each match condition I give a print statement to output to user .

Output:

Here I show you output how it give us result . Make this program on your pc and run it .


I hope you might find this helpful . 
Thanks for reading