Académique Documents
Professionnel Documents
Culture Documents
*/
#include <stdio.h> // FUNCTION DECLARATION
int sum(int , int );
int main()
{
int num1, num2;
printf("\n Enter the first number: ");
scanf("%d", &num1);
printf("\n Enter the second number: ");
scanf("%d", &num2);
// FUNCTION CALL
printf("\n Total = %d", sum(num1, num2));
return 0;
}
// FUNCTION DEFINITION
int sum (int a, int b) // FUNCTION HEADER
{ // FUNCTION BODY
return( a + b);
}
/*. Write a program to find biggest of three integers using functions.*/
#include <stdio.h>
int greater(int , int , int );
int main()
{
int num1, num2, num3, large;
printf("\n Enter the first number: ");
scanf("%d", &num1);
printf("\n Enter the second number: ");
scanf("%d", &num2);
printf("\n Enter the third number: ");
scanf("%d", &num3);
large = greater(num1, num2, num3);
printf("\n Largest number = %d", large);
return 0;
}
int greater(int a, int b, int c)
{
if(a>b && a>c)
return a;
if(b>a && b>c)
return b;
else
return c;
}
/*. Write a program to calculate area of a circle using function.*/
#include <stdio.h>
float cal_area(float );
int main()
{
where, S = ( a + b + c ) / 2
*/
float func(int,int,int,int);
#include<stdio.h>
#include<math.h>
main()
{
int s1,s2,s3,s;
int area;
printf("enter 3 sides of triangle: \n\n");
scanf("%d%d%d",&s1,&s2,&s3);
s=s1+s2+s3/2;
printf("\narea = %d",func(s1,s2,s3,s));
}
float func(int i, int j, int k, int h)
{
return (sqrt(h*((h-i)*(h-j)*(h-k))));
}
//Write a function to calculate the factorial value
//of any integer entered through the keyboard.
#include<stdio.h>
#include<conio.h>
void func();
main()
{
int num;
printf("Please enter any number: ");
scanf("%d",&num);
func(num);
}
void func(int n)
{
int fact=1;
for(;n>=1;n--) {
fact=fact*n;
}
printf("\n\nFactorial value = %d \n",fact);
}
/*Write a function to find the binary equivalent of a given decimal integer
and display it.
*/
int binary();
#include<stdio.h>
main()
{
int num;
printf("\t\tDecimal Integer to Binary conversion\n\n\n");
printf("Enter the number: ");
scanf("%d",&num);
binary(num);
}
int binary(int n)
{
while(n!=0)
{
printf("%d",n%2);
n=n/2;
}
}
Examples on recursion
#include<stdio.h>
int factorial(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, factorial(n));
return 0;
}
int factorial(int n)
{
if(n!=1)
return n*factorial(n-1);
}
/* calculate power of a number using recursion*/
#include <stdio.h>
long power (int, int);
int main()
{
int pow, num;
long result;
printf("Enter a number: ");
scanf("%d", &num);
printf("Enter it's power: ");
scanf("%d", &pow);
power(num, pow);