Sei sulla pagina 1di 16

1

Functions


/*W.A.P on functions sample1*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Output: \n");
printf("Main Function\n");
one();
two();
three();
getch();
}
one()
{
printf("one\n");
}
two()
{
printf("two\n");
}
three()
{
printf("three");
}

Output:
Main Function
one
two
three


/*W.A.P on functions sample2*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Output: \n");
printf("Main\n");
one();
two();
getch();
}
one()
{
printf("one\n");
}
two()
{
printf("two\n");
}
three()
{
printf("three");
}

Output:
Main
one
two

/*W.A.P on functions sample3*/
#include<stdio.h>
#include<conio.h>
void line();
void main()
{
clrscr();
printf("Output: \n");
printf("Countries\n");
line();
printf("\nIndia\t\tUSA\n");
printf("Capital\n");
line();
printf("\nNew Delhi\t Washington\n");
getch();
}
void line()
{
int i;
for(i=0;i<=10;i++)
printf("--");
}

Output:
Countries
----------------------
India USA
Capital
----------------------
New Delhi Washington


/*W.A.P to add two numbers 1st type*/
#include<stdio.h>
#include<conio.h>
void addition();
void main()
{
clrscr();
printf("Output: \n");
addition();
getch();
}
void addition()
{
int a,b;
printf("Enter two numbers: ");
scanf("%d %d",&a,&b);
printf("Sum = %d",a+b);
}

Output:
Enter two numbers: 5 6
Sum = 11

/*W.A.P to add two numbers 2nd type*/
#include<stdio.h>

/*W.A.P to add two numbers 3rd type*/
#include<stdio.h>

2

#include<conio.h>
void addition(int a,int b);
void main()
{
int a,b;
clrscr();
printf("Output: \n");
printf("Enter two numbers: ");
scanf("%d %d",&a,&b);
addition(a,b);
getch();
}
void addition(int x,int y)
{
printf("Sum = %d",x+y);
}

Output:
Enter two numbers: 10 20
Sum = 30
#include<conio.h>
int addition();
void main()
{
int sum;
clrscr();
printf("Output: \n");
sum=addition();
printf("Sum = %d",sum);
getch();
}
int addition()
{
int a,b,c;
printf("Enter two numbers: ");
scanf("%d %d",&a,&b);
c=a+b;
return(c);
}

Output:
Enter two numbers: 45 98
Sum = 143


/*W.A.P to find average of 3 numbers(1st type)*/
#include<stdio.h>
#include<conio.h>
void average();
void main()
{
clrscr();
printf("Output: \n");
average();
getch();
}
void average()
{
int a,b,c;float avg;
printf("Enter the values of a,b,c: ");
scanf("%d %d %d",&a,&b,&c);
avg=(a+b+c)/3.0;
printf("Average = %f",avg);
}

Output:
Enter the values of a,b,c: 10 20 30
Average = 20.000000


/*W.A.P to find the average of 3 numbers(2nd type)*/
#include<stdio.h>
#include<conio.h>
void average(int a,int b,int c,float avg);
void main()
{
int a,b,c;float avg;
clrscr();
printf("Output: \n");
printf("Enter the values of a,b,c: ");
scanf("%d %d %d",&a,&b,&c);
average(a,b,c,avg);
getch();
}
void average(int a,int b,int c,float avg)
{
avg=(a+b+c)/3.0;
printf("Average = %f",avg);
}
Output:
Enter the values of a,b,c: 10 20 30
Average = 20.000000


/*W.A.P to find the average of 3 numbers(3rd type)*/
#include<stdio.h>
#include<conio.h>
float average();
void main()
{
float avg;
clrscr();
printf("Output: \n");
avg=average();
printf("Average = %f",avg);
getch();
}
float average()
{
int a,b,c;
float avg;
printf("Enter the values of a,b,c: ");
scanf("%d %d %d",&a,&b,&c);
avg=(a+b+c)/3.0;
return avg;

/*W.A.P to find the average of 3 numbers(4th type)*/
#include<stdio.h>
#include<conio.h>
float average(int a,int b,int c);
void main()
{
int a,b,c;
clrscr();
printf("Output: \n");
printf("Enter the values of a,b,c: ");
scanf("%d %d %d",&a,&b,&c);
printf("Average = %f",average(a,b,c));
getch();
}
float average(int a,int b,int c)
{
return((a+b+c)/3.0);
}

Output:
Enter the values of a,b,c: 10 20 30

3

}

