Programs On Functions 1) //program To Demonstrate Functions With Arguments and Return Values
Programs On Functions 1) //program To Demonstrate Functions With Arguments and Return Values
R 1
Programs on Functions
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
#include<conio.h>
1
Sanjeetha.R 2
printf("sum is %d",z);
}
3) //program to demonstrate functions without arguments and No return value
#include<stdio.h>
#include<conio.h>
void add( );
void main( )
{
clrscr( );
add( );
getch( );
}
void add( )
{
int z,x,y;
printf("Enter two numbers\n");
scanf("%d%d",&x,&y);
z=x+y;
printf("sum is %d",z);
}
#include<stdio.h>
#include<conio.h>
int add( );
void main( )
{
int sum;
clrscr( );
sum=add( );
printf("sum is %d",sum);
getch( );
}
int add( )
{
int z,x,y;
printf("Enter two numbers\n");
scanf("%d%d",&x,&y);
z=x+y;
return(z);
2
Sanjeetha.R 3
#include<stdio.h>
#include<conio.h>
int biggest(int x,int y);
void main( )
{
int a,b,big;
clrscr( );
printf("Enter the values of a and b\n");
scanf("%d%d",&a,&b);
big=biggest(a,b);
printf("biggest number is %d",big);
getch( );
}
#include <stdio.h>
#include <conio.h>
3
Sanjeetha.R 4
void main( )
{
int a[10][10], b[10][10], sum[10][10];
int m, n, p, q;
int i, j, k;
clrscr( );
printf("\nEnter the Dimension of Matrix 1 ? : ");
scanf("%d%d",&m, &n);
printf("\nEnter the Dimension of Matrix 2 ? : ");
scanf("%d%d",&p, &q);
if((m==p)&&(n==q))
{
printf("\n\nEnter The Elements Of Matrix 1 ? : \n\n");
read(a, m, n);
printf("\nEnter The Elements Of Matrix 2 ? : \n\n");
read(b, p, q);
printf(" Elements of Matrix 1 is\n\n");
display(a,m,n);
printf(" Elements of Matrix 2 is\n\n");
display(b,p,q);
add(a, b,sum,m,n);
printf("\n\nThe Resultant Matrix Is : \n\n");
display(sum,m,n);
}
else
{
printf("\n\nMatrix addition is Not possible");
}
4
Sanjeetha.R 5
getch( );
} //end main
#include<stdio.h>
#include<conio.h>
void add( );
int a,b,sum,x,y,z;
void main( )
{
clrscr( );
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
add( );
printf("sum is %d",sum);
getch( );
}
#include<stdio.h>
#include<conio.h>
typedef struct complex_number
{
float real;
float imag;
} complex;
5
Sanjeetha.R 6
9)/* Develop functions: i) To read a matrix ii) To output a matrix iii) To compute
product of two matrices. */
#include <stdio.h>
#include <conio.h>
int m, n, p, q;
int i, j, k;
6
Sanjeetha.R 7
printf("\n");
}
}
void main( )
{
int a[10][10], b[10][10], prod[10][10];
clrscr( );
printf("\nEnter the Dimension of Matrix 1 ? : ");
scanf("%d%d",&m, &n);
printf("\nEnter the Dimension of Matrix 2 ? : ");
scanf("%d%d",&p, &q);
if(n==p)
{
printf("\n\nEnter The Elements Of Matrix 1 ? : \n\n");
read(a, m, n);
printf("\nEnter The Elements Of Matrix 2 ? : \n\n");
read(b, p, q);
printf(" Elements of Matrix 1 is\n\n");
display(a,m,n);
printf(" Elements of Matrix 2 is\n\n");
display(b,p,q);
add(a, b,prod,m,n);
printf("\n\nThe Resultant Matrix Is : \n\n");
display(prod,m,q);
}
else
{
printf("\n\nMatrix multiplication is Not possible");
}
getch( );
} //end main
10) /* Read a matrix and find the following using functions: i) Sum of all the
elements of each row ii) Sum of the elements of each column iii) Sum of all the
elements of the matrix */
#include <stdio.h>
#include <math.h>
int m,n,i,j,rsum,r,c;
int a[10][10];
void main( )
{
clrscr( );
7
Sanjeetha.R 8
int sumall( )
{
8
Sanjeetha.R 9
int i,j,asum=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
asum+=a[i][j];
return(asum);
}
int palin() ;
void main( )
{
int flag;
clrscr( );
flag= palin();
if(flag==1)
printf("Number is a palindrome\n");
else
printf("Number is not a palindrome\n");
getch();
}
int palin()
{
int m,rev=0,digit,num;
printf("Enter the number ");
scanf("%d",&num);
m=num;
while(num!=0)
{
digit=num%10;
num=num/10;
rev=rev*10+digit;
}
printf("Reversed number is %d\n",rev);
if(m==rev)
return(1);
else
return(0);
}
9
Sanjeetha.R 10
12) Write a C program to find GCD and LCM of 2 numbers using functions with
arguments, without return type.
#include<stdio.h>
#include<conio.h>
void gcd_lcm(int m, int n);
void main( )
{
int x,y;
clrscr( );
printf("Enter two numbers\n");
scanf("%d%d",&x,&y);
gcd_lcm(x,y) ;
getch() ;
}
#include<stdio.h>
#include<conio.h>
void triangle();
void main( )
{
triangle() ;
10
Sanjeetha.R 11
void triangle()
{
int a,b,c ;
printf(“Enter the sides of the triangle\n”);
scanf(“%d%d%d”,&a,&b,&c);
if(a==b&&b==c)
printf(“The triangle is equilateral\n”);
else
if((a==b)||(b==c)||(c==a))
printf(“The triangle is isoceles\n”);
else
printf(“The triangle is scalene\n”);
getch();
}
14) Write a C program to find factorial of a given number using functions with
arguments, with return type.
#include<stdio.h>
#include<conio.h>
11
Sanjeetha.R 12
#include<stdio.h>
#include<conio.h>
void bubble_sort(int num,int x[20]);
void display(int num, int x[20]);
void main( )
{
int a[20],i,t,n,j;
clrscr( );
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements of the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("elements of the array before sorting are\n");
display(n,a);
bubble_sort(n,a);
printf("\nelements of the array after sorting are\n");
display(n,a);
getch( );
}
12
Sanjeetha.R 13
16) /*program to find whether given number is a palindrome or not using functions
with arguments and with return type */
#include<stdio.h>
#include<conio.h>
int palin(int m);
void main()
{
int n,rev;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
rev=palin(n);
if(rev==n)
printf("the number is a palindrome\n");
else
printf("number is not a palindrome\n");
getch();
}
int palin(int m)
{
int digit,reverse=0;
while(m!=0)
{
digit=m%10;
reverse=reverse*10+digit;
m=m/10;
}
return(reverse);
}
13
Sanjeetha.R 14
printf("%d\n",x[i]);
printf("Enter the key element to be searched\n");
scanf("%d",&keyele); // read element to be searched
bin_srch(x,keyele,num);
getch();
}
14