Pooja
Pooja
Program 1
Problem Statement: Write a C program to calculate factorial of a given number using recursion.
Objective: To understand the concept of Recursion in C language.
Formula used: n! = n * n-1 * n-2 ……… * 1
Source code!
#include<stdio.h>
int fact (int n)
{
if(n==0)
{
return 1;
}
else
{
return n*fact(n-1);
}
}
int main()
{
int a;
printf("Enter the number ");
scanf("%d",&a);
printf("Factorial of %d = %d " ,a,fact(a));
return 0;
}
2
Output
Program 2
Problem Statement: Write a C program to calculate the sum of nth term of ex.
Objective: To understand the concept of sum of exponential function of aylor series.
Formula used: ex = 1 + x/1! + x2/2! + x3/3! + ......
Source code!
#include<stdio.h>
int main()
{
int power;
int i,n = 30;
float temp=1,sum = 1;
printf("Enter the power ");
scanf("%d",&power);
for( i = 1 ; i <= n ; i++)
{
temp= (temp*power)/i;
sum = sum + temp;
}
printf("\n e^%d = %f ",power,sum);
return 0;
}
4
Output
C:\Users\Pooja \OneDrive\Desktop\code\c> gcc q1.c
C:\Users\Pooja \OneDrive\Desktop\code\c> ./a.exe
Enter the power 5
e^5 = 148.413177
5
Program 3
Problem Statement: Write a C program to calculate the roots of an quadratic equation ax2+bx+c=0 by
calculating discriminant (d).
Objective: To understand the concept of roots of an quadratic’s equations.
Formula used: ax2+bx+c=0 , (-b±√d)/2a , d=b2-4ac
Source code!
#include<stdio.h>
#include<math.h>
int main()
{
float a ,b,c,r1 ,r2,d;
printf("Enter the value of a , b and c \n");
scanf("%f %f %f",&a,&b,&c);
d = b*b -4*a*c;
if(d>0)
{
printf("The roots are real and different \n");
r1 = (-b+sqrt(d))/(2*a);
r2 = (-b-sqrt(d))/(2*a);
printf("root 1 is %f and root 2 is %f",r1,r2);
}
else if(d==0)
{
printf("The roots are real and equal \n");
r1 = -b/(2*a);
r2 = -b/(2*a);
printf("root 1 is %f and root 2 is %f",r1,r2);
}
else
{
printf("The roots are imaginary");
}
return 0;
}
6
Output
Users\Pooja \OneDrive\Desktop\code\c> gcc q1.c
Users\Pooja \OneDrive\Desktop\code\c> ./a.exe
Enter the value of a , b and c
1
2
5
The roots are imaginary
Program 4
Problem Statement: Write a C program to calculate the absolute error , relative error and percentage
error in numerical computation.
Objective: To understand the concept of absolute error , relative error and percentage error in
numerical computation.
Formula used:-
Absolute error : EA= X - X1
Relative error: ER = EA/X
Percentage error: EP= 100 × ER
Source code!
#include<stdio.h>
#include<math.h>
int main()
{
float a ,b,AE,RE ,PE;
printf("Enter the true value \n");
scanf("%f",&a);
printf("Enter the approx value \n");
scanf("%f",&b);
AE = a-b;
RE = AE/a;
PE = RE*100;
printf("Absolute Error = %f \n" , AE);
printf("Relative Error = %f \n" , RE);
printf("Percentage Error = %f \n" , PE);
return 0;
}
Output
Enter the true value
3.1415926
Enter the approx.value
3.1428571
Absolute Error = -0.001265
Relative Error = -0.000403
Percentage Error = -0.040253
8