# Examples on User Defined Functions

“If you want to shine like a Sun, first burn like a Sun”

Hello everyone!!!

Hope you all must be doing well. Today we will discuss some interesting examples on user defined functions. Soo let’s start 🙂

Q1) Write a function isEven, which accepts an integer as parameter and returns 1 if the number is even, and 0 otherwise. Use this function in main to accept n numbers and check if they are even or odd.

#include
int iseven(int);
int main()
{
int n, ans;
printf(“Enter no:”);
scanf(“%d”,&n);
ans=iseven(n);
}
int iseven(int n1)
{
if(n1%2==0)
return 1;
else
return 0;
}

Output:-
When user enters even number output is 👇
Enter no:56
When user enters odd number output is👇
Enter no:23

Q2) Write a function in ‘C’ to calculate sum of digits of an integer. Use this function in main.

#include
int sumofdigit(int);
int main ()
{
int n, ans;
printf (“enter no:”);
scanf (“%d”, &n);
ans=sumofdigit(n);
printf(“Sum of digit is %d”, ans);
}
int sumofdigit(int n1)
{
int sum=0,r;
while (n1>0)
{
r=n1%10;
sum =sum + r;
n1=n1/10;
}
return sum;
}

Output:-
enter no:123
Sum of digit is 6

Q3) Write a function in ‘C’ to reverse an integer. Use this in main.

#include
int reverse (int);
int main()
{
int n, ans;
printf (“enter no:”);
scanf (“%d”, &n);
ans=reverse (n);
printf(“Reverse of digit is %d”, ans);
}
int reverse (int n1)
{
int rev=0,r;
while(n1>0)
{
r=n1%10;
rev =rev*10+r;
n1=n1/10;
}
return rev;
}

Output:-
enter no:345
Reverse of digit is 543

Q4) Write a ‘C’ functions to check if a number is prime. Use this function to display all prime Numbers between 100 and 500.

#include
int main()
{
int i, flag;
for (i = 100; i <=500; i++)
{
if (flag == 1)
printf(“%d “, i);
}
}
{
int j, flag = 1;
for (j = 2; j <= n / 2; j++)
{
if (n % j == 0)
{
flag = 0;
break;
}
}
return flag;
}

Output:-
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499

Q5) Write a ‘C’ program to calculate the factorial of a number using function.

#include
int fact (int);
int main()
{
int n, ans;
printf (“enter no:”);
scanf (“%d”, &n);
ans = fact (n);
printf(“Factorial is %d”, ans);
}
int fact (int n1)
{
int f=1,i;
for(i=1;i<=n1; i++)
{
f=f*i;
}
return f;
}

Output:-
enter no:5
Factorial is 120

Q6) Write a ‘C’ function isPrime, which accepts an integer number as parameter and returns 1 if the number is prime and 0 otherwise. Use this function to check if a number isprime.

#include
int isprime (int);
int main()
{
int n, ans;
printf (“enter no:”);
scanf (“%d”, &n);
ans = isprime (n);
printf(“Ans is %d”, ans);
}
int isprime(int n1)
{
int i;
for(i=2;i<=n1/2;i++)
{
if(n1%i==0)
return 0;
break;
}
return 1;
}

Output:-
When user enters prime number output is👇
enter no:23
Ans is 1
When user enters non-prime number output👇
enter no:56
Ans is 0

Q7) Write a function, which accepts a character and integer n as parameter and displays the Next n characters.

#include
void display (int, char);
int main()
{
int n,ans,ch;
printf(“enter value of n and character:”);
scanf(“%d %c”,&n,&ch);
display(n, ch);
}
void display(int n1,char ch1)
{
int i;
for(i=0;i<n1;i++)
{
ch1++;
printf (“%c”,ch1);
}
}

Output:-
enter value of n and character:6 a
bcdefg

Soo that’s it for today.