News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

Function for absolute without using Abs()

Started by thiruvasagamani, Sep 22, 2008, 11:29 AM

Previous topic - Next topic

thiruvasagamani

Write a function for absolute without using abs()

#include <stdio.h>
#include <conio.h>
int absolute(int n)
{
if ( n > 0)
return (-x); // return +ve value when 'x' is -ve
else
return (x); // return +ve value when 'x' is +ve
}
main()
{
int a, absolute(int);
printf(" enter +ve or -ve value \n");
scanf("%d", &a);
printf(" absolute value is %d", absolute(a)); // display value returned by function
getch();
}

o/p:
If you enter 'a' value -25 output is 25
If you enter 'a' value 75 output is 75

NOTE:
main() contains
1. read inputs by calling scanf()
2. call the function and send the inputs
3. display results by calling printf()
NOTE:
Actual parameter and formal parameter may have same name but they are different variables, their addresses are different

Find largest of 3 numbers by using function

#inlcude <stdio.h>
#include <conio.h>
int max(in a , in b , int c)
{
int lar;
lar= a; // assume that 'a' is largest number
if( b > lar)
lar = b; // if 'b is greater than large number 'b' is largest
if( c > lar)
lar = c; // if 'c' is greater than large number 'c' is largest
return( lar ); // largest at a , b , c is returned
}
main()
{
int x , y, z , max(int , int, int);
printf(" enter any 3 values");
scanf("%d %d %d", &x,&y,&z);
printf(" largest number is %d\n", max( x , y , z)); // displays largest of x , y , z
gatch();
}

o/p:
If you enter x= 5,y= 6, z= 7 output is 7

NOTE :
1. max( 10, 20 , 30) this is largest of 3 numbers
2. max(max( a , b , c), max( d , e , f), max( g , h , i)) this is largest of 9 numbers
Thiruvasakamani Karnan