[go: up one dir, main page]

0% found this document useful (0 votes)
51 views29 pages

123

The document contains C code snippets to demonstrate various functions related to arrays and matrices. These include: 1) Defining a function to calculate the average of numbers in an array. 2) Creating a 2D matrix and defining functions to calculate the sum and product of two matrices. 3) Sorting an array of numbers in ascending order using a function. The code snippets show how to define and call functions to perform common operations on arrays and matrices in C, such as calculating averages, sums, products, sorting, etc. Functions are used to modularize the code and make it reusable.

Uploaded by

apoorvaereddy
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)
51 views29 pages

123

The document contains C code snippets to demonstrate various functions related to arrays and matrices. These include: 1) Defining a function to calculate the average of numbers in an array. 2) Creating a 2D matrix and defining functions to calculate the sum and product of two matrices. 3) Sorting an array of numbers in ascending order using a function. The code snippets show how to define and call functions to perform common operations on arrays and matrices in C, such as calculating averages, sums, products, sorting, etc. Functions are used to modularize the code and make it reusable.

Uploaded by

apoorvaereddy
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/ 29

33.

Find average of given numbers using function


// Program to find average of given numbers using function//
#include<stdio.h>
int main()
{
int a[25],n,i,avg;
printf("Enter the number of terms:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Number %d=",i);
scanf("%d",&a[i]);
}
avg=average(a,n);
printf("The average of the given list of numbers is %d",avg);
return 0;
}
int average(int x[],int m)
{
int sum=0,avg,i;
for(i=1;i<=m;i++)
sum=sum+x[i];
avg=sum/m;
return(avg);
}
Output:
Enter the number of terms:5
Number 1=25
Number 2=24
Number 3=26
Number 4=28
Number 5=27
The average of the given list of numbers is 26
Process returned 0 (0x0) execution time : 18.803 s

34.Find the squareroot of a given number using function


//Program to find the squareroot of a given number using function//
#include<stdio.h>
#include<math.h>
int main()
{
int x,sqrt;
printf("Enter a number :");
scanf("%d",&x);
sqrt=squareroot(x);
printf("The squareroot of the given number is: %d",sqrt);
return 0;
}
int squareroot(int s)
{
int no;
no=sqrt(s);
return(no);
}
Output:
Enter a number :144
The squareroot of the given number is: 12
Process returned 0 (0x0) execution time : 3.239 s

35. Find n th Fibonacci using function


// Program to find n th Fibonacci using function//
#include<stdio.h>
int main()
{
int x,fibo;
printf("Enter a number: ");
scanf("%d",&x);
fibo=fibonacci(x);
printf("The number which is situated in the place of %d is :%d",x,fibo);
return 0;
}

int fibonacci(int n)
{
if(n==1)
return(1);
else if(n==2)
return(1);
else
return (fibonacci(n-1)+fibonacci(n-2));
}
Output:
Enter a number: 6
The number which is situated in the place of 6 is :8
Process returned 0 (0x0) execution time : 6.424 s

36.Find the nature of quadratic equation:


//Program to find the nature of quadratic equation://
#include<stdio.h>
#include<math.h>
int main()
{
float dis,R1,R2,disc;
int a,b,c;
printf("Write the coefficients of the quadratic equation:");
scanf("%d %d %d",&a,&b,&c);
if (a==0)
{
if(b==0)
{
if(c==0)
printf("The roots are arbitrary");
else
printf("There are no real roots");
}
else
printf("There is only one root which is :%f",(-c)/b);
}
else
{
dis=discriminant(a,b,c);
if(dis<0)
printf("There are no real roots");
else
if(dis==0)
{
R1=-b/(2*a);
printf("There is one real root :%f",R1);
}
else
{
R1=(-b+sqrt(dis))/(2*a);
R2=(-b-sqrt(dis))/(2*a);
printf("There are two real roots which are %f %f",R1,R2);
}
}
return 0;
}

int discriminant(int x,int y,int z)


{
float disc=(y*y)-(4*x*z);
return(disc);
}

