0% found this document useful (0 votes)
26 views8 pages

Pooja

The document contains details of 4 programs written in C language by a student. Each program contains the problem statement, objective, formulas used and sample source code and output. The programs include calculating factorial using recursion, calculating sum of exponential function using Taylor series, finding roots of quadratic equation and calculating errors in numerical computation.

Uploaded by

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

Pooja

The document contains details of 4 programs written in C language by a student. Each program contains the problem statement, objective, formulas used and sample source code and output. The programs include calculating factorial using recursion, calculating sum of exponential function using Taylor series, finding roots of quadratic equation and calculating errors in numerical computation.

Uploaded by

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

Name: Pooja 1

University Roll number: 2221427


Course: BCA
Section: C2

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

C:\Users\pooja \OneDrive\Desktop\code\c> gcc q1.c


C:\Users\Pooja \OneDrive\Desktop\code\c> ./a.exe
Enter the number 4
Factorial of 4 = 24
3

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

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
5
2
The roots are real and different
root 1 is -0.438447 and root 2 is -4.561553

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
1
The roots are real and equal
root 1 is -1.000000 and root 2 is -1.000000
7

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

You might also like