Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
in
www.techiya.in
BCA Solved Assignment 2019-20
Q1. Write a program in C/C++ to find the solution of system of linear equations
(given below), by suing Gauss- Elimination method:
𝑥+𝑦+𝑧=2
𝑥−2𝑦+3𝑧=14
𝑥+3𝑦−6𝑧=−23
Ans: program.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int n,i,j,k;
float a[10][10]={0.0}, x[10]={0.0};
float pivot = 0.0;
float factor = 0.0;
float sum = 0.0;
printf("Gauss Elimination \n\n");
printf("Enter number of equations:");
scanf("%d",&n);
printf("%d\n",n);
printf("\n\t Input Coefficients a[i, j+1], row-wise\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
1
www.techiya.in
www.techiya.in
{
scanf("%f",&a[i][j]);
}
}
printf("\n\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
printf("\t%10.0f",a[i][j]);
}
printf("\n\n");
}
for(k=1;k<=n-1;k++)
{
if(a[k][k]==0.0)
{
printf("error");
}
else
{
pivot = a[k][k];
for(j=k;j<=n+1;j++)
a[k][j]= a[k][j]/pivot;
for(i=k+1;i<=n;i++)
{
factor = a[i][k];
for(j = k;j<=n+1;j++)
{
a[i][j] = a[i][j] - factor * a[k][j];
}
}
}
if(a[n][n]==0)
printf("error");
else
{
x[n] = a[n][n+1]/a[n][n];
2
www.techiya.in
www.techiya.in
for(i=n-1;i>=1;i--)
{
sum = 0.0;
for(j=i+1;j<=n;j++)
Output:
3
www.techiya.in
www.techiya.in
Ans: program.c
#include<stdio.h>
#include<conio.h>
float f(float x)
{
return(1/(1+x));
}
void main()
{
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
printf("\n Enter values of x0,xn,h: ");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf("\n Refined value of n and h are:%d %f\n",n,h);
printf("\n Y values: \n");
for(i=0; i<=n; i++)
{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf("\n %f\n",y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
4
www.techiya.in
www.techiya.in
if(i%2==1)
{
so=so+y[i];
}
else
{
se=se+y[i];
}
}
ans=h/3*(y[0]+y[n]+4*so+2*se);
printf("\n Final integration is %f",ans);
getch();
}
Output:
5
www.techiya.in
www.techiya.in
Q3. Write a program in C/C++ to find the value of Sin(𝜋/6) by using Lagrange’s
Interpolation, the related data is given below
x : 0 𝜋 /4 𝜋 /2
y= Sin(x) : 0 0.70711 1.0
Ans:
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
#define PI 3.1415
#define N 12
int main()
{
// Declare and initialize two arrays to hold the coordinates of the initial data
points
double x[N], y[N];
Output:
Q4. Write a program in C/C++ to calculate the value of “cos 𝑥” by using the
series expansion given below:
cos𝑥= 1−𝑥2/2!+𝑥4/4!−𝑥6/6!+⋯
Ans: program.cpp
#include <bits/stdc++.h>
using namespace std;
const double PI = 3.142;
double cosXSertiesSum(double x, int n)
{
x = x * (PI / 180.0);
double res = 1;
double sign = 1, fact = 1,
pow = 1;
for (int i = 1; i < 5; i++)
7
www.techiya.in
www.techiya.in
{
sign = sign * -1;
fact = fact * (2 * i - 1) * (2 * i);
pow = pow * x * x;
res = res + sign *
pow / fact;
}
return res;
}
// Driver Code
int main()
{
float x = 50;
int n = 5;
cout << cosXSertiesSum(x, 5);
return 0;
}
Output:
8
www.techiya.in
www.techiya.in
Q5. Write a program in C/C++ to find the root of the following equation by using
“Bisection Method”:
Equation:
𝑥3−5𝑥+1=0; 𝑥∈[1,2]
Ans: program.c
#include<stdio.h>
//function used is x^3-5x+1
double func(double x)
{
return x*x*x - 5*x + 1;
}
double e=0.01;
double c;
9
www.techiya.in
www.techiya.in
return;
}
c = a;
while ((b-a) >= e)
{
c = (a+b)/2;
if (func(c) == 0.0){
printf("Root = %lf\n",c);
break;
}
else if (func(c)*func(a) < 0){
printf("Root = %lf\n",c);
b = c;
}
else{
printf("Root = %lf\n",c);
a = c;
}
}
}
int main()
{
double a,b;
a=-10;
b=20;
printf("The function used is x^3-5x+1\n");
printf("a = %lf\n",a);
printf("b = %lf\n",b);
bisection(a,b);
printf("\n");
printf("Accurate Root calculated is = %lf\n",c);
return 0;
}
Output:
10
www.techiya.in
www.techiya.in
Q6. Write a program in C/C++ to approximate the value of Integral (I), by using
Trapezoidal rule:
Ans: program.c
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(1/(1+pow(x,2)));
}
void main()
{
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
printf("\n Enter values of x0,xn,h:\n");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
11
www.techiya.in
www.techiya.in
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf("\nrefined value of n and h are:%d %f\n",n,h);
printf("\n Y values \n");
for(i=0; i<=n; i++)
{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf("\n%f\n",y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
so=so+y[i];
}
else
{
se=se+y[i];
}
}
ans=h/3*(y[0]+y[n]+4*so+2*se);
printf("\nfinal integration is %f",ans);
getch();
}
Output:
12
www.techiya.in
www.techiya.in
Ans: forward.c
#include<conio.h>
#include<stdio.h>
#define F(x) (x)*(x) + (x) + 7 //FUNCTION F(X)
void main()
{
int n;
float a,b,h,x;
double f1,f2,f0,FWDOP,BWDOP,CDOP,AVGOP;
/*FWDOP – FORWARD DIFFERENCE OPERATOR*/
clrscr();
printf("\n Enter the interval (a,b)");
printf("\nEnter the value of a: ");
scanf("%f",&a);
printf("\nEnter the value of b: ");
scanf("%f",&b);
printf("\nEnter the number of nodes(n) in the interval (a,b): ");
13
www.techiya.in
www.techiya.in
scanf("%d",&n);
h=(b-a)/n; //STEP SIZE CALCULATION
printf("\nSTEP SIZE (h) =: %f",h);
printf("\n VALUE OF POINT OF EVALUATION (X) :");
scanf("%f",&x);
FWDOP = F(x+h) - F(x);
printf("\n RESULT OF FORWARD DIFFERENCE OPEATOR = %f",FWDOP);
getch();
}
Output:
14
www.techiya.in
www.techiya.in
/* CDOP – CENTRAL DIFFERENCE OPERATOR */
clrscr();
printf("\n Enter the interval (a,b)");
printf("\nEnter the value of a: ");
scanf("%f",&a);
printf("\nEnter the value of b: ");
scanf("%f",&b);
printf("\nEnter the number of nodes(n) in the interval (a,b): ");
scanf("%d",&n);
h=(b-a)/n; //STEP SIZE CALCULATION
printf("\nSTEP SIZE (h) =: %f",h);
printf("\n VALUE OF POINT OF EVALUATION (X) :");
scanf("%f",&x);
CDOP = F(x+(h/2)) - F(x-(h/2));
printf("\n RESULT OF CENTRAL DIFFERENCE OPEATOR = %f",CDOP);
getch();
}
Output:
15
www.techiya.in
www.techiya.in
Q8. Write a program in C or C++ to calculate the value of 𝑒𝑥 by suing its series
expansion, given below:
𝑒𝑥=1+𝑥+𝑥2/2!+𝑥3/3!+⋯
Ans: expo.c
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main()
{
int i, n;
float x, sum=1, t=1;
clrscr();
16
www.techiya.in
www.techiya.in
Output:
17
www.techiya.in