Output

Write the coefficients of the quadratic equation:


0
0
0
The roots are arbitrary
Write the coefficients of the quadratic equation:
1
6
9
There are two real roots which are -3 -3

Write the coefficients of the quadratic equation:


2
4
2
There is one real root :-1.000000

Write the coefficients of the quadratic equation:4


4
4
There are no real roots

37. Find NCR using recursion


// Program to find NCR using recursion//
#include<stdio.h>
int main()
{
int n,r,nf,rf,nrf,ncr;
printf("Enter the value of n:");
scanf("%d",&n);
printf("Enter the value of r:");
scanf("%d",&r);
nf=factorial(n);
rf=factorial(r);
nrf=factorial(n-r);
ncr=nf/(rf*nrf);
printf("The NCR value is: %d",ncr);
return 0;
}
int factorial(int x)
{
if(x==0)
return(1);
else
return(x*factorial(x-1));
}

Output:
Enter the value of n:5
Enter the value of r:3
The NCR value is: 10
Process returned 0 (0x0) execution time : 4.234 s
47.To sort the elements in ascending order using arrays and functions
//Program to sort the elements in ascending order using arrays and functions//
#include<stdio.h>
int main()
{
int minin,i,n,num[10],temp,num2[10];
printf("Enter the number of items:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter number%d : ",i+1);
scanf("%d",&num[i]);
}
printf("\n The numbers before sort:\n");
for(i=0;i<n;i++)
printf("%d\t",num[i]);
sort(n,num);

return 0;
}
int sort(int n1,int no[10])
{
int minin,i,j,temp;
for(i=0;i<n1-1;i++)
{
minin=i;
for(j=i+1;j<n1;j++)
{
if(no[j]<no[minin])
minin=j;
}
if(minin!=i)
{
temp=no[i];
no[i]=no[minin];
no[minin]=temp;
}
}
printf("\n The numbers after sort:\n");
for(i=0;i<n1;i++)
printf("%d\t",no[i]);
}

Output:

Enter the number of items:6


Enter number1 : 23
Enter number2 : 1
Enter number3 : 76
Enter number4 : 33
Enter number5 : 65
Enter number6 : 22

The numbers before sort:


23 1 76 33 65 22
The numbers after sort:
1 22 23 33 65 76

38. Create an array


//Program to create an array //
#include<stdio.h>
int main()
{
int i,s[10];
for(i=0;i<=3;i++)
{
printf("Number %d:",i+1);
scanf("%d",&s[i]);
}
printf("The array of numbers :");
for(i=0;i<=3;i++)
{
printf("%d",s[i]);
}
return 0;
}
Output:
Number 1:4
Number 2:5
Number 3:6
Number 4:6
The array of numbers :4566
Process returned 0 (0x0) execution time : 6.906 s

39. Create a character array


//Program to create a character array//
#include<stdio.h>
int main()
{
char a[10];
printf("Enter your name: ");
scanf("%s",&a);
printf("%s",a);
return 0;
}
Output:
Enter your name: Saathwika
Saathwika
Process returned 0 (0x0) execution time : 6.537 s

40.Reverse the given array


//Program to reverse the given array//
#include<stdio.h>
int main()
{
int num;
int n,i,k;
int temp[10];
printf("Enter a 6 digit number:\n");
scanf("%d",&num);
n=num;
i=0;
while(n!=0)
{
temp[i]=n%10;
n=n/10;
i++;
}
printf("The reverse of the number:");
for(k=0;k<6;k++)
{
printf("%d",temp[k]);
}
return 0;
}
Output:
Enter a 6 digit number:
547683
The reverse of the number:386745
Process returned 0 (0x0) execution time : 6.344 s

