Monday, 25 February 2013

Print numbers from 1 to 10 using for loop


   #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int i;
 clrscr();
 for(i=1;i<=10;i++)
 printf("%d\n",i);
 getch();
 }

Output
1
2
3
4
5
6
7
8
9
10

for loop


C programming loops:
Loops causes program to repeat the certain block of code until some conditions are satisfied. Loops are used in performing repetitive work in programming.

Syntax of for loop:
for(initial expression; test expression; update expression)
{
            codes to be executed;
}

How for loops in C programming works?
The initial expression is initialized only once at the beginning of the loop. Then, the test expression is checked by the program.
If the test expression is false, for loop is terminated.
If the test expression is true then, the codes are executed and update expression is updated. Again, the test expression is checked. If it is false, loop is terminated and if it is true, the same process repeats until test expression is false.
For the easier understanding of how for loops works, analyze the flowchart of loop given below.

Important:
·         Initial, test and update expression in C for loop are separated by semicolon (;)
·         Two or more expression of same type in for loop are separated by comma(,)

Sunday, 24 February 2013

To Find Number is Even or Odd


#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter the Value of a ");
scanf("%d",&a);
if(a%2==0)
printf("a is even");
else
printf("a is odd");
getch();
}

Output
Enter the Value of a
5
a is odd

To Check which number is Maximum by using nested if


#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
clrscr();
printf("Enter the values of a,b,c");
scanf("%f%f%f",&a,&b,&c);
if(a>b && a>c)
printf("a is Maximum");
else
if(b>a && b>c);
printf("b is Maximum");
else
printf("c is Maximum");
getch();
}

Output
Enter the values of a,b,c
3.8
5.2
7.1
c is maximum

To Check which number is Greater by using if . . . else


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the value of a and b");
scanf("%d%d",&a,&b);
if(a>=b)
{
printf("a is Greater");
}
else
{
printf("b is Greater");
}
getch();
}

Output
Enter the value of a and b
4
7
b is Greater

Thursday, 21 February 2013

C Decision & Loops



if….else Statement
Decision making are needed when, the program encounters the situation to choose a particular statement among many statements. C programming provides following statements to make decision makings:
·         if…else statement
·         switch statement

if(test expression)
     statements to be executed if test expression is true;



Nested if…else statement (if…else Statement)
The if…else statement can be used in nested form when a serious decision are involved. The ANSI standard specifies that 15 levels of nesting may be continued.

Syntax of nested if…else Statement:
if(test expression)
     statement to be executed if test expression is true;
else
     if(test expression 1)
          statements to be executed if test expression 1 is true;
     else if
     .
     .
     .
       else
          statements to be executed if all test expressions are false;

How nested if…else works?
If the test expression is true, it will execute the relevant code but, if it is false, the control of the program jumps to the else part and check test expression 1 and the process continues. If all the test expressions are false then, only the last statement is executed.

Return Statement



Return Statement is used for returning a value from function definition to calling function.

Syntax of Return Statement
return(expression);
     OR
return;

For Example:
return;
return a;
return(a+b);

Note that, data type of add is int and also the return type of function is int. the data type of expression in return statement should also match the return type of function.


Tuesday, 19 February 2013

Function Definition



Function definition contains programming codes to perform specific task.

Syntax of function definition:
return_type function_name(type(1) argument(1),……, type(n) argument(n))
{
          //body of definition
}

Function definition has two major components:

1.     Function Declarator
Function declaratory is the first line of function definition. When a function is invoked from calling function, control of the program is transferred to function declaratory or called function.

Syntax of Function Declarator
return_type function_name(type(1) argument(1),….., type(n) argument(n))

Syntax of function declaration and declaratory are almost same except, there is no semicolon at the end of declaratory and function is followed by function body.

2.     Function Body
Function declarator is followed by body of function which is composed of statements.


Passing Arguments to Functions
In programming, argument/parameter is a piece of data (constant or variable) passed from a program to the function.
In below example two variable, num1 and num2 are passed to function during function call and these arguments are accepted by arguments a and b in function definition.


Arguments that are passed in function call and arguments that are accepted in function definition should have some data type. 
For Example:
If argument num1 was of int type and num2 was of float type the, argument should be of int type and b should be of float type in function definition i.e., type of argument during function call and function definition should be same.
A function can be called with or without an argument.

Monday, 18 February 2013

Function Call:



Control of the program cannot be transferred to user-defined function (function declaration) unless it is called (invoked).

Syntax of Function Call

function_name(argument(1),….argument(n));

In this above example, function call is made using statement “add(num1, num2);” in line 8. This makes control of program transferred to function declarator(line 12). In line 8, the value retuned by function is kept into “sum” which you will study in detail in function return type of this chapter.

Sunday, 17 February 2013

Function Prototype (declaration)



Every function in C programming should be declared before they are used. This type of declaration are also called function prototype. Function prototype gives compiler information about function name, type of arguments to be passed and return type.

Syntax of Function Prototype:

return_type function_name(type(1) argument(1),….., type(n) argument(n));

Here, in the above example, function prototype is “int add(int a, int b);” which provides following information to the compiler:
1.      Name of the function is “add”
2.      Return type of the function is int.
3.      Two arguments of type int are passed to function.

Function prototype is not needed, if you write function definition above the main function. In this program if the code from line 12 to line 19 is written above main(), there is no need of function prototype.