Record Template
Record Template
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
Date:15/12/2023
A7: Using Functions in C
Aim:
To learn about how to use Function in “C”.
Question 1:
Create a function CheckOddEven(num) that checks if the num is odd or even; sets
a flag accordingly and return it. Use this function to find the sum of even and odd
numbers in a given input of N numbers
Code:
# include <stdio.h>
# include <string.h>
int even(int eve[],int sum)
{
int j,e=0;
for(j=0;j<sum;j++)
{
e=e+eve[j];
}
printf("sum of Even = %d \n",e);
}
int odd(int arr[],int size)
{
int i,o=0;
for(i=0;i<size;i++)
{
o=o+arr[i];
}
printf("sum of odd = %d \n",o);
}
void main()
{
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
int i,s=0,b[100],c[100],d=0,n,a[100];
printf("Enter the number of number going to enter: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter num : ");
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]%2==0)
{
c[s]=a[i];
s++;
}
else
{
b[d]=a[i];
d++;
}
}
even(c,s);
odd(b,d);
}
Test Cases: (Minimum 5 Test Cases)
Write a C function ReverseNum(num) that takes integer num and reverses its
digits. Let numbe passed by reference.
Example:
Input: 453275
Output: 572354
Code:
# include <stdio.h>
# include <string.h>
for(int j =s-1;j>=0;j--)
printf("%d",arr[j]);
int main()
int n,i,a[100];
scanf("%d",&n);
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
for (i=0; i<n; i++)
scanf("%d",&a[i]);
swap(a,n);
Code:
# include <stdio.h>
if(x<=1)
return 1;
return x*factorial(x-1);
int out(int y)
int z=y%10;
if(z!=0)
return z;
return out(y/10);
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
}
void main ()
int i,t,n;
scanf("%d",&n);
int f=factorial(n);
printf("%d",out(f));
Learning Outcomes:
You will be able to implement functions in C with the following features:
- input parameters (Call by value)
- input-output parameters (Call by reference)
- recursive functions