41. Program to find maximum, minimum, Sum and average using arrays
//Max,min,Sum,Avg using arrays//
#include<stdio.h>
int main()
{
int max,min,i,j,avg,sum=0,num[10];
for(i=0;i<10;i++)
{
printf("Enter number %d:",i+1);
scanf("%d",&num[i]);
sum=sum+num[i];
}
max=min=num[0];
avg=sum/10;
for(j=1;j<10;j++)
{
if(max<num[j])
max=num[j];
if(min>num[j])
min=num[j];
}
printf("\n The maximum of the given numbers:%d",max);
printf("\n The minimum of the given numbers:%d",min);
printf("\n The sum:%d",sum);
printf("\n the average:%d",avg);
return 0;
}
Output:
Enter number 1:5
Enter number 2:7
Enter number 3:10
Enter number 4:18
Enter number 5:23
Enter number 6:9
Enter number 7:51
Enter number 8:90
Enter number 9:12
Enter number 10:13

The maximum of the given numbers:90


The minimum of the given numbers:5
The sum:238
the average:23

42. Find the odds and even in the array


//Program to find the odds and even in the array//
#include<stdio.h>
int main()
{
int i,j,odd=0,even=0,num[5];
for(i=0;i<5;i++)
{
printf("Enter number%d : ",i+1);
scanf("%d",&num[i]);
}
for(j=0;j<5;j++)
{
if(num[j]%2==0)
{
printf("%d is even\n",num[j]);
even++;
}
else
{
printf("%d is odd\n",num[j]);
odd++;
}
}
printf("The number of even numbers:%d\n",even);
printf("The number of odd numbers:%d",odd);
return 0;
}
Output:
Enter number1 : 4
Enter number2 : 54
Enter number3 : 33
Enter number4 : 45
Enter number5 : 33
4 is even
54 is even
33 is odd
45 is odd
33 is odd
The number of even numbers:2
The number of odd numbers:3
Process returned 0 (0x0) execution time : 7.487 s

43.Find the first and second largest


//Program to find the first and second largest//
#include<stdio.h>
int main()
{
int minin,i,j,num[6],temp;
for(i=0;i<7;i++)
{
printf("Enter number%d : ",i+1);
scanf("%d",&num[i]);
}
for(i=0;i<7-1;i++)
{
minin=i;
for(j=i+1;j<7;j++)
{
if(num[j]<num[minin])
minin=j;
}
if(minin!=i)
{
temp=num[i];
num[i]=num[minin];
num[minin]=temp;
}
}
printf("The first largest number: %d",num[6]);
printf("\n\nThe second largest number : %d",num[5]);
}

Output:
Enter number1 : 23
Enter number2 : 34
Enter number3 : 56
Enter number4 : 777
Enter number5 : 78
Enter number6 : 89
Enter number7 : 90

The first largest number: 777


The second largest number : 90
Process returned 0 (0x0) execution time : 11.566 s
44. Create a matrix
//Program to create a matrix
#include<stdio.h>
int main()
{
int a[10][10];
int i,j,m,n;
printf("Enter the order of matrix \n");
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("Enter A(%d,%d) :",i+1,j+1);
scanf("%d",&a[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(" %d",a[i][j]);
printf("\t");
}
printf("\n");
}
return 0;
}
Output:
Enter the order of matrix
3
2
Enter A(1,1) :1
Enter A(1,2) :3
Enter A(2,1) :5
Enter A(2,2) :7
Enter A(3,1) :92
Enter A(3,2) :4
1 3
5 7
92 4

Process returned 0 (0x0) execution time : 9.472 s


45.Find the sum of two matrices
//Program to find the sum of two matrices//
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,m,n;
printf("Enter the order of matrix \n");
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("Enter A(%d,%d)",i+1,j+1);
scanf("%d",&a[i][j]);
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("Enter B(%d,%d)",i+1,j+1);
scanf("%d",&b[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Matrix A and Matrix B are \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)

printf(" %d",a[i][j]);
printf("\t");

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

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

printf(" %d",c[i][j]);
printf("\n");
}
return 0;

Output:

Enter the order of matrix


2
2
Enter A(1,1)12
Enter A(1,2)34
Enter A(2,1)55
Enter A(2,2)66
Enter B(1,1)77
Enter B(1,2)88
Enter B(2,1)12
Enter B(2,2)11
Matrix A and Matrix B are
12 34 77 88
55 66 12 11

