0% found this document useful (0 votes)
12 views18 pages

Record Template

The document discusses programming exercises using functions in C. It includes 4 questions: 1) A function to find the sum of even and odd numbers in an input array. 2) A function to reverse the digits of a number. 3) A recursive function to find the product of floating point numbers input by the user. 4) A function to find the rightmost non-zero digit of the factorial of a given number between 1 to 100. For each question, sample code and test cases are provided.
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)
12 views18 pages

Record Template

The document discusses programming exercises using functions in C. It includes 4 questions: 1) A function to find the sum of even and odd numbers in an input array. 2) A function to reverse the digits of a number. 3) A recursive function to find the product of floating point numbers input by the user. 4) A function to find the rightmost non-zero digit of the factorial of a given number between 1 to 100. For each question, sample code and test cases are provided.
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/ 18

Shiv Nadar University Chennai

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)

1.Enter the number of number going to enter: 4


Enter num : 1
Enter num : 2
Enter num : 3
Enter num : 4
sum of Even = 6
sum of odd = 4
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
2.Enter the number of number going to enter: 6
Enter num : 7
Enter num : 5
Enter num : 9
Enter num : 8
Enter num : 3
Enter num : 9
sum of Even = 8
sum of odd = 33

3.Enter the number of number going to enter: 5


Enter num : 3
Enter num : 8
Enter num : 0
Enter num : 5
Enter num : 4
sum of Even = 12
sum of odd = 8

4.Enter the number of number going to enter: 4


Enter num : 6
Enter num : 8
Enter num : 9
Enter num : 4
sum of Even = 18
sum of odd = 9

5.Enter the number of number going to enter: 7


Enter num : 4
Enter num : 9
Enter num : 7
Enter num : 45
Enter num : 87
Enter num : 12
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
Enter num : 76
sum of Even = 92
sum of odd = 148

Output: (Terminal Screen Shot)


Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
Question 2:

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>

int swap(int arr[],int s)

for(int j =s-1;j>=0;j--)

printf("%d",arr[j]);

int main()

int n,i,a[100];

printf("enter the no. of number input: ");

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++)

printf("enter the number: ");

scanf("%d",&a[i]);

swap(a,n);

Test Cases: (Minimum 5 Test Cases)

1.enter the no. of number input: 4


enter the number: 11
enter the number: 3
enter the number: 5
enter the number: 67
675311

2.enter the no. of number input: 6


enter the number: 3
enter the number: 5
enter the number: 2
enter the number: 6
enter the number: 2
enter the number: 6
626253

3.enter the no. of number input: 5


enter the number: 2
enter the number: 6
enter the number: 1
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
enter the number: 7
enter the number: 2
27162

4.enter the no. of number input: 2


enter the number: 1
enter the number: 2
21

5.enter the no. of number input: 4


enter the number: 1
enter the number: 4
enter the number: 6
enter the number: 2
1462

Output: (Terminal Screen Shot)


Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
Question.3:
Find the product of n floating point numbers. The numbers should be read from
the keyboard.You should not use any looping construct. [Hint: use recursion and
decide a suitable sentinel for ter-mination of recursion.
Code:
# include <stdio.h>
float multi(int x)
{
float a;
if(x==0)
return 1;
printf("enter the number: ");
scanf("%f",&a);
return a*multi(x-1);
}
int main()
{
float n;
printf("Enter the number: ");
scanf("%f",&n);
printf("The product of the number is: %f",multi(n));
}
Test Cases: (Minimum 5 Test Cases)

1.Enter the no. of number: 5

enter the number: 1.34

enter the number: 12.5

enter the number: 12.67

enter the number: 3.56

enter the number: 2.1


Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
The product of the number is: 1586.575195

2.Enter the no. of number: 4

enter the number: 12.0

enter the number: 12.1

enter the number: 12.3

enter the number: 12.4

The product of the number is: 22145.904297

3.Enter the no. of number: 3.4

enter the number: 3.4

enter the number: 1.65

enter the number: 1.6

The product of the number is: 8.976001

4.Enter the no. of number: 4

enter the number: 13.5

enter the number: 13.5

enter the number: 13.5


Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
enter the number: 13.5

5.Enter the no. of number: 4

enter the number: 10

enter the number: 10

enter the number: 10

enter the number: 10

The product of the number is: 10000.00000

Output: (Terminal Screen Shot)


Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
Question.4:. Factorials
The factorial of an integer n, written n! is the product of all the integers from 1 to
n inclusive. Thefactorial quickly becomes very large; 13! is too large to store as an
integer on most computers, and 35! is too large for a floating-point variable.
Your task is to find the rightmost non-zero digit of n! (1<= n <= 100).
For example, 5! = 1 * 2 * 3 * 4 * 5 = 120, so the rightmost non-zero digit of 5! is 2.
Also, 7! =1 * 2 * 3 * 4 * 5 * 6 * 7 = 5040, so the rightmost non-zero digit of 7! is 4.
Test cases
Input Output
3 6
10 8

Code:
# include <stdio.h>

int factorial( int x)

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;

printf("enter the number: ");

scanf("%d",&n);

int f=factorial(n);

printf("%d",out(f));

Test Cases: (Minimum 5 Test Cases)

enter the number: 5


2
enter the number: 7
4
enter the number: 3
6
enter the number: 13
4
enter the number: 12
6
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
Output: (Terminal Screen Shot)

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy