C language widely becomes popular in the world. c language is basic of all other programming languages afterword syntax of different programming will differ but the logic to make a calculator using any programming language remains same, that's why in today article I am going to teach to that how you can make a simple calculator using a c programming language.
Coding for making a simple calculator is very simple. By using your simple calculator which is made on your own, you can perform Addition, Subtraction, Multiplication, and Division. To make a simple calculator you should use programing languages. I make a simple calculator using C programming.
What is Programming Language:-
Programming Language Allows us to communicate with computers, using programming language we are given the task to a computer for specific purposes like maths calculation taking a decision, image processing and many more.
Means instructions to perform any task are given to the computer, and the computer will work based on its instructions.
There are many programming languages available in the market like Low-level language, middle-level language, and high-level language.
Following are topics of major programming language:-
- condtion
- looping
- data structure
- go to statement
- class
- object
- function
- database
Low-level language:- That support machine code (assembly languages) to communicate with the computer
these languages are almost absolute today.
- These languages are very difficult to write and were very difficult to understand.
Example - Assembly Languages, COBOL, etc.
- Consume lots of power to process and consume lots of time for processing
- No hardware-friendly.
- No portability
- Does not support upgradations
Middle-Level Language:- Middle-level programming languages that support both the features of high-level languages and low-level languages it also support portability of code and are more realistic than low-level languages and also these languages are user friendly ( means these languages are English like languages that are easy to code and easy to build logic )
Middle-level language is C programming language.
High-Level Language:-
High-level programming languages that are easy to code and easy to understand.
These programming languages are English-like languages, we can make applications using a high-level language.
Why C language is a Middle-level language.
C language supports both the features of low-level language and the features of high-level Language
means using c language use can make an operating system and also user can make an application in c language. that is the reason why c language is a middle-level language.
What is C Language:-
C Language is a Programming language that supports making operating systems and making applications we are writing code in c language to make an operating system or to make an android application.
C language is a general-purpose programming language.c language is a basic for any programming language. This means all the programming languages based on c language .sysntax of different programming languages may differ but topics and logic for all programming languages will remain the same.
Following are topics of c language:-
condition:-
In c language decisions are handled by
if.. statement -
used when there is only one condition are decision has to be taken based on only one condition, here if the condition is specified in the if statement then blocks of if statement will execute.
if..else.. -
used when there is only one condition are decision has to be taken based on only one condition, in here if the condition specified in if statement then block of if statement will execute if the condition specified in if the statement becomes false then else block will execute
else... if ladder -
used when there is only one condition are decision has to be taken based on any multiple conditions, in here if the condition is specified in the if statement then a block of if statement will execute.
if the condition specified in if the state becomes false then check for next condition specified in else if statement if the condition specified in else it becomes true then block of else it will execute if any of the condition specified in else if statement does not true then else part of else if ladder will execute.
switch case -
used when there is only one condition are decision has to be taken based on only one condition or expression, in here expression, variable or result of condition will math with a case of switch case statement if any of the case matches, then the block of the matched case will execute,
if any of the cases will not match then the block of default will execute
looping:- if c language looping is done with the help of (for and while keyword)
data structure:- done using an array
go to statement:- done using Go to statement
Code for Simple Calculator :
#include<stdio.h>
#include<string.h>
int main()
{
float no1,no2;
char str[100],choise,ch; printf("\t\t\t******Calculator******");
abc:
printf("\n\nPerform operator what you want to perform in Simple calculator under (+,-,*,/)\n");
fflush(stdin);
scanf("%d",&no1);
fflush(stdin);
scanf("\n%c",&choise);
fflush(stdin);
scanf("\n%d",&no2);
if(choise == '+')
printf("=%d",no1+no2);
else if(choise == '-')
printf("=%d",no1-no2);
else if(choise == '*')
printf("=%d",no1*no2);
else
{
if(no2==0)
printf("\n Can't devide by 0");
else
printf("=%f",no1/no2);
}
xyz:
printf("\n\nwrite again for use of calculator again / write exit for want not use of calculator :"); scanf("%s",str);
if(strcmp(str,"again")==0)
goto abc;
else if(strcmp(str,"exit")==0)
printf("\n ----Thanks for visit---");
else
{
printf("\nTry again !! ");
goto xyz;
}
return 0;
}
Output :
Explanation of the above code :
I use to read data from a user of getting number 1. after then I get the operation of what we want to perform from the user. I get the second number from the user to perform the operation. I take two numbers as a float because sometimes division performs on the float. I check the condition for entering operation which is entered by a user to perform a specific operation. if the user enters the second number 0 in the division then I set the effect to can't divide by 0 . if I set the effect to repeat operation for the desire that. If this blog becomes useful to you, subscribe for more latest updates. Thank you.
0 Comments