Added matrix
89 122
67 77

46.Find the product of two matrices


//Program to find the product of two matrices//
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,k,l,m,n;
printf("Enter the order of matrix 1 \n");
scanf("%d %d",&l,&m);
printf("Enter the number of columns in 2 nd matrix:");
scanf("%d",&n);
for(i=0;i<l;i++)
for(j=0;j<m;j++)
{
printf("Enter A(%d,%d)=",i+1,j+1);
scanf("%d",&a[i][j]);
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("Enter B(%d,%d)=",i+1,j+1);
scanf("%d",&b[i][j]);
}
for(i=0;i<l;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
printf("The resultant matrix when A and B are multplied \n");
printf("Matrix A and Matrix B are \n");
for(i=0;i<l;i++)
{
for(j=0;j<m;j++)
{
printf(" %d",a[i][j]);
printf("\t");
}
printf("\n");
}
printf("\n\n\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(" %d",b[i][j]);
printf("\t");
}
printf("\n");
}
printf("\n Matrix C \n");
for(i=0;i<l;i++)
{
for(j=0;j<n;j++)
{
printf(" %d",c[i][j]);
printf("\t");
}
printf("\n");
}
return 0;

Output:
Enter the order of matrix 1
2
2
Enter the number of columns in 2 nd matrix:3
Enter A(1,1)=1
Enter A(1,2)=2
Enter A(2,1)=3
Enter A(2,2)=4
Enter B(1,1)=2
Enter B(1,2)=3
Enter B(1,3)=4
Enter B(2,1)=5
Enter B(2,2)=2
Enter B(2,3)=3
The resultant matrix when A and B are multplied
Matrix A and Matrix B are
1 2
3 4

2 3 4
5 2 3

Matrix C
12 7 10
26 17 24

Process returned 0 (0x0) execution time : 13.493 s

48. Find the transpose of a given matrix


//Program to find transpose of a given matrix//
#include<stdio.h>
int main()
{
int a[10][10],b[10][10];
int i,j,m;
printf("Enter the order of matrix \n");
scanf("%d",&m);
for(i=0;i<m;i++)
for(j=0;j<m;j++)
{
printf("Enter A(%d,%d)",i+1,j+1);
scanf("%d",&a[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf(" %d",a[i][j]);
printf("\t");
}
printf("\n");
}
printf("\n\n\n");

printf("The transpose of the given matrix:\n");


for(i=0;i<m;i++)
for(j=0;j<m;j++)
{
b[i][j]=a[j][i];
b[j][i]=a[i][j];
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf(" %d",b[i][j]);
printf("\t");
}
printf("\n");
}
return 0;
}
Output:
Enter the order of matrix
2
Enter A(1,1)3
Enter A(1,2)4
Enter A(2,1)55
Enter A(2,2)66
3 4
55 66

The transpose of the given matrix:


3 55
4 66

Pascal
#include<stdio.h>
int main()
{
int sum=0,i,j,k,n,nc;
printf("Enter the number of rows:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(k=1;k<=i;k++)
{

printf("%d ",fncr(i-1,k-1));

}
printf("\n");
}
return 0;
}
int fncr(int n,int r)
{
int p;
if(n==r)
return(1);
else if(n>r)
{
p=factorial(n)/(factorial(r)*factorial(n-r));
return(p);
}
else if(n<r)
{
return (0);
}
}
int factorial(int x)
{
int i,q;
if(x==0)
return(1);
else if(x>0)
{
q=x*factorial(x-1);
return(q);
}

}
Enter the number of rows:5
1
11
121
1331
14641

Process returned 0 (0x0) execution time : 6.244 s


