Smile

Thursday, August 25, 2011

C program Examples

Example-1

C Program for converting temperature from Celsius to Fahrenheit

#include<stdio.h>
#include<conio.h>

void main()

{

float c,f;

clrscr();

printf("Enter the temperature in celsius:");

scanf("%f",&c);

f=c*9/5+32;

printf("Temperature in Fahrenheit=%.2f",f);

getch();

}

Example-2

C Program for printing grade to the student based on the percentages given below

>75-------> A+

70-75-----> A

60-70----->B

below 60-->C

#include<stdio.h>
#include<conio.h>

void main()

{

int m,p,c,sum;

float avg;

clrscr();

x: printf("Enter the marks Maths Physics Chemistry:");

scanf("%d%d%d",&m,&p,&c);

if(m<0||m>100||p<0||p>100||c<0||c>100)

goto x;

sum=m+p+c;

avg=(float)sum/3;

printf("Maths=%d\nPhysics=%d\nChemistry=%d\nSum=%d\nAverage=%.2f\n",m,p,c,sum,avg);

m<35||p<35||c<35?printf("Fail"):avg>75?printf("Grade=A+"):avg>70?printf("Grade=A"): avg>60?printf("Grade=B"):printf("Grade=C");

getch();

}

Example-3

C Program for calculating income tax based on salary of the employee details given below
Salary----->tax%
----------------------
>500000-------> 10%
300000-500000 -->7%
100000-300000 --->5%
50000-100000 ---->3%
<50000 ---------->0%

#include<stdio.h>
#include<conio.h>

void main()

{

float inc,t,sal;

clrscr();

printf("Enter the salary:");

scanf("%f",&sal);

t=sal>500000?10:sal>300000?7:sal>100000?5:sal>50000?3:0;

inc=sal*t/100;

printf("Tax=%.2f\nIncome tax=%.2f",t,inc);

getch();

}


Wednesday, August 17, 2011

How to Execute a C program

To write a C program, we have Borland Turbo C editor

Borland Turbo C editor

Compile-------> ALT + F9 or F9

Run-----------> CTRL + F9

Output-------> ALT + F5

Trace--------> F7

Sunday, August 14, 2011

Sample C program


Explanation
---------------
//
This is used for single line comment. This portion will not be executed.

/* */
This is used for multiple line comments. This portion will not be executed.

#include<stdio.h> or #include"stdio.h"

#-----> preprocessor directive
stdio.h----> standard input output header file
If header file included between < > , C linker searches header file in predefined library section.
If header file included between " " , C linker searches header file in both predefined library section and user defined library section.

#include<conio.h> or #include"conio.h"
This is console input output header file.

void main()
void--------> This is return type of function means nothing to return.
main()------> This is main function. The execution of C program starts from here. This is mandatory. Without this program returns error.

{ }
Body of the function to be executed. The portion enclosed between {} is said to be block.



Important functions for sample C program

1. printf()
-------------
This function is used to display the messages,variables etc on the output screen or console.
Syntax
-------
printf("messages in double quotes",var1,var2,var3,..............);

2. scanf()
------------
This function is used to read the variables explicitly at run time.
Syntax
-------
scanf("format specifiers",&var1,&var2,........);

3. clrscr()
------------
This function is used to clear the output screen.
Syntax
-------
clrscr();

4. getch()
------------
This function is used to read a single character. This is mainly used to display the output without termination.
Syntax
-------
getch();