Function Examples (Pass and Return)

Current Set : Set 7
Set 1     Set 2     Set 3     Set 4     Set 5     Set 6     Set 7     Set 8    Set 9    Set 10

Question:3
Write a function C language to take input for a numbers calculate and print its factorial?
Sol:

#include <stdio.h>

//function definition
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return(f);
}
int main()
{
int a,b;
printf("Enter any no ");
scanf("%d",&a);
b=fact(a);
printf("factorial = %d",b);
return 0;
}

Question:4
Write a function C language to take input for 3 numbers check and print the largest number?
Sol:

#include <stdio.h>

//function definition
int largest(int p,int q,int r)
{
int m;
if(p>q && p>r)
m=p;
else
if(q>r)
m=q;
else
m=r;
return(m);
}
/* 2nd method */
/*
int largest(int p,int q,int r)
{
if(p>q && p>r)
return(p);
else
if(q>r)
return(q);
else
return(r);
}
*/
int main()
{
int a,b,c,ma;
printf("Enter 3 nos ");
scanf("%d %d %d",&a,&b,&c);
ma=largest(a,b,c);
printf("largest no = %d",ma);
return 0;
}

Current Set : Set 7
Set 1     Set 2     Set 3     Set 4     Set 5     Set 6     Set 7     Set 8    Set 9    Set 10

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects