EX NO:4 FUNCTIONS
14/11/24
AIM:
To solve the problems using functions in C language programming.
Question:1
Find the cube of any number using function
CubeNum()- Receives parameter and return cube value.
Source code:
#include<stdio.h>
int CubeNum(number)
{return number*number*number;}
void main()
{int n;
printf("enter the number to find cube of that=");
scanf("%d",&n);
int result=CubeNum(n);
printf("the cube of the number %d is %d",n,result);
Output:
Question:2
Find the sum of the series 1!/1+2!/2+3!/3+4!/4+5!/5 using the function.
FactNum() – Receives parameter and return factorial.
Source code:
#include<stdio.h>
int factNum(int n)
{int fact=1,i;
for (i=1;i<=n;i++)
{fact=i*fact;}
return fact;}
void main()
{int n,result,j,sum=0;
printf("enter the number to find series of factorial:");
scanf("%d",&n);
for(j=1;j<=n;j++)
{sum+=factNum(j)/j;}
printf("the sum of the factorial series of %d is %d",n,sum);
Output:
Question:3
Check whether a number is a prime number or not using the function.
PrimeNum() - Receives parameter and return 1(prime) or 0(not a prime)
Source code:
#include<stdio.h>
int PrimeNum(int n)
{if (n <= 1) {
return 0; }
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return 0; }}
return 1;}
void main()
{int n, result;
printf("Enter the number to find whether it is prime or not: ");
scanf("%d", &n);
result = PrimeNum(n);
if (result == 1) {
printf("The given number %d is a prime number.\n", n); }
else
{printf("The given number %d is not a prime number.\n", n);}
}
Output:
Question:4
Check Armstrong and Perfect numbers using the function
checkArmstrong()- Receives parameter and return 1 or 0
checkPerfect() - Receives parameter and return 1 or 0
Source code:
include <stdio.h>
#include <math.h>
int checkArmstrong(int n) {
int original = n;
int sum = 0;
int digits = 0;
while (n != 0) {
n /= 10;
digits++;}
n = original;
while (n != 0) {
int digit = n % 10;
sum += pow(digit, digits);
n /= 10;}
return (sum == original) ? 1 : 0;
int checkPerfect(int n) {
int sum = 0;
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) {
sum += i;}}
return (sum == n) ? 1 : 0;
}
void main(){
int n;
printf("Enter a number to check if it is Armstrong or Perfect: ");
scanf("%d", &n);
if (checkArmstrong(n)) {
printf("%d is an Armstrong number.\n", n);
} else {
printf("%d is not an Armstrong number.\n", n);
if (checkPerfect(n)) {
printf("%d is a Perfect number.\n", n);
} else {
printf("%d is not a Perfect number.\n", n);
Output:
Question:5
Print all perfect numbers in a given range using the function
checkPerfect()-Receives parameter and return 1 or 0
PerfectNumbers- receives starting and ending limit and invoke checkPerfect() to
check the perfect number and print the numbers (note: Not return any value)
Source code:
Output:
Question:6
Input three numbers from user and find maximum and minimum of the given
numbers using functions
MaxNum()- Receives parameters and return max value
MinNum()-Receives parameters and return min value
Source code:
Output:
Question:7
Find GCD and LCM of two numbers using function
Gcd()-Receives parameters and return Gcd value
Lcm()-Receives parameters and return Lcm value
Source code:
Output:
Question:8
Input radius of circle from user and find diameter, circumference and area of the
given circle using function
DiameterCircle()-Receives parameters and return diameter value
CircumCircle()-Receives parameters and return circumference value
AreaCircle()-Receives parameters and return area value
Source code:
Output:
Question:9
Find the factorial and Fibonacci series of the number using function
Fact()
Fibo()
Source code:
Output:
Question:10
Read the number from the user and find number of digits, sum of digits and
reverse of the numbers
NumDigi()
SumDigi()
ReverseDigi()
Source code:
Output: