Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
113 views

Programs On Functions 1) //program To Demonstrate Functions With Arguments and Return Values

The document contains examples of C programs that demonstrate different types of functions: 1. Functions with arguments and return values that adds two numbers. 2. Functions with arguments but no return value that also adds two numbers and prints the result. 3. Functions without arguments but with a return value that gets two numbers from the user and returns their sum. 4. Functions without arguments or return value that gets two numbers from the user and prints their sum. 5. A function that returns the largest of two numbers passed as arguments.

Uploaded by

sanjeetha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

Programs On Functions 1) //program To Demonstrate Functions With Arguments and Return Values

The document contains examples of C programs that demonstrate different types of functions: 1. Functions with arguments and return values that adds two numbers. 2. Functions with arguments but no return value that also adds two numbers and prints the result. 3. Functions without arguments but with a return value that gets two numbers from the user and returns their sum. 4. Functions without arguments or return value that gets two numbers from the user and prints their sum. 5. A function that returns the largest of two numbers passed as arguments.

Uploaded by

sanjeetha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

Sanjeetha.

R 1

Programs on Functions

1) //program to demonstrate functions with arguments and return values

#include<stdio.h>
#include<conio.h>

int add(int x, int y);


void main( )
{
int a,b,sum;
clrscr( );
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
sum=add(a,b);
printf("sum is %d",sum);
getch( );
}

int add( int x, int y)


{
int z;
z=x+y;
return(z);
}

2) //program to demonstrate functions with arguments and No return value

#include<stdio.h>
#include<conio.h>

void add(int x, int y);


void main( )
{
int a,b;
clrscr( );
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
add(a,b);
getch( );
}

void add( int x, int y)


{
int z;
z=x+y;

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);
}

4) //program to demonstrate functions without arguments and with return value

#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

5) /* program to find biggest of two numbers using functions */

#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( );
}

int biggest(int x,int y)


{
if(x>y)
return (x);
else
return (y);
}

6) /* Develop functions: i) To read a matrix ii) To output a matrix iii) To compute


addition of two matrices. */

#include <stdio.h>
#include <conio.h>

void read(int mat[10][10], int r, int c)


{
int i, j;
for (i = 0;i < r; i++)
for (j = 0; j < c; j++)
scanf("%d",&mat[i][j]);
}

void add(int a[10][10], int b[10][10], int sum[10][10], int m,int n)


{
int i,j;
for (i = 0;i < m; i++)

3
Sanjeetha.R 4

for (j = 0;j < n; j++)


{
sum[i][j] = 0;
sum[i][j] = a[i][j] + b[i][j];
}
}

void display(int mat[10][10], int r, int c)


{
int i, j;
for (i = 0; i < r; i++) {
for (j = 0;j < c; j++)
printf("%d\t ",mat[i][j]);
printf("\n");
}
}

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

7) /*program to illustrate use of global variables*/

#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( );
}

void add( int x, int y)


{
z=x+y;
}

8) /* program to add two complex numbers using structures */

#include<stdio.h>
#include<conio.h>
typedef struct complex_number
{
float real;
float imag;
} complex;

complex sum(complex m, complex n);


void main( )
{
complex x,y,z;
clrscr( );
printf("Enter x & y\n ");
scanf("%f%f%f%f",&x.real,&x.imag,&y.real,&y.imag);
z=sum(x,y);

5
Sanjeetha.R 6

printf("sum of x & y is %f +i%f",z.real,z.imag);


getch();
}

complex sum(complex m, complex n)


{
complex o;
o.real=m.real+n.real;
o.imag=m.imag+n.imag;
return(o);
}

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;

void read(int mat[10][10], int r, int c)


{
int i, j;
for (i = 0;i < r; i++)
for (j = 0; j < c; j++)
scanf("%d",&mat[i][j]);
}

void add(int a[10][10], int b[10][10], int mul[10][10], int m,int q)