Output:
Enter the values of a,b,c: 10 20 30
Average = 20.000000

Average = 20.000000

/*W.A.P to find the average of 3 numbers(5th type)*/
#include<stdio.h>
#include<conio.h>
float average(int a,int b,int c);
void main()
{
int a,b,c;float avg;
clrscr();
printf("Output: \n");
printf("Enter the values of a,b,c: ");
scanf("%d %d %d",&a,&b,&c);
avg=average(a,b,c);
printf("Average = %f",avg);
getch();
}
float average(int a,int b,int c)
{
float avg;
avg=(a+b+c)/3.0;
return avg;
}

Output:
Enter the values of a,b,c: 10 20 30
Average = 20.000000


/*W.A.P to check given no> is even or not*/
#include<stdio.h>
#include<conio.h>
void evenodd();
void main()
{
clrscr();
printf("Output: \n");
evenodd();
getch();
}
void evenodd()
{
int n;
printf("Enter a no.: ");
scanf("%d",&n);
if(n%2==0)
printf("%d is a even number",n);
else
printf("%d is a odd number",n);
}

Output:
Enter a no.: 7
7 is a odd number

/*W.A.P to check given no. is even or not 2nd type*/
#include<stdio.h>
#include<conio.h>
void evenodd(int n);
void main()
{
clrscr();
printf("Output: \n");
int n;
printf("Enter a number: ");
scanf("%d",&n);
evenodd(n);
getch();
}
void evenodd(int n)
{
if(n%2==0)
printf("%d is a even number",n);
else
printf("%d is a odd number",n);
}

Output:
Enter a number: 98
98 is a even number

/*W.A.P to check given no. is even or not 3rd type*/
#include<stdio.h>
#include<conio.h>
int evenodd();
void main()
{
int x,n;
clrscr();
printf("Output: \n");
x=evenodd();
if(x==1)
printf("Even number");
else
printf("Odd number");
getch();
}
int evenodd()
{
int n;
printf("Enter a number: ");
scanf("%d",n);
if(n%2==0)
return 1;
else
return 0;
}

Output:
Enter a number: 98
98 is a even number


/*W.A.P to find factorial of a number 1st type*/
#include<stdio.h>
#include<conio.h>
void factorial();

/*W.A.P to find factorial of a number 2nd type*/
#include<stdio.h>
#include<conio.h>
void factorial(int n,int fact,int m);

4

void main()
{
clrscr();
printf("Output: \n");
factorial();
getch();
}
void factorial()
{
int n,fact=1,m;
printf("Enter a number: ");
scanf("%d",&n);
for(m=n;n>0;fact=fact*n,n--)
{
}
printf("Factorial of %d = %d",m,fact);
}

Output:
Enter a number: 6
Factorial of 6 = 720

void main()
{
int n,fact=1,m;
clrscr();
printf("Output: \n");
printf("Enter a number: ");
scanf("%d",&n);
factorial(n,fact,m);
getch();
}
void factorial(int n,int fact,int m)
{
for(m=n;n>0;fact=fact*n,n--)
{
}
printf("Factorial of %d = %d",m,fact);
}

Output:
Enter a number: 5
Factorial of 5 = 120

/*W.A.P to covert to lowercase letter into uppercase(1st type)*/
#include<stdio.h>
#include<conio.h>
void conversion();
void main()
{
clrscr();
printf("Output: \n");
conversion();
getch();
}
void conversion()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
ch=ch-32;
printf("ch = %c",ch);
}

Output:
Enter a character: a
ch = A


/*W.A.P to covert to lowercase letter into uppercase(2nd type)*/
#include<stdio.h>
#include<conio.h>
void conversion(char ch);
void main()
{
char ch;
clrscr();
printf("Output: \n");
printf("Enter a character: ");
scanf("%c",&ch);
conversion(ch);
getch();
}
void conversion(char ch)
{
ch=ch-32;
printf("ch = %c",ch);
}

Output:
Enter a character: a
ch = A

