Monday, September 5, 2011

Sample C Program


/*Program to add three numbers and calculate   them average*/

 #include<stdio.h>
 #include<conio.h>
 void main()
 {
  //first we declare of variable
  int n1=10;
  int n2=20;
  int n3=30;
  int sum,avg;
  clrscr();
    sum=n1+n2+n3;
    avg=sum/3;
    printf("\nSum Of Threes numbers is %d",sum);
    printf("\nAverage of  n1,n2,n3=%d",avg);
 }
 Output of the above program : 

 Sum Of Threes numbers is 60
 Average of n1,n2,n3=20


Let's discuss some useful tips of above program :
  • In C language there are two types of comments written as :
    1. Single line comment:single line comment give by two forward slash (//).Example : // this is single line comment
    2. Multi line comment: Multi line comment give by this form : /* it is a multi line comment and its can be spilt into several lines.*/
  • Comments can't be nested.
  • main() is a function and gives to a set of statement. If a function is void then it is nothing return otherwise its always return value.
  • Any variable used in the program must be declared before using it.
  • printf() function: It is used to print the value at console. The syntax of printf() as following:                                     printf("<format string>",<variable name>);                   
    format string can be following type:
    %d=printing integer values
    %f=printing real values
    %c=printing character values
  •   "\n" is one of the several escape sequence in C.It is force to cursour go to next line.

No comments:

Post a Comment