So guys Today we will learn some basic programs in C or C++
Please don't be panic, it's not hard as many think specially beginners.
let's start with a basic program of a simple calculator in C
And if u don't know programming or a have just started programming then probably this is the best place
for u people. I will explain the program as well as basics of C (programming). And ya I know you have just started programming..
Lets have the programme
But before starting the programme let's try to imagine what we have to do in the programme.
Easy guess a calculator will perform add, subtract, blaw, blaw...
But how we will perform it now in the very beginning.
Remember that we don't know how to give user's option like select any option with keyboard and mouse.
Because it's the beginning. So now in the Short time we can perform it with simple inputs from the user.
So like in our programme first we will take two inputs from the user means this is the number or the operands
(Operands are those on which operation is performed) and then we will ask the user (i.e. who is using the program don't laugh now it's u only)
to enter some numbers like 1 for adding those previously input numbers, likewise 2 for subtracting, 3 for multiplying, 4 for dividing.
So as per the users choice if user chooses to add i.e. enter 1 then we will print the sum in the screen.
So here we go
// This is a single line comment
/* This is many line comments
Comments are used by programmers to keep some note for other
programmers or for themselves too.
It is the part in the code which is not executed
So here ends many line comments */
#include<stdio.h>
/* So basically we use #include<stdio.h>
So stdio.h is a file which is to be included before the programme compiles
And we are including this file because it contains information of some keywords
which we will use in the programme like printf, scanf
as we don't understand a new word like wise a computer
too don't understand anything unless specified
so this files are called header files with the extension '.h' */
/*It is the main part of the programme
Because it is the only part which is being executed by the compiler
Compiler: A compiler is a programme which converts the programs to
such a way that a machine can understand it.
So remember if the code does not contain main function then
the programme is not going to be executed */
int main()
{
int choice;
/* Defining a variable called 'choice'. A variable is a named locationfloat a, b, result;
of some memory in the computer or it is a portion of a memory to which
we have given name as choice. You can choose anything in variable but
it must start with underscore `_` or a latter, it must not start with a
number and since special characters too are not allowed in declaring a
variable name.
So here in the code snippet we have written int which signifies that the
value which will be stored in the variable if a integer.
Similarly for characters we use `char`, for floating point decimal numbers
we use float etc. */
/* printf is a function with which we print something in the screen.printf("Enter Your Choice");
And as specified earlier that its definitions is in the header file stdio.h */
/* Remember: \n is user for new line character so that after this `\n` anything written will be printed in the new line */printf("\n1. Add");
printf("\n1. Subtract");
printf("\n1. Multiply");
printf("\n1. Divide");
/* scanf for taking inputs from the user and remember choice is the variable name in which we are storing the value entered by the user.scanf("%d", &choice);
We have used &choice instead of choice because we are assigning the input value to the variable choice */
printf("\nNow enter two numbers");
scanf("%f", &a);
scanf("%f", &b);
/* No explanation needed if else. Means if the condition specified in the if() is true the part of the code written in the { } will be executedif (choice == 1)
Otherwise the else part will be executed
Remember that `=` and `==` are two different things
`=` means the right hand side value will be assigned to the left hand side variable
like a = 2;
whereas `==` means if both the two are equals or not */
{
result = a + b;
printf("%f", result);
/* So inputting 1 means we need to add the numbers and likewise for all and then printing it in the screen */} else if (choice == 2)
{
result = a - b;
printf("%f", result);
} else if (choice == 3)
{
result = a * b;
printf("%f", result);
} else if (choice == 4)
{
result = a / b;
printf("%f", result);
} else
{
printf("Wrong Choice");
}
return 0;
}
So the final program is
#include <stdio.h>
int main()
{
int choice;
float a, b, result;
printf("Enter Your Choice");
printf("\n1. Add");
printf("\n1. Subtract");
printf("\n1. Multiply");
printf("\n1. Divide");
scanf("%d", &choice);
printf("\nNow enter two numbers");
scanf("%f", &a);
scanf("%f", &b);
if (choice == 1)
{
result = a + b;
printf("%f", result);
} else if (choice == 2)
{
result = a - b;
printf("%f", result);
} else if (choice == 3)
{
result = a * b;
printf("%f", result);
} else if (choice == 4)
{
result = a / b;
printf("%f", result);
} else
{
printf("Wrong Choice");
}
return 0;
}
Try the code by clicking here.
So run this programme in ur emulator or here and write ur reviews in the comment box
Thank You
No comments:
Post a Comment