Archive

Archive for January, 2010

C++ Program to find Sum of Series (When n is Odd/Even) – Q2

January 2, 2010 Leave a comment

Q2. Program to find Sum of Series:
i) x-x^3/3!-x^5/5!+x^7/7!-….. x^n/n! (When n is odd)
ii) x^2/2!-x^4/4!+x^6/6!-….. x^n/n! (When n is even)

… from College notes (BCA/MCA assignments):
 

#include <iostream.h>
#include <math.h>
#include <conio.h>

long Fact(long);
float Odd_Series(int, int);
float Even_Series(int , int);

int main(){
	int x, n;
	float sum;
	clrscr();

	cout<<"\n Enter the Value of x: ";
	cin>>x;
	cout<<"\n Enter the Value of n: ";
	cin>>n;

	sum = Odd_Series(x, n);
	cout<<"\n Sum of Odd Series: "<<sum;

	sum = Even_Series(x, n);
	cout<<"\n Sum of Even Series: "<<sum;

	getch();
	return 0;
	}

long Fact(long f){
	if(f<=0)
		return 1;
	else
		return (f * Fact(f-1));
}

float Odd_Series(int x, int n){
	int i, f;
	float sum = 0;
	for(i=1, f=0; i<=n; i=i+2, f++)
		sum += pow(-1, f) * pow(x, i) / Fact(i);
	return sum;
}

float Even_Series(int x, int n){
	int i, f;
	float sum = 0;

	for(i=2, f=0; i<=n; i=i+2, f++)
		sum += pow(-1, f) * pow(x, i) / Fact(i);

	return sum;
}

 

Output:

Enter the Value of x: 4

Enter the Value of n: 5

Sum of Odd Series: 1.866667
Sum of Even Series: -2.666667


Categories: Cpp Tags: ,

C++ Program to find Roots (Real & Complex) of a Quadratic Equation – Q1

January 1, 2010 Leave a comment

Q1. Program to find Roots (Real & Complex) of a Quadratic Equation:

… from College notes (BCA/MCA assignments):
 

#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <complex.h>

void main(){
	int aint, bint, cint, b2, dsq;
	float rt1 , rt2, d;
	clrscr();

	cout<<"\n Quadratic Equation is:- \n";
	cout<<"\n\t a*x^2 + b*x + c";

	cout<<"\n Enter the Value of a: ";
	cin>>aint;

	cout<<"\n Enter the Value of b: ";
	cin>>bint;

	cout<<"\n Enter the Value of c: ";
	cin>>cint;

	b2 = pow(bint, 2);
	d = b2 - 4 *  aint * cint;

	if(d > 0){
		dsq = sqrt(d);
		rt1 = (-bint + dsq) / (2 * aint);
		rt2 = (-bint - dsq) / (2 * aint);
		cout<<"\n Roots are Real and distinct";
		}
	else if(d == 0){
		rt1 = -bint/2 * aint;
		rt2 = rt1;
		cout<<"\n Roots are Real and same";
		}
	else {
		rt1 = -bint/2 * aint;
		rt2 = sqrt(-bint)/2 * aint;
		cout<<"\n Roots are Complex and distinct";
		}

	cout<<"\n rt1 : "<<rt1;
	cout<<"\n rt2 : "<<rt2;

	getch();
}

 

Output:

Equation is:-

a*x^2 + b*x + c

Enter the Value of a: 3

Enter the Value of b: 4

Enter the Value of c: 5

Roots are:-
rt1 : 9.476982e-41
rt2 : 219.999786