0% found this document useful (0 votes)
2 views

C_Lab_B

Uploaded by

mraj43621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C_Lab_B

Uploaded by

mraj43621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Q8.

C PROGRAM TO ACCEPT N NUMBER INTO AN ARRAY AND TO FIND THE SUM AND AVERAGE OF
THOSE NUMBER?

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,n;
float avg,sum=0;
clrscr();
printf("Enter the size of array");
scanf("%d",&n);
printf("Enter %d integer number\n",n);
for(i=0;i<n; i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{

sum=sum+a[i];
}
avg=(float)sum/n;
printf("\nSum is %f",sum);
printf("\n Average %f",avg);
getch();
}

OUTPUT
Q9. C PROGRAM TO ACCEPT A LIST OF ELEMENTS AND TO FIND THE MAXIMUM AND MINIMUM
ELEMENTS ALONG WITH THEIR POSITION?

include<stdio.h>
#include<conio.h>
void main()
{
int arr[10],n,i,big,small,bpos,spos;
clrscr();
printf("Enter the number of elements");
scanf("%d",&n);
printf("Enter the elements");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
big=arr[0];
small=arr[0];
bpos=1;
spos=1;
for(i=0;i<n;i++)
{
if(big<arr[i])
{
big=arr[i];
bpos=i+1;
}
if(small>arr[i])
{
small=arr[i];
spos=i+1;
}
}
printf("The largest number is %d and its position is %d\n",big,bpos);
printf("The smallest number is %d and its position is %d\n",small,spos);
getch();
}
OUTPUT
Q10. C PROGRAM TO ACCEPT A MATRIX AND FIND THE TRANSPOSE OF THE MATRIX. ALSO FIND
WHETHER THE GIVEN MATRIX IS SYMMETRIC OR NOT ?

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],m,n,i,j,flag;
clrscr();
flag=0;
printf("Enter rows and columns\n");
scanf("%d %d",&m,&n);
printf("Enter matrix element");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The matrix is \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Transpose of the matrix is\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",a[j][i]);
}
printf("\n");
}
if(m!=n)
{
printf("Matrix is not symmetric");
getch();
exit();
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]!=a[j][i])
{
flag=1;
break;
}
}
}
if(flag==0)
printf("\nMatrix is symmetric");
else
printf("\n Matrix is not symmetric");
getch();
}
OUTPUT:

Q11. C PROGRAM TO ACCEPT TWO MATRICES AND MULTIPLY THEM ?

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,p,q,j,i,k,sum=0;
int mat1[10][10],mat2[10][10],mat3[10][10];
clrscr();
printf("Enter number of rows and columns of first matrix");
scanf("%d%d",&n,&m);
printf("Enter the elements of first matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&mat1[i][j]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d",&p,&q);
if(n!=p)
printf("matrices with entered order can't be multiplied with each other\n");
else
{
printf("Enter the element of second matrix\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&mat2[i][j]);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<p;k++)
{
sum=sum+mat1[i][k]*mat2[k][j];
}
mat3[i][j]=sum;
sum=0;
}
}
printf("Product of the matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",mat3[i][j]);
printf("\n");
}
}
getch();
}
OUTPUT:

Q12. C PROGRAM TO ACCEPT A STRING AND CHECK WHETHER THE GIVEN STRING IS PALINDROME
OR NOT . (WITHOUT USING BUILT-IN FUNCTIONS) ?

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20];
int i,len;
int flag=0;
clrscr();
printf("Enter the string\n");
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]!=str[len-i-1])
{
flag=1;
break;
}
}
if(flag)
{
printf("%s is not a palindrome",str);
}
else
{
printf("%s is palindrome",str);
}
getch();
}
OUTPUT:

Q13. C PROGRAM TO DISPLAY FACTORIAL OF FIRST ‘n’ INTEGERS USING RECURSIVE FUNCTION ?

#include<stdio.h>
#include<conio.h>
long factorial(int n)
{
if(n==0)
return 1;
else
return(n*factorial(n-1));
}
void main()
{
int num;
long fact;
clrscr();
printf("Enter numberto find factorial\n");
scanf("%d",&num);
fact=factorial(num);
printf("factorial of %d is %ld",num,fact);
getch();
}
OUTPUT:

You might also like