MODULE 4- PROGRAMS
FUNCTIONS, STRUCTURE AND UNION
1. SUM OF TWO NUMBERS USING FUNCTION
/*C program to find the sum of two numbers using function*/
#include<stdio.h>
int sum(int a, int b); //function declaration
void main()
{
int x,y,result;
printf("Enter 2 numbers\n");
scanf("%d%d",&x,&y);
result=sum(x,y); // function call. Here x,y are the actual parameters
printf("sum=%d",result);
}
//function definition
int sum(int a, int b) //function definition. Here a,b are the formal parameters
{
int c;
c=a+b;
return c;
}
Output
Enter 2 numbers
5 4
sum=9
—--------------------------------------------------------------------------------------------
2. SUM AND AVERAGE OF n ELEMENTS IN AN ARRAY USING FUNCTION
/*C program to find the sum and average of n elements in an array using function*/
#include<stdio.h>
void sumaverage(int , int []); //function declaration
void main()
{
int a[10],size,i;
printf("Enter size of the array : ");
scanf("%d",&size);
for(i=0;i<size;i++)
{
scanf("%d", &a[i]);
}
sumaverage(size,a); //function call
}
void sumaverage(int n, int arr[]) //function definition
{
int i,sum=0;
float average;
for(i=0;i<n;i++)
sum=sum+arr[i];
average=(float)sum/n;
printf("Sum= %d\n", sum);
printf("Average= %f", average);
}
Output
Enter size of the array : 4
5 2 3 7
Sum= 17
Average= 4.250000
—---------------------------------------------------------------------------------------------------------
3. SORT n NUMBERS USING FUNCTION
/*C program to sort an array using function*/
#include<stdio.h>
void sort(int , int []); //function declaration
void main()
{
int arr[10],size,i;
printf("Enter size of the array : ");
scanf("%d",&size);
for(i=0;i<size;i++)
{
scanf("%d", &arr[i]);
}
sort(size,arr); //function call
printf("Sorted array is\n");
for(i=0;i<size;i++)
{
printf("%d\t", arr[i]);
}
}
void sort(int n, int A[]) //function definition
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(A[j]> A[j+1])
{
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}
}
}
}
Output
Enter size of the array : 5
6 2 5 9 1
Sorted array is
1 2 5 6 9
—------------------------------------------------------------------------------------------------------
4) FACTORIAL OF A NUMBER USING RECURSION
/* C program to find factorial of a number using recursion*/
#include<stdio.h>
void main()
{ int fact(int);
int n,result;
printf("Enter the number\n");
scanf("%d", &n);
result=fact(n);
printf("Factorial=%d",result);
}
int fact(int no)
{
if(no==1 || no==0)
return 1;
else
return no*fact(no-1);
}
Output
Enter the number
5
Factorial=120
—---------------------------------------------------------------------------------------------------------
5) SUM OF FIRST n NATURAL NUMBERS USING RECURSION
/* C program to find factorial of a number using recursion*/
#include<stdio.h>
void main()
{ int sum(int);
int n,result;
printf("Enter the limit\n");
scanf("%d", &n);
result=sum(n);
printf("Sum of first %d natural numbers = %d",n,result);
}
int sum(int no)
{
if(no==1)
return 1;
else
return no+sum(no-1);
}
Output
Enter the limit
4
Sum of first 4 natural numbers = 10
6) FIBONACCI SERIES USING RECURSION
/* C program to print fibonacci series using recursion*/
#include<stdio.h>
void main()
{ int FIB(int);
int n,i,m=0;
printf("Enter the no. of terms in the series\n");
scanf("%d", &n);
printf("The fibonacci series is\n");
for(i=0;i<n;i++)
{
printf("%d\t",FIB(m));
m++;
}
}
int FIB(int no)
{
if(no==0 || no==1)
return no;
else
return ( FIB(no-1)+FIB(no-2) );
}
Output
Enter the no. of terms in the series
8
The fibonacci series is
0 1 1 2 3 5 8 13
—----------------------------------------------------------------------------------------------------------------
7) Write a C program to create a structure with fields rollno, name and mark.
Read the above details for a single student and display the details.
/* C program to read and print the details of a student using structure*/
#include<stdio.h>
struct student
{
int rollno;
char name[20];
float mark;
};
void main()
{
struct student s;
printf("Enter the details of student\n");
printf("Enter roll no : ");
scanf("%d", &s.rollno);
printf("Enter name : ");
scanf("%s", s.name);
printf("Enter mark : ");
scanf("%f", &s.mark);
printf("STUDENT DETAILS\n");
printf("***************\n");
printf("ROLL NO : %d\n",s.rollno);
printf("NAME : %s\n",s.name);
printf("MARK : %f\n",s.mark);
}
Output
Enter the details of student
Enter roll no : 1
Enter name : aaa
Enter mark : 45
STUDENT DETAILS
***************
ROLL NO : 1
NAME : aaa
MARK : 45.000000
—------------------------------------------------------------------------------------------------------
8) Write a C program to read two points in the euclidean space and add those
two points using structure
/* C program to find the sum of two points using structure*/
struct point
{
int x;
int y;
}p1,p2,p3;
#include<stdio.h>
void main()
{
printf("Enter x and y coordinates of first point\n");
scanf("%d%d", &p1.x, &p1.y);
printf("Enter x and y coordinates of second point\n");
scanf("%d%d", &p2.x, &p2.y);
p3.x =p1.x+p2.x;
p3.y= p1.y+p2.y;
printf("The sum is :(%d,%d)", p3.x,p3.y);
}
Output
Enter x and y coordinates of first point
2 3
Enter x and y coordinates of second point
5 4
The sum is :(7,7)
—-----------------------------------------------------------------------------------------------------
9) Write a C program to read two points in the euclidean space and find
Euclidean distance between them.
/* C program to find the euclidean distance of two points using structure*/
#include<stdio.h>
#include<math.h>
struct point
{
int x;
int y;
}p1,p2;
void main()
{
float sum, distance;
printf("Enter x and y coordinates of first point\n");
scanf("%d%d", &p1.x, &p1.y);
printf("Enter x and y coordinates of second point\n");
scanf("%d%d", &p2.x, &p2.y);
sum= (p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)* (p2.y-p1.y) ;
distance= sqrt(sum);
printf("The Euclidean distance = %f", distance);
}
Output
Enter x and y coordinates of first point
2 2
Enter x and y coordinates of second point
2 4
The Euclidean distance = 2.000000
—-------------------------------------------------------------------------------------------------------------
10) Using structure, read and print data of ‘n’ employees ( Name, Employee id,
salary)
#include<stdio.h>
struct Employees
{
char emp_name[20];
int emp_id;
float emp_salary;
}e[50];
void main()
{
int n,i;
printf("Enter the no.of employees\n");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Enter details of employee %d \n",i+1);
printf("Enter employeeid, name and salary\n");
scanf("%d%s%f",&e[i].emp_id,e[i].emp_name,&e[i].emp_salary);
printf("------------------------------------\n");
}
printf("***EMPLOYEE DETAILS***\n");
printf("-----------------------\n");
printf("EMP_ID\t EMP_NAME \t EMP_SALARY\n");
for(i=0;i<n;i++)
{
printf("%d \t %s \t\t %f \n", e[i].emp_id,e[i].emp_name,e[i].emp_salary);
}
}
Output
Enter the no.of employees
3
Enter details of employee 1
Enter employeeid, name and salary
100 aaa 50000
------------------------------------
Enter details of employee 2
Enter employeeid, name and salary
101 bbb 70000
------------------------------------
Enter details of employee 3
Enter employeeid, name and salary
102 ccc 10000
------------------------------------
***EMPLOYEE DETAILS***
-----------------------
EMP_ID EMP_NAME EMP_SALARY
100 aaa 50000.000000
101 bbb 70000.000000
102 ccc 10000.000000
—------------------------------------------------------------------------------------------------------------
11) Declare a union containing 5 string variables (Name, House name, city name,
state and pin code) each with a length of C_SIZE (user defined constant). Then
read and display the address of a person using a variable of the union.
#include<stdio.h>
#define C_SIZE 30
union Address
{
char name[C_SIZE],house_name[C_SIZE];
char city_name[C_SIZE], state[C_SIZE],pin[C_SIZE];
}p1;
void main()
{
printf("Enter the Address of a person\n");
printf("Enter NAME : ");
scanf("%s",p1.name);
printf("NAME = %s \n---------\n", p1.name);
printf("Enter HOUSE NAME : ");
scanf("%s",p1.house_name);
printf("HOUSE NAME = %s \n---------\n", p1.house_name);
printf("Enter CITY NAME : ");
scanf("%s",p1.city_name);
printf("CITY NAME = %s \n---------\n", p1.city_name);
printf("Enter STATE : ");
scanf("%s",p1.state);
printf("STATE = %s \n---------\n", p1.state);
printf("Enter PIN : ");
scanf("%s",p1.pin);
printf("PIN = %s \n",p1.pin);
}
Output
Enter the Address of a person
Enter NAME : Anu
NAME = Anu
---------
Enter HOUSE NAME : abc
HOUSE NAME = abc
---------
Enter CITY NAME : kannur
CITY NAME = kannur
---------
Enter STATE : kerala
STATE = kerala
---------
Enter PIN : 676532
PIN = 676532
—----------------------------------------------------------------------------------------------------------
12) Find the factorial of a given natural number ‘n’ using recursive and
non-recursive functions.
#include<stdio.h>
void main()
{ int fact(int);
int n,result1,result2;
printf("Enter the number\n");
scanf("%d", &n);
result1=fact(n);
result2=fact_rec(n);
printf("Factorial(without recursion)=%d\n",result1);
printf("Factorial(using recursion)=%d",result2);
int fact(int no) //non-recursive function
{
int f=1,i;
for(i=1;i<=no;i++)
{
f=f*i;
}
return f;
}
int fact_rec(int no) // recursive function
{
if(no==1 || no==0)
return 1;
else
return no*fact_rec(no-1);
}
Output
Enter the number
5
Factorial(without recursion)=120
Factorial(using recursion)=120
—-------------------------------------------------------------------------------------------------------
13) Read a string , store it in an array and obtain its reverse by using a user
defined function.
// program to reverse a string using function
#include <stdio.h>
void stringreverse(char[]);
void main()
{
char str[100], chr;
printf("Enter a string: ");
scanf("%s",str);
stringreverse(str);
}
void stringreverse(char s[])
{
int length,i;
for (length = 0; s[length] != '\0'; length++);
printf("The reverse of the string is : ");
for(i=length-1;i>=0;i--)
{
printf("%c",s[i]);
}
}
Output
Enter a string: qwerty
The reverse of the string is : ytrewq
—--------------------------------------------------------------------------------------------------------
14) Write a menu driven program for performing matrix addition, multiplication
and finding the transpose. Use functions to (i) read a matrix, (ii) find the sum of
two matrices, (iii) find the product of two matrices, (iv) find the transpose of a
matrix and (v) display a matrix.
//menu driven program using functions
#include <stdio.h>
#include <stdlib.h>
void readmatrix(int a[][100],int m,int n)
{
int i,j;
printf("enter the elements row by row\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void displaymatrix(int a[][100],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%5d",a[i][j]);
printf("\n");
}
}
void addmatrix(int a[][100],int b[][100],int m,int n)
{
int i,j,c[100][100];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
printf("Sum of matrix...\n");
displaymatrix(c,m,n);
}
void transpose(int a[][100],int m,int n)
{
int i,j,c[100][100];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[j][i]=a[i][j];
displaymatrix(c,n,m);
}
void multmatrix(int a[][100],int b[][100],int m1,int n1,int n2)
{
int c[100][100],i,j,k;
// Multiply the two
for (i = 0; i < m1; i++) {
for (j = 0; j < n2; j++) {
c[i][j] = 0;
for (k = 0; k < n1; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
printf("Product of matrix...\n");
displaymatrix(c,m1,n2);
}
int main()
{ int a[100][100],b[100][100],m1,n1,m2,n2,op;
system("clear");
printf("Enter the size of the matrix A row,column\n");
scanf("%d%d",&m1,&n1);
printf("Enter Matrix A\n");
readmatrix(a,m1,n1);
printf("Enter the size of the matrix B row column\n");
scanf("%d%d",&m2,&n2);
printf("Enter Matrix B\n");
readmatrix(b,m2,n2);
system("clear");
printf("Matrix A..\n");
displaymatrix(a,m1,n1);
printf("Matrix B..\n");
displaymatrix(b,m2,n2);
while(1)
{
printf("\n************************************\n");
printf("1.add 2.multiply 3.transpose 4.exit \n");
printf("Enter the option.....:");
scanf("%d",&op);
switch(op)
{
case 1: if(m1==m2 && n1==n2)
addmatrix(a,b,m1,n1);
else
printf("Incompatable matrix...cannot add..\n");
break;
case 2: if(n1==m2)
multmatrix(a,b,m1,n1,n2);
else
printf("Incompatable matrix...cannot mutliply..\n");
break;
case 3: printf("Transpose of A..\n");
transpose(a,m1,n1);
printf("Transpose of B..\n");
transpose(b,m2,n2);
break;
case 4: exit(0);
}
}
}
Output
Enter the size of the matrix A row,column
2 2
Enter Matrix A
enter the elements row by row
1 2 3 4
Enter the size of the matrix B row column
2 2
Enter Matrix B
enter the elements row by row
1 2 3 4
Matrix A..
1 2
3 4
Matrix B..
1 2
3 4
1.add 2.multiply 3.transpose 4.exit
Enter the option.....:1
Sum of matrix...
2 4
6 8
************************************
1.add 2.multiply 3.transpose 4.exit
Enter the option.....2
Product of matrix...
7 10
15 22
************************************
1.add 2.multiply 3.transpose 4.exit
Enter the option.....:3
Transpose of A..
1 3
2 4
Transpose of B..
1 3
2 4
************************************
1.add 2.multiply 3.transpose 4.exit
Enter the option.....:4