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

C Language

The C program uses recursive functions to calculate: 1) The greatest common divisor (GCD) of two numbers by recursively calling the gcd function and returning the GCD once either number is 0. 2) The factorial of a given number by recursively multiplying the number by the factorial of one less than the number until the number is 0. 3) The Fibonacci series by recursively calling the function with the last two numbers to generate the next number in the series. 4) Exponentiation by recursively multiplying the base number by itself n-1 times.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

C Language

The C program uses recursive functions to calculate: 1) The greatest common divisor (GCD) of two numbers by recursively calling the gcd function and returning the GCD once either number is 0. 2) The factorial of a given number by recursively multiplying the number by the factorial of one less than the number until the number is 0. 3) The Fibonacci series by recursively calling the function with the last two numbers to generate the next number in the series. 4) Exponentiation by recursively multiplying the base number by itself n-1 times.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

7.b.

Write a C program to find mean, variance, standard deviation for a given list of
elements.
#include <stdio.h>
#include <math.h>
#define MAXSIZE 10
void main()
{
float nums[MAXSIZE];
int i, n;
float mean, var, sd, sum=0, v_sum=0; /* declares the variables*/
printf("Enter the number of elements\n"); /* accepts values from user */
scanf("%d", &n);
printf("Enter %d real numbers\n",n);
for (i = 0; i < n; i++)
{
scanf("%f", & nums[i]);
}
for(i=0; i<n; i++) /* Compute the sum of all elements */
{
sum = sum + nums[i];
}
mean = sum /(float) n; /* Calculating the mean using the equation */
for(i=0; i<n; i++) /* Compute variance and standard deviation */
{
v_sum= v_sum + pow((nums[i] - mean), 2);
}
var = v_sum / (float) n; /* calculate the variance using general equation */
sd = sqrt(var); /* calculating standard deviation */
/* prints the output mean, standard deviation, and variance */
printf("Mean of all elements = %.2f\n", mean);
printf("The variance of all elements = %.2f\n", var);
printf("Standard deviation SD = %.2f\n", sd);
}

7.c. Writea menu driven C program that allows a user to enter n numbers and then
choose between finding the smallest, largest, sum, or average. The menu and all the
choices are to be functions.Use a switch statement to determine what action to take.
Display an error message if an invalidchoice is entered.

#include<stdio.h>
void smallest(int a[],int n);
void largest(int a[],int n);
void sum(int a[],int n);
void average(int a[],int n);
int main()
{
int a[30], i, n, choice;
printf("\nEnter size of the list:");
scanf("%d", &n);
printf("\nEnter the elements into the list:");
for (i = 0; i< n; i++)
scanf("%d",&a[i]);
do
{
printf("\n1.smallest among numbers");
printf("\n 2. largest among numbers");
printf("\n 3. Sum of numbers");
printf("\n 4. Average of numbers");
printf("\n 5. Exit");
printf("\n enter the choice");
scanf("%d",&choice);
switch( choice)
{
case1:smallest(a,n); break;
case2:largest(a,n); break;
case3:sum(a,n); break;
case4:average(a,n); break;
case5:exit(0); break;
default: printf("\n enter the correct choice");
}
}while(choice<=5);
return 0;
}
void smallest(int a[],int n)
{
int i, small = a[0];
for (i = 0; i< n; i++)
{
if (a[i] <small)
{
small = a[i];
}
}
printf("\n Smallest Element : %d", small);
}
void largest(int a[],int n)
{
int i, large = a[0]; for(i=0;i<n;i++)
{
if(a[i] >large)
{
large= a[i];
}
}
printf("\nLargestElement:%d",large);
}
void sum(int a[],int n)
{
int i, sum=0;
for (i = 0; i< n; i++)
{
sum=sum+a[i];
}
printf("\nThe sum is : %d", sum);
}
void average(int a[],int n)
{
inti,sum=0;
float avg=0;
for (i = 0; i< n; i++)
{
sum=sum+a[i];
}
avg=sum/n;
printf("\nThe average is : %f", avg);
}

WEEK-8:
a. Write a C program to transpose a matrix.
#include <stdio.h>
int main(){
int m, n, i, j, matrix[10][10], transpose[10][10];
printf("Enter rows and columns :\n");
scanf("%d%d", &m, &n);
printf("Enter elements of the matrix\n");
for (i= 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &matrix[i][j]);
for (i = 0;i < m;i++)
for (j = 0; j < n; j++)
transpose[j][i] = matrix[i][j];
printf("Transpose of the matrix:\n");
for (i = 0; i< n; i++) {
for (j = 0; j < m; j++)
printf("%d\t", transpose[i][j]);
printf("\n");
}
return 0;
}

8.b Write a C program to perform the Addition of Two Matrices.


8.c Writea C program to perform the Multiplication of Two Matrices.

Program:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("\n enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n entered first matri xis=\n");
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
printf("%d",a[i][j]);
}
}
printf("\n enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n entered second matrix is=\n");
for(i=0;i<r;i++)
{
printf("\n");
{
for(j=0;j<c;j++)
{
printf("%d",b[i][j]);
}
}
printf("\n multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
}

WEEK-9:
Write programs using non recursive and recursive functions for the following
a. Find GCD.

#include <stdio.h>
int gcd(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, gcd(n1, n2));
return 0;
}

int gcd(int n1, int n2) {


if (n2 != 0)
return gcd(n2, n1 % n2);
else
return n1;
}

b. Find the factorial of a given number.


c. Generate the Fibonacci series.
d.

#include <stdio.h>
int power(int n1, int n2);
int main() {
int x, n, result;
printf("Enter base number: ");
scanf("%d", &x);
printf("Enter power number(positive integer): ");
scanf("%d", &n);
result = power(x, n);
printf("%d^%d = %d", x, n, result);
return 0;
}
int power(int x, int n)
{
if (n != 0)
return (x * power(x, n - 1));
else
return 1
}

You might also like