/* *******************************************************
57.To find the sum of the elements in an array using functions
******************************************************* */
#include<*stdio.h>
#include<*conio.h>
int sum(int);
void main()
{
int n,s;
clrscr();
printf("Enter the number of elements in the array\n");
scanf("%d",&n);
s=sum(n);
printf("The sum of the elements in the array is %d",s);
getch();
}
int sum(int n)
{
int s=0,arr[20],i;
printf("Enter the array elements");
for(i=0;i
scanf("%d",&arr[i]);
for(i=0;i
s=s+arr[i];
return(s);
}
OUTPUT
Enter the number of elements in the array
5
Enter the array elements 1 2 3 4 5
The sum of the elements in the array is 15
/* ***********************************************
58.To find the transpose of a matrix using functions
*********************************************** */
#include<*stdio.h>
#include<*conio.h>
void trans(int[3][3]);
void main()
{
int i,j,arr[3][3];
clrscr();
printf("Enter the elements to the array (3x3)\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&arr[i][j]);
}
trans(arr);
getch();
}
void trans(int ar[3][3])
{
int tra[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
tra[i][j]=ar[j][i];
}
printf("The transpose is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",tra[i][j]);
}
printf("\n\n");
}
}
OUTPUT
Enter the elements to the array (3x3)
1 2 3 4 5 6 7 8 9
The transpose is
1 4 7
2 5 8
3 6 9
/* **********************************************
59.To find the length of a string using functions
********************************************* */
#include<*stdio.h>
#include<*conio.h>
int len(char[20]);
void main()
{
char s[20];
int l;
clrscr();
printf("Enter the string\n");
scanf("%s",&s);
l=len(s);
printf("The length of the string is %d",l);
getch();
}
int len(char st[20])
{
int i=0,l=0;
while(st[i]!='\0')
{
l++;
i++;
}
return(l);
}
OUTPUT
Enter the string
Successful
The length of the string is 10
/* *************************************************
60.To find the factorial of a number using recursion
************************************************* */
#include<*stdio.h>
#include<*conio.h>
int fact(int);
void main()
{
int n,f;
clrscr();
printf("Enter the number whose factorial has to be found\n");
scanf("%d",&n);
f=fact(n);
printf("The factorial of %d is %d ",n,f);
getch();
}
int fact(int n)
{
if(n==1)
return(1);
else
return(n*fact(n-1));
}
OUTPUT
Enter the number whose factorial has to be found
5
The factorial of 5 is 120
0 comments:
Post a Comment