Freelancer

Basic calculator using if statement

C Programming Language Example
Calculator

Basic calculator using if statement - C Programming Example

This program is an example of C program code that represents a basic calculator which can perform addition, subtraction, multiplication and division.

C Programming Code


#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
    int x;
    float a,b,sum,sub,mul,div;
    printf("Which mathematical problem would you like to do \n");
    printf("Options 1.Addition 2.Subtraction 3.Multiplication 4.Division \n ");
    scanf("%d",&x);
    if (x==1)
    {

    printf("Input the first number \n");
    scanf("%f",&a);
    printf("Input the second number \n");
    scanf("%f",&b);
    sum=a+b;
    printf("The math is %.2f + %.2f = %.2f",a,b,sum);
    }
    else if (x==2)
    {

    printf("Input the first number \n");
    scanf("%f",&a);
    printf("Input the second number \n");
    scanf("%f",&b);
    sub=a-b;
    printf("The math is %.2f - %.2f = %.2f",a,b,sub);
    }
    else if (x==3)
    {

    printf("The first number is \n");
    scanf ("%f",&a);
    printf("The second number is \n");
    scanf("%f",&b);
    mul= a*b;
    printf("The multiplication of the numbers is = %.2f",mul);
    }
    else if (x==4)
    {


    printf("Input the first number \n");
    scanf("%f",&a);
    printf("Input the second number \n");
    scanf("%f",&b);
    div=a/b;
    printf("The math is %.2f / %.2f = %.2f",a,b,div);
    }
    getch();
}

Output of the code

C Programming Language Examples
Summation

C Programming Language Examples
Subtraction

C Programming Language Examples
Multiplication

C Programming Language Examples
Division

Explanation of the code


·         #include<stdio.h>

#include<stdio.h>
Ø  This are called header file.A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source filesThe C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header <stdio.h>.In the other hand  <conio.h> is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C. This header declares several useful library functions for performing "console input and output" from a program.

·         main()
Ø  In C, program execution starts from the main() function. The main function can in-turn call other functions. When main calls a function, it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of function is reached.

·        int x;
int is a variable or data type.  A variable declared to be of type int can be used for storing    numbers which do not have decimal points. Here we declare x variable in inttype.
·        float a,b,sum,sub,mul,div;
Ø  float is a variable or data type. A variable declared to be of type float can be used for storing numbers which have decimal points. Here we declare a,b,sum,sub,mul,divvariable in float type.
·         printf() function
Ø  C uses printf() function to write from the input devices. This function has been declared in the header file called stdio.h . Any text written within the pair of quotes ("") is displayed as such by printf() function on the screen.
printf("Which mathematical problem would you like to do \n");
printf("Options 1.Addition 2.Subtraction 3.Multiplication 4.Division \n ");

As we are creating this program to act as a basic calculator which only does 4 types of mathematical problems, we first wrote these two lines so that the user can choose their desired mathematical program to run, with the 4 programs set as options from 1-4.
Which mathematical problem would you like to do \n
Options 1.Addition 2.Subtraction 3.Multiplication 4.Division \n
\n is used for showing a the lines below each other like a new line when the program is run.
Then the user will input any one of the numbers from 1-4 and the program will then scan and store it for further use.

·         scanf() function
Ø  This function is used to get input from the user of the program. scanf(“%d”,&x”) is used here. So, this program will read in a integer value for “x” variable that the user enters on the keyboard (%dis for interger values. As there is “&x”, the number user enters will be the value of “x” variable).
As there are 4 conditions here, to run this program NESTED IF LOOPis being used to simplify the program and make is understandable to the learners.

if (x==1)
    {
    printf("Input the first number \n");
    scanf("%f",&a);
    printf("Input the second number \n");
    scanf("%f",&b);
    sum=a+b;
    printf("The math is %.2f + %.2f = %.2f",a,b,sum);
    }

Here as the first option is or option 1 is addition and if the input of the user is 1 , after scanning and storing it the program will run the program in this loop as the condition for the program is set at if (x==1)
After entering the loop itself the code itself is very simple. The user will enter his input in integer/float type variable and the program will store them in a and b respectively, then the sum is declared, sum=a+b and then after coming to printf the results will be shown on the screen.
Note: The sentences in printf can be changed to the preference of the person writing the code.

else if (x==2)
    { 
    printf("Input the first number \n");
    scanf("%f",&a);
    printf("Input the second number \n");
    scanf("%f",&b);
    sub=a-b;
    printf("The math is %.2f - %.2f = %.2f",a,b,sub);
    }
   
Here as the second option is or option 2 is subtraction and if the input of the user is 2 , after scanning and storing it the program will run the program in this loop as the condition for the program is set at else if (x==2)
After entering the loop itself the code itself is very simple. The user will enter his input in integer/float type variable and the program will store them in a and b respectively, then the sub is declared,sub=a-b, and then after coming to printf the results will be shown on the screen.
Note: The sentences in printf can be changed to the preference of the person writing the code.

else if (x==3)
    {
    printf("The first number is \n");
    scanf ("%f",&a);
    printf("The second number is \n");
    scanf("%f",&b);
    mul= a*b;
    printf("The multiplication of the numbers is = %.2f",mul);
    }

Here as the third option is or option 3 is multiplication and if the input of the user is 3 , after scanning and storing it the program will run the program in this loop as the condition for the program is set at else if (x==3)
After entering the loop itself the code itself is very simple. The user will enter his input in integer/float type variable and the program will store them in a and b respectively, then the mul is declared, mul=a*b, and then after coming to printf the results will be shown on the screen.
Note: The sentences in printf can be changed to the preference of the person writing the code.

 else if (x==4)
    {
    printf("Input the first number \n");
    scanf("%f",&a);
    printf("Input the second number \n");
    scanf("%f",&b);
    div=a/b;
    printf("The math is %.2f / %.2f = %.2f",a,b,div);
    }

Here as the fourth option is or option 4 is division and if the input of the user is 4 , after scanning and storing it the program will run the program in this loop as the condition for the program is set at else if (x==4)
After entering the loop itself the code itself is very simple. The user will enter his input in integer/float type variable and the program will store them in a and b respectively, then the div is declared, div=a/b, and then after coming to printf the results will be shown on the screen.
Note: The sentences in printf can be changed to the preference of the person writing the code.
    getch();
And finally getch(): is written to end the program.

You may also like




No comments

Powered by Blogger.