/*W.A.P to covert to lowercase letter into uppercase(3rd type)*/
#include<stdio.h>
#include<conio.h>
char conversion();
void main()
{
char ch;
clrscr();
printf("Output: \n");
ch=conversion();
printf("ch = %c",ch);
getch();
}
char conversion()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
ch=ch-32;
return ch;
}

Output:

/*W.A.P to covert to lowercase letter into uppercase(4th type)*/
#include<stdio.h>
#include<conio.h>
char conversion(char ch);
void main()
{
char ch;
clrscr();
printf("Output: \n");
printf("Enter a character: ");
scanf("%c",ch);
ch=conversion(ch);
printf("ch = %c",ch);
getch();
}
char conversion(char ch)
{
ch=ch-32;
return ch;
}

Output:
Enter a character: a

5

Enter a character: a
ch = A

ch = o


/*W.A.P to display various parameters*/
#include<stdio.h>
#include<conio.h>
void display(int,float,char);
void main()
{
clrscr();
printf("Output: \n");
display(10,3.4,'+');
getch();
}
void display(int a,float b,char c)
{
printf("%d %f %c",a,b,c);
}

Output:
10 3.400000 +

/*W.A.P to explain local declaration*/
#include<stdio.h>
#include<conio.h>
void hello();
void main()
{
int x=10;
clrscr();
printf("Output: \n");
printf("x in main = %d\n",x);
hello();
getch();
}
void hello()
{
int y=20;
printf("y in hello = %d",y);
}

Output:
x in main = 10
y in hello = 20


/*W.A.P to explain global declaration*/
#include<stdio.h>
#include<conio.h>
int x;
void one();
void two();
void main()
{
x=10;
clrscr();
printf("Output: \n");
printf("x in main = %d\n",x);
one();two();
getch();
}
void one()
{
x=20;
printf("x in one = %d\n",x);
}
void two()
{
x=30;
printf("x in two = %d",x);
}

Output:
x in main = 10
x in one = 20
x in two = 30

/*W.A.P to explain global declaration2*/
#include<stdio.h>
#include<conio.h>
int x;
void one();
void two();
void main()
{
x=10;
clrscr();
printf("Output: \n");
printf("x in main = %d\n",x);
one();
two();
printf("x in main = %d\n",x);
getch();
}
void one()
{
int x=20;
printf("x in one = %d\n",x);
x++;
}
void two()
{
int x=30;
printf("x in two = %d\n",x);
x++;
}

Output:
x in main = 10
x in one = 20
x in two = 30
x in main = 10


/*W.A.P on call by value with functions*/
#include<stdio.h>
#include<conio.h>
void exchange(int,int);
void main(void)

/*W.A.P on call by reference*/
#include<stdio.h>
#include<conio.h>
void exchange(int *,int *);
void main(void)

6

{
int a=10,b=20;
clrscr();
printf("Output: \n");
printf("Before a = %d b = %d\n",a,b);
exchange(a,b);
printf("After calling function a = %d b = %d\n",a,b);
getch();
}
void exchange(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
}

Output:
Before calling function a = 10 b = 20
After calling function a = 10 b = 20

{
int a=10,b=20;
clrscr();
printf("Output: \n");
printf("Before calling function a = %d b = %d\n",a,b);
exchange(&a,&b);
printf("After calling function a = %d b = %d\n",a,b);
getch();
}
void exchange(int *p,int *q)
{
int temp;
temp=*p;
*p=*q;
*q=temp;
}

Output:
Before calling function a = 10 b = 20
After calling function a = 20 b = 10


/*W.A.P on default arguments*/
#include<stdio.h>
#include<conio.h>
void display(int=10, int=50, int=100);
void display(int a, int b, int c)
{
printf("%d %d %d\n",a,b,c);
}
void main()
{
clrscr();
printf("Output: \n");
display(10,20,30);
display(10,20);
display(10);
display();
getch();
}

Output:
10 20 30
10 20 100
10 50 100
10 50 100

/*W.A.P on return*/
#include<stdio.h>
#include<conio.h>
int function();
void main()
{
clrscr();
printf("Output: \n");
printf("%d",function());
getch();
}
int function()
{
int n;
printf("Enter a number: ");
scanf("%d",&n);
if(n==0) return 0;
if(n==1) return 10;
if(n==2) return 10*10;
}

Output:
Enter a number: 1
10

