C PROGRAMMING
COMPILED BY ARUP ROY CHOUDHURY, ASSOCIATE PROFESSOR, DEPARTMENT OF MATHEMATICS, MALDA COLLEGE, MALDA
C program: Operators
C-Program
Example: An amount of rupees, say P, is deposited in a commercial bank for N years, which pays simple interest at the rate of R% annually. Write a C program that prints the amount after N years.
Solution: Flow Chart of the problem:
Start
Input P, N, R
Calculate A=P(1+N*R/100)
Print the amount A
End
C Program
/*Principal and Interest Calculation*/ /*Documentation Section*/
#include<stdio.h> /* Link Section*/
main( ) /* Function Section*/
{
float P, N, R, A; /*Declaration Section*/
printf(“\n Enter the deposited amount, year and rate of interest:”);
scanf(”%d %d %d”, &P, &N, &R); /* Input Section*/
A = P*(1+ N*R/100); /* Executable Section*/
printf(“\n The total amount after %.1f years is %.2f” N, A); /*Output Section*/
}
C Program
Rules to be followed to write a C program:
C Program
within opening brace ‘{‘ and closing brace ‘}’.
5) Always lower case letters to be used in a C program.
6) Every program statement in a C language must end with a semicolon.
7) Compiler directives such as define and include do not end with a semicolon.
8) All variables must be declared for their types before they are used in the program within main( ).
Control Statements: In C, some control statements are used to avoid execution of a statement block. These are as follows:
If statement: Whenever we need to transfer control at the time of execution of a program depending on some test conditions if statements are used. This is a two way control statement. There are four types of if statement:
C Program
i) Simple if statement ii) if-else statement iii) Nested if-else statement iv) If-else-if statement.
if( test condition )
{ true
statement block; false
}
statement block;
Example: Write a C program to identify the even numbers.
Solution: /* Processing of even numbers*/
#include<stdio.h>
C Program
main( )
{
int n;
printf(“\n Enter a number:”);
scanf(“%d”,&n);
if(n%2==0)
printf(“\n The number is even.”);
}
ii) We write the same program by using if-else statement.
C Program
/* Processing of even and odd numbers*/
#include<stdio.h>
main( )
{
int n;
printf(“\n Enter a number:”);
scanf(“%d”,&n);
if(n%2==0)
printf(“\n The number %d is even”,n);
else
printf(“\n The number %d is odd”,n);
}