Friday, April 29, 2016

C program to show use of User Defined Functions.


This program demonstrate use of FindAverage() function.

#include<stdio.h>
void FindAverage(); // function prototype declaration.
int main()
{
     FindAverage(); //Call function.
     return 0;
}

void FindAverage()
{
     //function logic is written here..
     float num1, num2;
    
     printf("Enter number1: ");
     scanf("%f",&num1);
    
     printf("Enter number2: ");
     scanf("%f",&num2);
    
     printf("Average of two numbers: %f.", (num1+num2)/2);    
}
//Output
Enter number1: 10
Enter number2: 20

Average of two numbers: 15.000000.

C program to show use of sqrt() Library Functions.

This program demonstrate use of two library function:
--> sqrt()
--> printf()
*/
#include<stdio.h>
#include<math.h>
void main()
{
     float ans;
     ans = sqrt(100);
    
     printf("The square root of 100 is %f.", ans);  
}
//Output
The square root of 100 is 10.000000.

Wednesday, April 27, 2016

C program to use Arrow keys.


#include<stdio.h>
int main()
{
int chr1, chr2;
printf("Press any arrow key...\n");
chr1 = getch();
if (chr1 == 0xE0) //to check scroll key interrupt
{
chr2 = getch();  //to read arrow key
switch(chr2)
{
case 72: printf("UP ARROW KEY PRESSED\n");
break;
case 80: printf("DOWN ARROW KEY PRESSED\n");
break;
case 75: printf("LEFT ARROW KEY PRESSED\n");
break;
case 77: printf("RIGHT ARROW KEY PRESSED\n");
break;
default: printf("OTHER KEY PRESSED: %d %d\n", chr1, chr2);
break;
};
}
 return 0;
}

//Output of program

Press any arrow key...
UP ARROW KEY PRESSED