/*W.A.P to find factorial of a given no.(1st type)*/
/*Non recursive approach*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,fact;
clrscr();
printf("Output: \n");
printf("Enter a number: ");
scanf("%d",&n);
for(fact=1;n>=1;n--)
fact=fact*n;
printf("Factorial = %d",fact);
getch();
}

Output:
Enter a number: 5
Factorial = 120

/*W.A.P to find factorial of a given no.(2nd type)*/
/*Recursive approach*/
#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
{
int n,fact;
clrscr();
printf("Output: \n");
printf("Enter a number: ");
scanf("%d",&n);
fact=factorial(n);
printf("Factorial of %d = %d",n,fact);
getch();
}
int factorial(int n)
{
int f;
if(n==1)
return 1;
else
f=n*factorial(n-1);
return f;

7

}

Output:
Enter a number: 7
Factorial of 7 = 5040


/*W.A.P to find sum of digits of a given no. using non
recursion*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,m,a,sum=0;
clrscr();
printf("Output: \n");
printf("Enter a number: ");
scanf("%d",&n);
m=n;
while(n>0)
{
a=n%10;
sum=sum+a;
n=n/10;
}
printf("Sum of digits of %d = %d",m,sum);
getch();
}

Output:
Enter a number: 32
Sum of digits of 32 = 5



/*W.A.P to find sum of digits of a given no. using recursion*/
#include<stdio.h>
#include<conio.h>
int sumofdigits(int);
void main()
{
int n,sum;
clrscr();
printf("Enter a number: ");
scanf("%d",&n);
sum=sumofdigits(n);
printf("Sum of digits of %d = %d",n,sum);
getch();
}
int sumofdigits(int n)
{
int sum;
if(n==0)
return 0;
sum=n%10+sumofdigits(n/10);
return sum;
}

Output:
Enter a number: 32
Sum of digits of 32 = 5

/*W.A.P a program to print fibonacci no.'s upto given no. using
recursion*/
#include<stdio.h>
#include<conio.h>
void fibonacci(int, int, int);
void main()
{
int n;
clrscr();
printf("Output: \n");
printf("Enter a number: ");
scanf("%d",&n);
fibonacci(0,1,n);
getch();
}
void fibonacci(int a, int b, int n)
{
int c;
c=a+b;
if(c>n)
return;
else
{
printf("%d ",c);
a=b;b=c;
fibonacci(a,b,n);
}
}

Output:
Enter a number: 5
1 2 3 5





8


/*W.A.P on automatic storage classes*/
#include<stdio.h>
#include<conio.h>
void main()
{
auto int i=1;
{
auto int j=2;
clrscr();
printf("Output: \n");
printf("%d\n",j);
printf("%d\n",i);
}
///printf("%d\n",j);
printf("%d",i);
getch();
}

Output:
2
1
1

/*W.A.P on register storage classes*/
#include<stdio.h>
#include<conio.h>
void main()
{
register int a=1;
clrscr();
printf("Output: \n");
{
register int a=2;
printf("%d\n",a);
}
printf("%d",a);
getch();
}

Output:
2
1

/*W.A.P to program on static variable*/
#include<stdio.h>
#include<conio.h>
void display();
void main()
{
int i;
clrscr();
printf("Output: \n");
for(i=1;i<=10;i++)
display();
getch();
}
void display()
{
static int count=1;
printf("%d ",count);
count++;
}

Output:
1 2 3 4 5 6 7 8 9 10


/*W.A.P on Extern storage classes*/
#include<stdio.h>
#include<conio.h>
extern int x;
void one();
void main()
{
clrscr();
printf("Output: \n");
printf("%d\n",x);
x=100;
one();
getch();
}
void one()
{
printf("%d",x);
}

Output:
10
100

Multiple Compilation
#include<stdio.h>
#include<conio.h>
#include"one.h"
#include"two.h"
#include"three.h"
void main()
{
clrscr();
printf("Output: \n");
printf("Hello\n");
one();
two();
three();
getch();
}

#include<stdio.h>
#include<conio.h>
void one()
{
printf("One\n");
}

Output:
Hello
One
Two
Three
#include<stdio.h>
#include<conio.h>
void two()
{
printf("Two\n");
}
#include<stdio.h>
#include<conio.h>
void three()
{
printf("Three\n");
}

9

Functions with Arrays