{
for (i = 0;i < m; i++)
for (j = 0;j < q; j++)
{
mul[i][j] = 0;
for (k = 0;k < p; k++)
mul[i][j] = mul[i][j] + a[i][k] * b[k][j];
}
}

void display(int mat[10][10], int r, int c)


{
int i, j;
for (i = 0; i < r; i++) {
for (j = 0;j < c; j++)
printf("%d\t ",mat[i][j]);

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

printf("Enter the order of matrix A : ");


scanf("%d%d",&m,&n);
printf("Enter the elements ( %d ) for matrix A",m*n);
for (i=0; i<m;i++)
for(j=0;j<n;j++)
scanf ("%d",&a[i][j]);

printf("The elements of matrix A are\n");


for (i=0; i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
printf("\n");
}

printf("Enter the row number \n");


scanf("%d",&r);
printf("\nSum of the specified row is as follows:\n");
printf("\t%d\t",rowsum(r-1));

printf("\nEnter the column number \n");


scanf("%d",&c);
printf("\nSum of the specified column is as follows:\n");
printf("\t%d\t",colsum(c-1));
printf("\nSum of all element is %d",sumall( ));
getch( );
}

int rowsum(int row)


{
int j,sum=0;
for(j=0;j<n;j++)
sum+=a[row][j];
return(sum);
}

int colsum(int col)


{
int i,csum=0;
for(i=0;i<m;i++)
csum+=a[i][col];
return(csum);
}

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);
}

11) Write a C program to find whether a number is a palindrome using functions


without arguments, with return type.
#include<stdio.h>
#include<conio.h>

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() ;
}

void gcd_lcm(int m, int n)


{
int p,q,gcd,lcm,r ;
p=m;
q=n;
while(n!=0)
{
r=m%n;
m=n;
n=r;
}
gcd=m;
lcm=p*q/gcd;
printf("GCD of %d and %d is %d\n",p,q,gcd);
printf("LCM of %d and %d is %d\n",p,q,lcm);
}

13) Write a C program to find whether a given triangle is isosceles scalene or


equilateral using functions without arguments, without return type.

#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>

int fact(int num);


void main( )
{
int n,factorial;
printf(“Enter the number\n”);
scanf(“%d”,&n);
factorial=fact(n);
printf(“Factorial of %d is %d”,n,factorial);
getch();
}

int fact(int num)


{
int i,f=1;
for(i=1;i<=num;i++)
f=f*i;
return(f);
}

15) /*program to perform bubble sort using functions */

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( );
}

void bubble_sort(int num,int x[20])


{
int i,j,t;
for(i=0;i<num-1;i++) //Outer for loop for each pass
for(j=1;j<num-i;j++) //Sorting in each pass
{
if(x[j]<x[j-1]) //move largest element to the end of array
{
t=x[j];
x[j]=x[j-1];
x[j-1]=t;
}
}
}

void display(int num, int x[20])


{
int i;
for(i=0;i<num;i++)
printf("%d\t",x[i]);
}

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);
}

17) /* program to perform binary search using functions */


#include<stdio.h>
#include<conio.h>
void bin_srch(int a[20],int key,int n);
void main( )
{
int x[20],i,keyele,num;
clrscr( );
printf("Enter the size of the array\n");
scanf("%d",&num);
printf("Enter the elements of the array in ascending order\n");
for(i=0;i<num;i++)
scanf("%d",&x[i]);
printf("elements of the array are\n");
for(i=0;i<num;i++)

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();
}

void bin_srch(int a[20],int key,int n)


{
int low,high,mid,flag=0;
low=0; //assign values for low and high
high=n-1;
while(low<=high) //if low>high element is not found
{
mid=(low+high)/2; //find mid of array
if(a[mid]==key) //compare a[mid] with key
{
flag=1;
break;
}
else if(a[mid]>key) //Search in first half of array
high=mid-1;
else
low=mid+1; //Search in second half of array
}
if(flag==1) // if flag is 1 then element is found
printf("Element found at position %d",mid+1);
else
printf("Element not found");
}

14

You might also like