Example Of Function 
#include
<stdio.h>
#include<conio.h>
void
display();          //function
declaration
void
display()             //function
definition
{
    printf("Welcome to
LearningPoint92\n");
    }
void main()
{
     clrscr();
     display (); //function call
     getch();
}
Output:
Welcome to LearningPoint92 
Addition of two numbers without using
void
#include
<stdio.h>
#include<conio.h>
int
addition(int num1, int num2); // function declaration
int
addition(int num1, int num2) //function definition
{
     int  sum;
  sum = num1+num2;
   return  sum;
}
void main()
{
     int  var1, var2;
     clrscr();
     printf("Enter 1stnumber\n");
     scanf("%d",&var1);
     printf("Enter 2ndnumber\n");
     scanf("%d",&var2);
  int result = addition(var1, var2);
     printf ("sum is: %d", result);
  getch();
}
Output
Enter 1stnumber                                                                                                                
23                                                                                                                             
Enter 2ndnumber                                                                                                                
45                                                                                                                             
sum is: 68 
OR
Addition of two number using void
#include
<stdio.h>
#include<conio.h>
void
addition(int num1, int num2); // function declaration
void
addition(int num1, int num2) //function definition
{
     int sum;
     sum = num1+num2;
  printf ("sum is: %d", sum);
}
void main()
{
     int var1, var2;
     clrscr();
     printf("Enter 1stnumber\n");
     scanf("%d",&var1);
     printf("Enter 2ndnumber\n");
     scanf("%d",&var2);
     addition(var1, var2); //function call
        getch();
}
Output:
Enter 1stnumber                                                                                                                
23                                                                                                                             
Enter 2ndnumber                                                                                                                
45                                                                                                                             
sum is: 68 
Calculate Area of circle
#include
<stdio.h>
#include<conio.h>
double
FindArea(double radius); //function declaration
double
FindArea(double radius) //function definition
{
    return (3.14* radius * radius); 
}
void main() 
{
    double radius,  area;
    clrscr();
    printf("Enter radius of circle:
");
    scanf("%lf", &radius);
     area
= FindArea(radius);    //function
call       
     printf("Area of the circle is %lf",
area);
     getch();
}
Output:
Enter radius of circle: 4.5                                                                                                    
Area of the circle is 63.585000  
Calculate cube of any number using
function
#include
<stdio.h>
#include<conio.h>
int cube(int
num);
int cube(int
num)
{
    return (num * num * num);
}
void main()
{
    int num;
    int c;
    clrscr();
   printf("Enter any number: ");
    scanf("%d", &num);
     c =
cube(num);
 printf("Cube is  %d", c); 
     getch();
}
Output
Enter any number: 7                                                                                                            
Cube is  343  
To check whether a number is even or
odd 
#include
<stdio.h>
#include<conio.h>
void  checkEven(int num); //function declaration
void  checkEven(int num) //function definition
{
    if(num%2==0)
     {
        printf("The number is
even.");
    }
    else
    {
        printf("The number is odd.");
    }
    }
void main()
{
    int num;
    clrscr();
    printf("Enter any number: ");
    scanf("%d", &num);
      checkEven(num); //function call
       getch();
}
Output:
Enter any number: 12                                                                                                           
The number is even.   
Swapping of two number using call by
value
#include
<stdio.h>
#include<conio.h>
void
swap(int a, int b);   //function
declaration     
  
void
swap(int a, int b)   //function definition
{ 
    int tmp;
    tmp = a;
    a = b;
    b = tmp;
    printf("\n values after swap var1 = %d
and var2 = %d", a, b);
}
void main()
{
     int
var1,var2;
     clrscr();
     printf("Enter 1stnumber\n");
     scanf("%d",&var1);
     printf("Enter 2ndnumber\n");
     scanf("%d",&var2);
    printf(" values before swap var1 = %d and
var2 = %d", var1, var2);
    swap(var1,var2); //function call
    getch();
}
Output:
Enter 1stnumber                                                                                                                
23                                                                                                                             
Enter 2ndnumber                                                                                                                
45                                                                                                                             
 values before swap var1 = 23 and var2 = 45                                                                                    
 values after swap var1 = 45 and var2 = 23  
Swapping of two number using call by
reference
#include
<stdio.h>
#include<conio.h>
void
swap(int *a, int *b);   //function
declaration  
     
void
swap(int *a, int *b)   //function
definition
{ 
    int tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
    printf("\n values after swap var1 = %d
and var2 = %d", *a, *b);
}
void main()
{
 int var1,var2;
   clrscr();
     printf("Enter 1stnumber\n");
     scanf("%d",&var1);
     printf("Enter 2ndnumber\n");
     scanf("%d",&var2);
   printf(" values before swap var1 = %d and
var2 = %d", var1, var2);
    swap(&var1,&var2); //function call
    getch();
}
Output:
Enter 1stnumber                                                                                                                
23                                                                                                                             
Enter 2ndnumber                                                                                                                
45                                                                                                                             
 values before swap var1 = 23 and var2 = 45                                                                                    
 values after swap var1 = 45 and var2 = 23  
To check whether a number is prime and
Armstrong using functions
#include
<stdio.h>
#include<conio.h>
int
FindPrime(int num);
int
FindArmstrong(int num);
void main()
{
    int num;
   printf("Enter any number\n ");
    scanf("%d", &num);
    clrscr();
    // Call functions FindPrime() functions
    if(FindPrime(num))
    {
        printf("%d is Prime
number.\n", num);
    }
    else
    {
        printf("%d is  not Prime number.\n", num);
    }
    // Call FindArmstrong() function
    if(FindArmstrong(num))
    {
        printf("%d is Armstrong
number.\n", num);
    }
    else
    {
        printf("%d is not Armstrong
number.\n", num);
    }
   getch();
}
int
FindPrime(int num) 
{
    int i;
    for(i=2; i<=num/2; i++)  
    {  
   //Returns 1 if the number is Prime number
otherwise 0.
    if(num%i == 0)  
        {
            return 0;
        } 
    } 
      return 
1; 
}
int  FindArmstrong(int num) 
{
    int sum = 0, t, remainder;
 t = num;
  while( t != 0 )  {
      remainder = t%10;
      sum = sum +
remainder*remainder*remainder;
      t = t/10;
   }
     return (num == sum);
}
Output:
Enter any number                                                                                                               
 153                                                                                                                           
153 is not Prime number.                                                                                                      
153 is Armstrong number
To find maximum and minimum between
two numbers using functions
#include
<stdio.h>
#include<conio.h>
int max(int
num1, int num2);
int min(int
num1, int num2);
void main() 
{
    int num1, num2, maximum, minimum;
  clrscr();
    printf("Enter any two numbers:
");
    scanf("%d%d", &num1,
&num2);
    maximum = max(num1, num2);  // Call maximum function
    minimum = min(num1, num2);  // Call minimum function
    printf("\nMaximum = %d\n",
maximum);
    printf("Minimum = %d", minimum);
    getch();
}
int max(int
num1, int num2)
{
    return (num1 > num2 ) ? num1 : num2;
}
int min(int
num1, int num2) 
{
    return (num1 > num2 ) ? num2 : num1;
}
Output:
Enter any two numbers: 23                                                                                                      
67                                                                                                                             
Maximum = 67                                                                                                                   
Minimum = 23
Factorial of a Number Using Recursion
#include
<stdio.h>
#include<conio.h>
long
Factorial(int n);
void main()
{
    int n;
    long result;
    clrscr();
    printf("Enter a number ");
    scanf("%d", &n);
    result=Factorial(n);
    printf("Factorial is
%ld",result);
    getch();
}
long
Factorial(int n)
{
    if (n >= 1)
        return n*Factorial(n-1);
    else
        return 1;
}
Output:
 Enter a number 7                                                                                                               
Factorial is 5040
 
 
No comments:
Post a Comment