Vous êtes sur la page 1sur 6

/*. Write a program to add two integers using functions.

*/
#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()
{

float area, radius;


printf("\n Enter the radius of the circle: ");
scanf("%f", &radius);
printf("\n Area of the circle with radius %.2f = %.2f", radius, cal_area(radius));
return 0;
}
float cal_area(float radius)
{
return (3.14 * radius * radius);
}
/*. Write a program, using functions, to fi nd whether a number is even or odd.*/
#include <stdio.h>
int evenodd(int);
int main()
{
int num, flag;
printf("\n Enter the number: ");
scanf("%d", &num);
flag = evenodd(num);
if (flag == 1)
printf("\n %d is EVEN", num);
else
printf("\n %d is ODD", num);
return 0;
}
int evenodd(int a)
{
return(a%2==0 ?1:0);
}
/*. Write a program to convert time to minutes.*/
#include <stdio.h>
#include <conio.h>
int myfunc(int , int );
main()
{
int hrs, minutes;
printf("\n Enter hours and minutes: ");
scanf("%d %d", &hrs, &minutes);
printf("\n Total minutes = %d", myfunc(hrs, minutes));
getch();
return 0;
}
int myfunc(int hrs, int minutes)
{
return(hrs*60 + minutes);
}
/*If the lengths of the sides of a triangle are denoted by a, b, and c, then area of
triangle is given by
rootover ( S * (S-a) * (S-b) * (S-c))

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);

printf("%d^%d is %ld", num, pow, power(num, pow));


return 0;
}
long power (int num, int pow)
{
if (pow!=0)
{
return (num * power(num, pow - 1));
}
return 1;
}
/* sum of digits using recursion*/
#include <stdio.h>
int sum (int a);
int main()
{
int num, result;
printf("Enter the number: ");
scanf("%d", &num);
result = sum(num);
printf("Sum of digits in %d is %d\n", num, result);
return 0;
}
int sum (int num)
{
if (num != 0)
{
return (num % 10 + sum (num / 10));
}
else
{
return 0;
}
}
#include <stdio.h>
int sum (int );
int main()
{
int num, result;
printf("Enter the number: ");
scanf("%d", &num);
sum(num);
printf("Sum of digits in %d is %d\n", num, sum(num));
return 0;
}

int sum (int num)


{
if (num != 0)
{
return (num % 10 + sum (num / 10));
}
else
{
return 0;
}
}

Vous aimerez peut-être aussi