#include<stdio.h>
int main()
{
int i,j,A[10][10],B[10][10],size,symmetric;
printf("Enter the size of a matrix \n");
scanf("%d",&size);
for(i=1;i<=size;i++)
for(j=1;j<=size;j++)
{
printf("Number (%d,%d): ",i,j);
scanf("\n %d",&A[i][j]);
}
printf("Matrix A is: \n");
for(i=1;i<=size;i++)
{
for(j=1;j<=size;j++)
{
printf("%d \t",A[i][j]);
}
printf("\n");
}
printf("Finding transpose of A: \n");
for(i=1;i<=size;i++)
{
for(j=1;j<=size;j++)
{
B[i][j]=A[j][i];
}
}
printf("Transpose Matrix is: \n");
for(i=1;i<=size;i++)
{
for(j=1;j<=size;j++)
{
printf("%d \t",B[i][j]);
}
printf("\n");
}
symmetric=0;
for(i=1;i<=size;i++)
{
for(j=1;j<=size;j++)
{
if(A[i][j]!=B[i][j])
symmetric=1;
break;
}
}
if(symmetric==1)
{
printf("The given matrix is not symmetric");
}
else
{
printf("The given matrix is symmetric");
}
return 0;
}
Press any key to continue.
Enter the size of a matrix
2
Number (1,1): 1
Number (1,2): 2
Number (2,1): 3
Number (2,2): 4
Matrix A is:
1 2
3 4
Finding transpose of A:
Transpose Matrix is:
1 3
2 4
The given matrix is not symmetric
Enter the size of a matrix
2
Number (1,1): 1
Number (1,2): 2
Number (2,1): 2
Number (2,2): 1
Matrix A is:
1 2
2 1
Finding transpose of A:
Transpose Matrix is:
1 2
2 1
The given matrix is symmetric
Process returned 0 (0x0) execution time : 4.882 s
51.Create a program to concatenate two strings and to find the length of the given string
//Program to concatenate two strings and to find the length of the given string
#include<stdio.h>
int main()
{
char a[20],b[20];
int length;
printf("Enter any two strings:");
scanf("%s %s",&a,&b);
printf("String 1: %s",a);
printf("\nString 2: %s",b);
printf("\nLength of String 1: %d",strlen(a));
printf("\nLength of String 2: %d",strlen(b));
printf("\nJoining two strings:%s",strcat(a,b));
printf("\nLength of Concatenated string: %d",strlen(a));
return 0;
}
Enter any two strings:
Sai
krishna
String 1: Sai
String 2: krishna
Length of String 1: 3
Length of String 2: 7
Joining two strings:Saikrishna
Length of Concatenated string: 10

52.Program to find compare, copy, uppercase and lowercase operations in string


#include<stdio.h>
int main()
{
char a[20],b[20];
int length;
printf("Enter any two strings:");
scanf("%s %s",&a,&b);
printf("String 1: %s",a);
printf("\nString 2: %s",b);
printf("\n\nChecking String 1==String 2: %d",strcmp(a,b));
printf("\nLowercase of a: %s",strlwr(a));
printf("\nUppercase of b: %s",strupr(b));
printf("\n\nCopy string 2 to string 1");
printf("\nCurrent value of a: %s",strcpy(a,b));
printf("\nChecking current String 1==String 2: %d",strcmp(a,b));
return 0;
}
OUTPUT
Enter any two strings:Sai
Saathwika
String 1: Sai
String 2: Saathwika

Checking String 1==String 2: 1


Lowercase of a: sai
Uppercase of b: SAATHWIKA

Copy string 2 to string 1


Current value of a: SAATHWIKA
Checking current String 1==String 2:

#include<stdio.h>
int main()
{
char str[15];
int d,n,i;
printf("Enter a string:");
scanf("%s",&str);
n=strlen(str);
for(i=0;i<=n;i++)
{
if(str[i]!=str[n-i-1])
{
d=1;
break;
}
}
if(d==1)
{
printf("It is not a palindrome");
}
else
printf("It is a palindrome");
return 0;
}
Enter a string:ab123321ba
It is a palindrome
Process returned 0 (0x0) execution time : 6.886 s
Press any key to continue.

Enter a string:run
It is not a palindrome
Process returned 0 (0x0) execution time : 4.004 s

You might also like