Comments and Simple Interest Program
Comments and Simple Interest Program
Hi,
Theory is always boring but very important for implementation of concepts. Theory on a topic makes you aware of topic and implementation helps to explore the topic.
Now, from previous post we have gone through many kinds of rules, but trust me once you start your independent programming, you will realize that what you read were not rules but just good programming practices which, when practiced helps you to develop good readable programs.
We have read basic rules for constants, variables and keywords, now we will implement the same in our Simple Interest program.
In between we will also learn commenting system in C.
Here is program for calculating Simple Interest:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include<stdio.h> #include<conio.h> void main() { int prin, time; float rate, interest; /*Simple Interest Formula interest=(prin*rate*time)/100*/ /*Read inputs first*/ printf("\nEnter Principal:"); scanf("%d", &prin); printf("\nEnter Annual Interest Rate:"); scanf("%f", &rate); printf("\nEnter Years:"); scanf("%d", &time); //Apply Simple Interest Formula on inputs interest=(prin*rate*time)/100.0; //print interest value on output printf("\nInterest is: %5.2f", interest); getch(); } |
The output looks like:
1 2 3 4 | Enter Principal:1000 Enter Annual Interest Rate:5 Enter Years:2 Interest is:100.00 |
Detailed Explanation:
Here you can find out that I have added comments on actual code from line no.s 8 to 10 and then on line no. 17 and 19.
Why Comments are required?
And what is the significance of comments for a programmer?
Commenting are not necessary but its good to write comments. When program size increases and task is sub-divided into modules, with multiple people working on same modules, proper comments help others to read out program and help us to understand others program.
One should develop habit of writing comments in parallel to learning any programming language.
One should develop habit of writing comments in parallel to learning any programming language.
Here you can find 2 kinds of commenting system
First is /*------------------*/ This is block commenting system where you can write multi-line comments.
/* --->signifies start of comment
STATEMENT-1 TO COMMENT
STATEMENT-2 TO COMMENT
STATEMENT-3 TO COMMENT
STATEMENT-1 TO COMMENT
STATEMENT-2 TO COMMENT
STATEMENT-3 TO COMMENT
*/ ---->signifies end of comment
Between this lies the whole contents of comments.
Specialty of this commenting system is comments can be at the start of statement, end of statement or inline with the statement.
Second is //---------
This is single line comment system. Everything written beyond // is treated as comment and is not executed.
This is single line comment system. Everything written beyond // is treated as comment and is not executed.
//Statement commented
While compiling, compiler straight away ignores the comments and compiles rest of the code.
While compiling, compiler straight away ignores the comments and compiles rest of the code.
Understanding the Program:
The Snippet:
1. #include<stdio.h>
2. #include<conio.h>
3.
4. void main()
This is to include stdio and conio files in your program.
scanf() is declared in stdio.h and getch() is declared in conio.h.
As mentioned in earlier post, main() is the entry point for the program.
main(): by default main() is called by operating system for program execution.
6. int prin, time; 7. float rate, interest;
Concept: In C, all variables should be defined with a data type before using them. Data type can be generic or user defined.
Purpose: Its an indication to compiler about space to be allotted to the variable, and type of data that will be stored on that location.
In our case, In line 6 and 7, variables prin and time are declared with data type as int and variables rate and interest are declared with data type float.
11. printf("\nEnter Principal:"); 12. scanf("%d", &prin); 13. printf("\nEnter Annual Interest Rate:"); 14. scanf("%f", &rate); 15. printf("\nEnter Years:"); 16. scanf("%d", &time);
Concept: In C, printf is used for seeing formatted output on the console.
"\n" is an escape character used to shift cursor to new.
The string in quotes will be printed as it is, except the formatted letters/characters.
In line 11, 13 and 15, the only format specifier is "\\n".
Concept: In C, scanf() is used to take input from the keyboard.
The "%d" or "%f" in scanf() suggests that inputs will be integer and float value respectively. The values will be stored in respective memory location.
In order to point to memory location, "&" operator is used before the variable name.
scanf() will reads one input at a time.
18. interest=(prin*rate*time)/100.0;
Here, the simple interest formula is written and result is stored in "interest" variable.
Concept: here, since prin, rate and time are integers, their multiplication will be an integer and in C when integer is divided by integer, the result is integer only.
So, if, we write,
(prin*rate*time)/100;
The result will be an integer value and will be stored in "interest" variable, with digits after decimal truncated.
Finally, we have to print output on the screen, for this, we will use printf() again, with format specifier.
20. printf("\nInterest is: %5.2f", interest); 21. getch();
In line 20, the value of "interest" will be printed.
%5.2f is the format specifier here. It denotes that, a float variable is to be printed in 5 letters with precision of 2 decimal points only. The variable "interest", is written after comma.
Note: If there are multiple format specifiers, than, variable list should be in order in which they needs to be printed.
Last but not the least. A question which is asked in many interviews is:
Question: Why getch() is used at the end of C program?
Answer: getch() by definition is a function which waits for a character input from user without displaying it on the screen. But, in C its implemented to hold the output screen.
So execution of program don't gets completed when output is displayed, but its completed when you enter a character (any) on console, getch() receives and exits the program.
This completes your Simple Interest Calculation program.
The new terminologies and concepts used here are:
- printf()
- scanf()
- format specifiers
- escape sequence
Wait for detailed blog on these topics
.
Till then,
Thanks.
Prateek !!!
Comments
Post a Comment