1 of 9

C PROGRAMMING

COMPILED BY ARUP ROY CHOUDHURY, ASSOCIATE PROFESSOR, DEPARTMENT OF MATHEMATICS, MALDA COLLEGE, MALDA

2 of 9

C program: Operators

  •  

3 of 9

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

4 of 9

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*/

}

5 of 9

C Program

Rules to be followed to write a C program:

  1. At first we have to document the program within /* ……..*/. This is optional.
  2. Next we must include header files by using #include directive which we require e.g., for standard I/O function #include<stdio.h>, for some mathematical function #include<math.h>. For conditional I/O #include<conio.h> is used which includes the library functions clrscr( ) and getch( ).
  3. Every C program requires a main( ) function. It is the place where the program execution begins.
  4. In the main( ) section declaration part and executable parts are written

6 of 9

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:

  1. if statement ii) switch statement iii) goto statement.

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:

7 of 9

C Program

i) Simple if statement ii) if-else statement iii) Nested if-else statement iv) If-else-if statement.

  1. Simple if statement: The syntax of the if statement is as follows:

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>

8 of 9

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.

9 of 9

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);

}