/*W.A.P to read & print array elements*/
#include<stdio.h>
#include<conio.h>
void read(int a[], int n);
void display(int [], int);
void main()
{
int a[10],n;
clrscr();
printf("Output: \n");
printf("Enter no. of elements: ");
scanf("%d",&n);
read(a,n);
display(a,n);
getch();
}
void read(int a[10], int n)
{
int i;
printf("Enter array elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display(int a[10], int n)
{
int i;
printf("Array element are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}

Output:
Enter no. of elements: 5
Enter array elements1 2 3 4 5
Array element are
1 2 3 4 5

/*W.A.P to copy one array elements into another array by using
functions*/
#include<stdio.h>
#include<conio.h>
void read(int a[], int n);
void display(int [], int);
void copyarray(int [], int [], int);
void main()
{
int a[10],b[10],n;
clrscr();
printf("Output: \n");
printf("Enter no. of elements: ");
scanf("%d",&n);
read(a,n);
copyarray(a,b,n);
printf("\nFirst elements are \n");
display(a,n);
printf("\nSecond array elements are \n");
display(a,n);
getch();
}
void read(int a[10], int n)
{
int i;
printf("Enter array elements ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display(int a[10], int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
void copyarray(int a[], int b[], int n)
{
int i;
for(i=0;i<n;i++)
b[i]=a[i];
}

Output:
Enter no. of elements: 5
Enter array elements 1 2 3 4 5

First elements are
1 2 3 4 5
Second array elements are
1 2 3 4 5


/*W.A.P to insert an element into the array*/
#include<stdio.h>
void read(int []);
void display(int []);
void insert(int [],int,int);
int n;
main()
{
int a[10],pos,element;
printf("Output: \n");
printf("Enter no. of elements: ");
scanf("%d",&n);
read(a);
printf("\nBefore inserting the elements are");
display(a);
printf("\nEnter element to insert: ");

/*W.A.P to copy two array elements into third array by using
functions*/
#include<stdio.h>
#include<conio.h>
void read(int a[], int n);
void display(int [], int);
void copy(int [], int, int [], int, int []);
void main()
{
int a[10],b[10],c[20],m,n,p;
clrscr();
printf("Output: \n");
printf("Enter no. of elements in A array: ");
scanf("%d",&m);
printf("Enter no. of elements in B array: ");
scanf("%d",&n);

10

scanf("%d",&element);
printf("\nEnter position to insert: ");
scanf("%d",&pos);
insert(a,pos,element);
printf("\nAfter inserting the elements are");
display(a);
}
void read(int a[10])
{
int i;
printf("Enter array elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display(int a[10])
{
int i;
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
void insert(int a[10],int pos, int e)
{
int i;
if(pos<1||pos>n)
{
printf("Invalid postion");
return;
}
n=n+1;
for(i=n-1;i>=pos-1;i--)
{
a[i]=a[i-1];
}
a[pos-1]=e;
}

Output:
Enter no. of elements: 5
Enter array elements1 2 3 4 5

Before inserting the elements are1 2 3 4 5
Enter element to insert: 6

Enter position to insert: 3

After inserting the elements are1 2 6 3 4 5
p=m+n;
printf("\nEnter A elements \n");
read(a,m);
printf("\nEnter B elements \n");
read(b,n);
copy(a,m,b,n,c);
printf("\nA array elements are \n");
display(a,m);
printf("\nB array elements are \n");
display(b,n);
printf("\nC array elements are \n");
display(c,p);
getch();
}
void read(int a[10], int n)
{
int i;
printf("Enter array elements ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display(int a[10], int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
void copy(int a[], int m, int b[], int n, int c[])
{
int i,j=0;
for(i=0;i<m;i++)
{
c[i]=a[i];
j++;
}
for(i=0;i<n;i++)
{
c[j]=b[j];
j++;
}
}

Output:
Enter no. of elements in A array: 5
Enter no. of elements in B array: 5

Enter A elements
Enter array elements 1 2 3 4 5

Enter B elements
Enter array elements 6 7 8 9 0

A array elements are
1 2 3 4 5
B array elements are
6 7 8 9 0
C array elements are
1 2 3 4 5 6 7 8 9 0

Functions with DDArrays

/*W.A.P to read & print matrix elements using functions*/
#include<stdio.h>
#include<conio.h>
void read(int [][5], int, int);
void display(int [][5], int, int);
void main()
{
int a[5][5],m,n;
clrscr();

/*W.A.P to find sum of diagonal elements in the matrix*/
#include<stdio.h>
#include<conio.h>
void read(int [][5], int, int);
void display(int [][5], int, int);
void diagonal(int [][5], int, int);
void main()
{
int a[5][5],m,n;

11

printf("Enter size of matrix: ");
scanf("%d %d",&m,&n);
read(a,m,n);
display(a,m,n);
getch();
}
void read(int a[][5], int m, int n)
{
int i,j;
printf("Enter matrix elements: \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void display(int a[][5], int m, int n)
{
int i,j;
printf("The matrix elements are\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}

Output:
Enter size of matrix: 2 2
Enter matrix elements:
1 2 3 4
The matrix elements are
1 2
3 4
clrscr();
printf("Enter size of matrix: ");
scanf("%d %d",&m,&n);
read(a,m,n);
display(a,m,n);
diagonal(a,m,n);
getch();
}
void read(int a[][5], int m, int n)
{
int i,j;
printf("Enter matrix elements: \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void display(int a[][5], int m, int n)
{
int i,j;
printf("The matrix elements are\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
void diagonal(int a[][5], int m, int n)
{
int i,j,sum=0;
if(m==n)
{
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(i==j)
sum=sum+a[i][j];
printf("sum = %d",sum);
}
else
printf("Not square matrix");
}

Output:
Enter size of matrix: 2 2
Enter matrix elements:
1 2 3 4
The matrix elements are
1 2
3 4
sum = 5

/*W.A.P to make the given matrix as unit matrix*/
#include<stdio.h>
#include<conio.h>
void read(int [][5], int, int);
void display(int [][5], int, int);
void unitmatrix(int [][5], int, int);
void main()
{
int a[5][5],m,n;
clrscr();
printf("Enter size of matrix: ");
scanf("%d %d",&m,&n);
read(a,m,n);
display(a,m,n);
unitmatrix(a,m,n);
printf("After unit matrix ");
display(a,m,n);
getch();
}
void read(int a[][5], int m, int n)

/*W.A.P to interchange the given two row elements in a
matrix*/
#include<stdio.h>
#include<conio.h>
void read(int [][5], int, int);
void display(int [][5], int, int);
void interchange(int [][5], int, int);
void main()
{
int a[5][5],m,n;
clrscr();
printf("Enter size of matrix: ");
scanf("%d %d",&m,&n);
read(a,m,n);
display(a,m,n);
interchange(a,m,n);
printf("After interchange ");
display(a,m,n);
getch();
}

12

{
int i,j;
printf("Enter matrix elements: \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void display(int a[][5], int m, int n)
{
int i,j;
printf("the matrix elements are\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
void unitmatrix(int a[][5], int m, int n)
{
int i,j;
if(m==n)
{
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(i==j)
a[i][j]=1;
else
a[i][j]=0;
}
else
printf("Not square matrix");
}

Output:
Enter size of matrix: 2 2
Enter matrix elements:
1 2 3 4
the matrix elements are
1 2
3 4
After unit matrix the matrix elements are
1 0
0 1
void read(int a[][5], int m, int n)
{
int i,j;
printf("Enter matrix elements: \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void display(int a[][5], int m, int n)
{
int i,j;
printf("The matrix elements are\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
void interchange(int a[][5], int m, int n)
{
int i,j,r1,r2,temp;
printf("Enter rows to be interchanged");
scanf("%d %d",&r1,&r2);
if(r1<1||r1>m||r2<1||r2>m)
{
printf("Invalid matirx");
return;
}
for(j=0;j<n;j++)
{
temp=a[r1-1][j];
a[r1-1][j]=a[r2-1][j];
a[r2-1][j]=temp;
}
}

Output:
Enter size of matrix: 3 3
Enter matrix elements:
1 2 3 4 5 6 7 8 9
The matrix elements are
1 2 3
4 5 6
7 8 9
Enter rows to be interchanged1 3
After interchange The matrix elements are
7 8 9
4 5 6
1 2 3

/*W.A.P to transpose a given matrix*/
#include<stdio.h>
#include<conio.h>
void read(int [][5], int, int);
void display(int [][5], int, int);
void transpose(int [][5], int, int);
void main()
{
int a[5][5],m,n;
clrscr();
printf("Enter size of matrix: ");
scanf("%d %d",&m,&n);
read(a,m,n);
display(a,m,n);
transpose(a,m,n);
getch();
}
void read(int a[][5], int m, int n)
{
int i,j;

/*W.A.P to addition of two matrices*/
#include<stdio.h>
#include<conio.h>
void read(int [][5], int, int);
void display(int [][5], int, int);
void add(int [][5], int, int, int [][5], int, int);
void main()
{
int a[5][5],b[5][5],m,n,p,q;
clrscr();
printf("Enter the size of first matrix: ");
scanf("%d %d",&m,&n);
printf("Enter the elements for first matrix ");
read(a,m,n);
printf("Enter the size of second matirx: ");
scanf("%d %d",&p,&q);
printf("Enter the elements for second matrix ");
read(b,p,q);
printf("First matirx elements are \n");
display(a,m,n);

13

printf("Enter matrix elements: \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void display(int a[][5], int m, int n)
{
int i,j;
printf("the matrix elements are\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
void transpose(int a[][5], int m, int n)
{
int b[6][5],i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[j][i]=a[i][j];
printf("After interchange ");
display(b,n,m);
}

Output:
Enter size of matrix: 3 3
Enter matrix elements:
1 2 3 4 5 6 7 8 9
the matrix elements are
1 2 3
4 5 6
7 8 9
The resultant matrix is the matrix elements are
1 4 7
2 5 8
3 6 9
printf("Second matrix elements are \n");
display(b,p,q);
if(m==p&&n==q)
add(a,m,n,b,p,q);
else
printf("Addition is not possible ");
getch();
}
void read(int a[][5], int m, int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void display(int a[][5], int m, int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
void add (int a[][5], int m, int n, int b[][5], int p, int q)
{
int c[5][5],i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
printf("Resultant matrix elements are \n");
display(c,p,q);
}

Output:
Enter the size of first matrix: 2 2
Enter the elements for first matrix 1 2 3 4
Enter the size of second matirx: 2 2
Enter the elements for second matrix 5 6 7 8
First matirx elements are
1 2
3 4
Second matrix elements are
5 6
7 8
Resultant matrix elements are
6 8
10 12

/*W.A.P to subtraction of two matrices*/
#include<stdio.h>
#include<conio.h>
void read(int [][5], int, int);
void display(int [][5], int, int);
void sub(int [][5], int, int, int [][5], int, int);
void main()
{
int a[5][5],b[5][5],m,n,p,q;
clrscr();
printf("Enter the size of first matrix: ");
scanf("%d %d",&m,&n);
printf("Enter the elements for first matrix ");
read(a,m,n);
printf("Enter the size of second matirx: ");
scanf("%d %d",&p,&q);
printf("Enter the elements for second matrix ");
read(b,p,q);
printf("First matirx elements are \n");
display(a,m,n);
printf("Second matrix elements are \n");

/*W.A.P to multiplication of two matrices*/
#include<stdio.h>
#include<conio.h>
void read(int [][5], int, int);
void display(int [][5], int, int);
void multiplication(int [][5], int [][5], int, int, int);
void main()
{
int a[5][5],b[5][5],m,n,p,q;
clrscr();
printf("Enter the size of first matrix: ");
scanf("%d %d",&m,&n);
printf("Enter the elements for first matrix ");
read(a,m,n);
printf("Enter the size of second matirx: ");
scanf("%d %d",&p,&q);
printf("Enter the elements for second matrix ");
read(b,p,q);
printf("First matirx elements are \n");
display(a,m,n);
printf("Second matrix elements are \n");

14

display(b,p,q);
if(m==p&&n==q)
sub(a,m,n,b,p,q);
else
printf("Addition is not possible ");
getch();
}
void read(int a[][5], int m, int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void display(int a[][5], int m, int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
void sub(int a[][5], int m, int n, int b[][5], int p, int q)
{
int c[5][5],i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
printf("Resultant matrix elements are \n");
display(c,p,q);
}

Output:
Enter the size of first matrix: 2 2
Enter the elements for first matrix 1 2 3 4
Enter the size of second matirx: 2 2
Enter the elements for second matrix 1 2 3 4
First matirx elements are
1 2
3 4
Second matrix elements are
1 2
3 4
Resultant matrix elements are
0 0
0 0
display(b,p,q);
if(n==p)
multiplication(a,b,m,n,q);
else
printf("Matrix multiplication is not possible ");
getch();
}
void read(int a[][5], int m, int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void display(int a[][5], int m, int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
void multiplication(int a[][5], int b[][5], int m, int n, int q)
{
int c[5][5],i,j,k;
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("The resultant matrix is \n");
display(c,m,q);
}

Output:
Enter the size of first matrix: 2 2
Enter the elements for first matrix 1 2 3 4
Enter the size of second matirx: 2 2
Enter the elements for second matrix 5 6 7 8
First matirx elements are
1 2
3 4
Second matrix elements are
5 6
7 8
The resultant matrix is
19 22
43 50

Functions with Strings

/*W.A.P to read & print string values using functions*/
#include<stdio.h>
#include<conio.h>
void read(char [20]);
void display(char [20]);
void main()
{
char str[20];
clrscr();
read(str);
display(str);
getch();
}

/*W.A.P to find whether the given string is palindrome or not*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void read(char [20]);
void display(char [20]);
void palindrome(char []);
void main()
{
char str[20];
clrscr();
read(str);
display(str);

15

void read(char s[20])
{
printf("Enter a string: ");
gets(s);
}
void display(char s[])
{
printf("The string is ");
puts(s);
}

Output:
Enter a string: srinivas
The string is srinivas
palindrome(str);
getch();
}
void read(char s[20])
{
printf("Enter a string: ");
gets(s);
}
void display(char s[])
{
printf("The string is ");
puts(s);
}
void palindrome(char s[])
{
int length, c=0,i,j;
length=strlen(s);
for(i=0,j=length-1;i<=j;i++,j--)
if(s[i]==s[j])
c++;
if(c>=length/2)
printf("The string is palindrome");
else
printf("Not palindrome");
}

Output:
Enter a string: liril
The string is liril
The string is palindrome

/*W.A.P to read & print the student names using functions*/
#include<stdio.h>
#include<conio.h>
void readnames(char [][20], int);
void displaynames(char [][20],int);
void main()
{
char names[30][20];int n;
clrscr();
printf("Enter the no. of names you want to enter: ");
scanf("%d",&n);
readnames(names,n);
displaynames(names,n);
getch();
}
void readnames(char names[][20], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Enter %d element",i);
gets(names[i]);
}
}
void displaynames(char names[][20], int n)
{
int i;
printf("The names are ");
for(i=0;i<n;i++)
puts(names[i]);
}


/*Write a menu driven program to calculate statistical
parameters 1)mean 2)median 3)mode 4)variance 5)standard
deviation*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
float a[20];int n;
float mean();
float median();
void mode();
float variance();
void standarddeviation();
void main()
{
int choice,i;
clrscr();
printf("Output: \n");
printf("Enter no. of elements: ");
scanf("%d",&n);
printf("Enter array elements: \n");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
do
{
printf("1.mean\n 2.median\n 3.mode\n
4.variance\n 5.standard deviation\n 6.exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("mean =
%f\n",mean());
break;
case 2:printf("median =
%f\n",median());
break;
case 3:mode();
break;
case 4:printf("variance =
%f\n",variance());

16

break;
case 5:standarddeviation();
break;
case 6:exit(0);
break;
}
}while(choice!=6);
getch();
}
float mean()
{
int i;
float sum=0;
for(i=0;i<n;i++)
sum=sum+a[i];
return(sum/n);
}
float median()
{
int i,j,temp;
float med;
/*sort*/
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
if(n%2!=0)
med=a[n/2];
else
med=(a[n/2]+a[n/2-1])/2;
return med;
}
void mode()
{
float med,mn,md;
med=median();
mn=mean();
md=3*med-2*mn;
printf("mode = %f\n",md);
}
float variance()
{
float mn,sum=0;int i;
mn=mean();
for(i=0;i<n;i++)
sum=sum+(a[i]-mn)*(a[i]-mn);
return(sum/n);
}
void standarddeviation()
{
float var,sd;
var=variance();
sd=sqrt(var);
printf("SD = %f\n",sd);
}

Potrebbero piacerti anche