0% found this document useful (0 votes)
38 views8 pages

FUNCTION

The document discusses 8 examples of using functions in C code. The examples cover: 1. Defining and calling a function to return the greater of two numbers. 2. Defining and calling a function to calculate the factorial of a number. 3. Defining and calling functions to count the digits in a number, check if a number is a palindrome, and check if a number is prime. 4. The functions demonstrate passing parameters, returning values, and performing calculations within functions.

Uploaded by

vinit sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views8 pages

FUNCTION

The document discusses 8 examples of using functions in C code. The examples cover: 1. Defining and calling a function to return the greater of two numbers. 2. Defining and calling a function to calculate the factorial of a number. 3. Defining and calling functions to count the digits in a number, check if a number is a palindrome, and check if a number is prime. 4. The functions demonstrate passing parameters, returning values, and performing calculations within functions.

Uploaded by

vinit sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

FUNCTION

(1)CODE:

#include<stdio.h>
int great(int a,int b);
int main()
{
int a,b,p;
printf("Enter two numbers :");
scanf("%d%d",&a,&b);
p=great(a,b);
printf("The greater number is :%d",p);
return 0;
}
int great(int a,int b)
{ int k;
if(a>b)
{
k=a;
}
else{
k=b;
}
return k;
}
OUTPUT:

Enter two numbers :6 8

The greater number is :8

(2)CODE:

#include<stdio.h>
int fact(int a);
int main()
{
int a,p;
printf("Enter the number:");
scanf("%d",&a);
p=fact(a);
printf("The factorial of the number is :%d",p);
}
int fact(int a)
{int b=1;
for(;a>=1;a--)
{
b*=a;
}
return b;
}
OUTPUT:

Enter the number:5

The factorial of the number is :120

(3)CODE:

#include<stdio.h>
int fact(int a);
int main()
{
int a,p;
printf("Enter the number:");
scanf("%d",&a);
p=fact(a);
printf("The number of digits :%d",p);
}
int fact(int a)
{int count;
for(;a%10!=0;a=a/10)
{
count++;
}
return count;
}
OUTPUT:

Enter the number:1234567893

The number of digits :10

(4)CODE:

#include<stdio.h>
int fact(int a);
int main()
{
int a,p;
printf("Enter the number:");
scanf("%d",&a);
p=fact(a);
if(a==p)
{
printf("It is a palliandram number");
}
else
{printf("It is not a palliandram number");}
return 0;
}
int fact(int a)
{int c=0,d;
for(;a%10!=0;a=a/10)
{
d=a%10;
c=(c*10)+d;
}
return c;
}
OUTPUT:]

Enter the number:121

It is a palliandram number

(5)CODE:

#include<stdio.h>
int fact(int a);
int main()
{
int a,p;
printf("Enter the number:");
scanf("%d",&a);
p=fact(a);
if(p>=1)
{
printf("It is a prime number");
}
else
{printf("It is not a prime number");}
return 0;
}
int fact(int a)
{int c=0,d=a-1;
for(;d>1;d--)
{if(a%d==0)
c++;
}
return c;
}
OUTPUT:

Enter the number:124

It is a prime number

(6)CODE:

#include<stdio.h>
int fact(int a,int b);
int main()
{
int a,b,p;
printf("Enter two number:");
scanf("%d%d",&a,&b);
p=fact(a,b);
printf("The HCF IS :%d",p);
return 0;
}
int fact(int a,int b)
{int c,i,min,d=1;
min=(a>b)?a:b;
for(i=1;i<=min;i++)
{if(a%i==0&&b%i==0)
c=(i>c)?i:c;
}
return c;
}
OUTPUT;

Enter two number:100 15

The HCF IS :5

(7)CODE:

#include<stdio.h>
int mul(int a,int b);
int main()
{
int a,b,p;
printf("Enter two number:");
scanf("%d%d",&a,&b);
p=mul(a,b);
printf("The multiplication is:%d",p);
return 0;
}
int mul(int a,int b)
{int c=0,i;
for(i=1;i<=b;i++)
{
c=c+a;
}
return c;
}
OUTPUT:

Enter two number:2 3

The multiplication is:6

(8)CODE:

#include<stdio.h>
int mul(int a,int b);
int main()
{
int a,b,p;
printf("Enter two number:");
scanf("%d%d",&a,&b);
mul(a,b);
return 0;
}
int mul(int a,int b)
{int c;
c=a;
a=b;
b=c;
printf("The two numbers are :%5d%5d",a,b);
return 0;
}
OUTPUT:

Enter two number:4 2

The two numbers are : 2 4

TRY THIS TOO

(1)CODE:
#include<stdio.h>
int show();
int main()
{
show();
}
int show()
{
int a,b,c;
printf("Enter two numbers :");
scanf("%d%d",&a,&b);
c=a+b;
printf("The sum is :%d",c);
}
OUTPUT:

Enter two numbers :2 3

The sum is :5

OUTPUT:

(2)CODE:
#include<stdio.h>
int show(int,int);
int main()
{
int a,b;
printf("Enter two numbers :");
scanf("%d%d",&a,&b);
show(a,b);
}
int show(int a,int b)
{
int c;
c=a+b;
printf("The sum is :%d",c);
}
OUTPUT:

Enter two numbers :2 3

The sum is :5

(3)CODE:
#include<stdio.h>
int show();
int main()
{
int p;
p=show();
printf("The sum is :%d",p);
}
int show()
{
int a,b;
int c;
printf("Enter two numbers :");
scanf("%d%d",&a,&b);
c=a+b;
return c;
}
OUTPUT:

Enter two numbers :2 3

The sum is :5

(4)CODE:
#include<stdio.h>
int show(int a,int b);
int main()
{
int a,b, p;
printf("Enter two numbers :");
scanf("%d%d",&a,&b);
p=show(a,b);
printf("The sum is :%d",p);
}
int show(int a,int b)
{
int c;
c=a+b;
return c;
}
OUTPUT:

Enter two numbers :2 3

The sum is :5

(5)CODE:
#include<stdio.h>
int show(int *a,int *b);
int main()
{
int a,b, p;
printf("Enter two numbers :");
scanf("%d%d",&a,&b);
p=show(&a,&b);
printf("The sum is :%d",p);
}
int show(int *a,int *b)
{
int c;
c=*a+*b;
return c;
}
OUTPUT:

Enter two numbers :2 3

The sum is :5

(6)CODE:

OUTPUT:

(7)CODE:

OUTPUT:

(8)CODE:

OUTPUT:

You might also like