Professional Documents
Culture Documents
#include<iostream> #include<conio.h> using namespace std; int reverseDigit(int n) { int d,nn=0; while(n>0) { d=n%10; n=n/10; nn=nn*10+d;
} return nn; } int main() { cout<<"To reverse the inputted number\n"; int e; cout<<"\nEnter a number\n"; cin>>e; cout<<"The reverse digit is "<<reverseDigit(e); getch(); return 0;
Output:
#include<iostream> #include<conio.h> #include<string> using namespace std; int noc=0; double salary,pp; int getdata() { cout<<"To compute Federal Tax\n"; char a[50]; cout<<"\nEnter Marital Status: Married or not\n"; cin.getline(a,50); cout<<"\nEnter the percentage of income in pension plan\n"; cin>>pp; if(strcmp(a,"Married")==0) { cout<<"\nEnter the number of children under the age of 14\n"; cin>>noc; cout<<"Enter the combined salary of yours along with your spouse\n"; cin>>salary;
} double taxAmount() { int m=getdata(); double ti; if(m==1) ti=salary-(7000+1500*(noc+2)+pp*salary/100); else ti=salary-(4000+1500+pp*salary/100); if((0<=ti)&&(ti<=15000)) return(0.15*ti); else if((15001<=ti)&&(ti<=40000)) return(2250+0.25*(ti-15000)); else if(ti>=40001) return(8460+0.35*(ti-40000)); else return 0; }
Output:
Chapter 9 Question 1: Output the array so that 10 elements per line are printed.
#include<iostream> #include<conio.h> using namespace std; int main() { double a[50]; int x; for(x=0;x<50;x++) { if(x<=24) a[x]=x*x; else a[x]=x*3; } int t=1; for(x=0;x<50;x++) { if(t==11) { t=1; cout<<endl; }
Output:
#include<iostream> #include<conio.h>
using namespace std; int main() { cout<<"Sum Of The Series 1 + 1/2 + 1/3 + .....+ 1/n:\n"; double sum=0,n,x; cout<<"\nEnter the value of N \n"; cin>>n; for(x=1;x<=n;x++) sum=sum + (1/x); cout<<"\nThe value is "<<sum; getch(); return 0; }
Output:
#include<iostream> #include<conio.h> using namespace std; int main() { float t,nsum=0,psum=0; int n,x,i=0; cout<<"To CAlculate the Average of floating point numbers\n"; cout<<"\nEnter the total number of floating numbers to be entered \n"; cin>>n; cout<<"\nEter a floating point number\n";
for(x=1;x<=n;x++) { cin>>t; if(t<0) { i++; nsum+=(-1*t); } else psum+=t; } cout<<"\nAverage of negative floating point numbers is "<<(nsum/i)<<endl; cout<<"\nAverage of positive floating point numbers is "<<(psum/(n-i))<<endl; getch(); return 0; }
Output:
Syllabus Question 3: To find if the provided number is prime or not using Boolean function.
#include<iostream> #include<conio.h> using namespace std; bool prime(int n) { int f=0; for(int x=2;x<n;x++) { if(n%x==0) f=1; } if(f==1) return false; else
return true; }
int main() { cout<<"To find if the provided number is prime or not\n"; int i,t; cout<<"\nEnter a number\n"; cin>>i; t=prime(i); if(t==1) cout<<"\nYes a prime number it is"; else cout<<"\nNot a prime number"; getch(); return 0; }
Output:
#include<iostream> #include<conio.h> using namespace std; double absolute(double n) { if(n<0) return (-1*n); else return n; } int main() { cout<<"To Calculate the absolute value of number\n"; double e; cout<<"\nEnter a number\n"; cin>>e; cout<<"\nAbsolute value is "; cout<<absolute(e); getch(); return 0; }
Output:
#include<iostream> #include<conio.h> #include<ctime> using namespace std; int main() { cout<<"To generate a random number between 0 and 99\n"; int num; num=(rand() + time(0))%100;
Output:
#include<iostream> #include<conio.h> using namespace std; void printpyramid(int n) { int x,y; for(x=1;x<=n;x++) { for(y=1;y<=2*n;y++) { if(y>(n-x)&&y<(n+x)) cout<<"*"; else cout<<" "; } cout<<endl; }
} int main() { cout<<"To print pyramid of height n\n"; int k; cout<<"\nEnter a number\n"; cin>>k; printpyramid(k);
getch(); return 0; }
Output:
cout<<"Multiplication Of Two Arrays\n\n\n"; int i,j,p,sum=0,a[100][100],b[100][100],c[100][100],n,m,r,x,y; cout<<"Enter Array A size\n"; cin>>m>>n; cout<<"Enter Array B size\n"; cin>>n>>r; cout<<"\nEnter the elements of Array A\n"; for(x=0;x<m;x++) for(y=0;y<n;y++) cin>>a[x][y]; cout<<"\nEnter the elements of Array B\n"; for(x=0;x<n;x++) for(y=0;y<r;y++) cin>>b[x][y]; for(x=0;x<m;x++) { i=0; while(i<r) { sum=0; for(y=0;y<n;y++) { if(x<r) { p=a[x][y]*b[y][i]; sum=sum+p; }
for(x=0;x<m;x++) { for(y=0;y<n;y++) cout<<a[x][y]<<" "; cout<<endl; } cout<<"\nB = \n"; for(x=0;x<n;x++) { for(y=0;y<r;y++) cout<<b[x][y]<<" "; cout<<endl; } cout<<"\nA * B = \n"; for(x=0;x<m;x++) { for(y=0;y<r;y++) cout<<c[x][y]<<" "; cout<<endl;
} getch(); return 0; }
Output:
#include<iostream> #include<conio.h> using namespace std; int main() { cout<<"Arranging numbers in ascending order\n"; int a[100],x,y,t,d,n; cout<<"\nEnter total numbers to be entered in an array\n"; cin>>n; cout<<"\nEnter numbers\n"; for(x=0;x<n;x++) cin>>a[x]; for(x=0;x<n;x++) { t=a[x]; for(y=x+1;y<n;y++) { if(t>a[y]) { d=t; t=a[y]; a[y]=d; }
Output:
Syllabus Question 9: To Check if a given numbers exist in the provided list of 10 numbers.
#include<iostream> #include<conio.h> using namespace std; int main() { cout<<"To Check if a given numbers exist in the provided list of 10 numbers\n"; int n,a[10],x,f=0; cout<<"\nEnter number\n"; for(x=0;x<10;x++) cin>>a[x]; cout<<"Enter number to be searched\n"; cin>>n; for(x=0;x<10;x++) { if(a[x]==n) f=1; } if(f==1) cout<<"Yes the number exist in the list";
Output:
Syllabus Question 10: Bisection Method for the function x^3 + 2x^2 - 3x - 1
#include<iostream> #include<conio.h> #include<stdio.h> #include<math.h> using namespace std; float func(float t) { return(pow(t,3)+2*pow(t,2)-3*t-1); } int main() { cout<<"Bisection Method for the function x^3 + 2x^2 - 3x - 1\n"; float t,a,b,x,y; int n,i; cout<<"\nEnter the end points of the interval\n"; cin>>a>>b; cout<<"\nEnter the number of iterations\n"; cin>>n; if(func(a)>func(b)) { y=b; b=a;
a=y; } t=0; y=0; for(i=1;i<=n;i++) { x=(a+b)/2; t=func(x); if(t==0) { cout<<"The root is "<<x; y=1; break; }
else a=x; cout<<a<<"\n"<<b<<endl; } if(y!=1) cout<<"The approximated root is "<<x<<endl; getch(); return 0; }
Output:
Syllabus Question 11: Newton Root Findin Method for the function x^3 + 2x^2 - 3x - 1
#include<iostream> #include<conio.h> #include<math.h> using namespace std; double func(double t) { return(pow(t,3)+2*pow(t,2)-3*t-1); } int main() { cout<<"Newton Root Findin Method for the function x^3 + 2x^2 - 3x - 1\n"; double i,n,p,a,b,ya,yb,yp; cout<<"\nEnter the end points \n"; cin>>a>>b; cout<<"\nEnter number of iteration \n"; cin>>n; ya=func(a); yb=func(b); if(ya>yb) { double t=a; a=b; b=t; } if(ya*yb<0) {
for(i=1;i<=n;i++) { ya=func(a); yb=func(b); double t=yb-ya; p=b-yb*(b-a)/t; yp=func(p); if(yp<0) a=p; else b=p; } cout<<"Approximate value at "<<p<<endl; } else cout<<"Does not contain zero \n"; getch(); return 0; }
Output:
Syllabus Question 12: Secant Method for the function x^3 + 2x^2 - 3x - 1
#include<iostream> #include<conio.h>
#include<stdio.h> #include<math.h> using namespace std; float func(float t) { return(pow(t,3)+2*pow(t,2)-3*t-1); } int main() { cout<<"Secant Method for the function x^3 + 2x^2 - 3x - 1\n"; float a,b,x,fa,fb; int n,i; cout<<"\nEnter the two approximated roots\n"; cin>>a>>b; cout<<"\nEnter the number of iterations\n"; cin>>n; for(i=1;i<=n;i++) { fa=func(a); fb=func(b); x=a-(a-b)*fa/(fa-fb); b=a; a=x; } cout<<"The approximated root is "<<x<<endl; getch(); return 0;
Output:
#include<iostream> #include<conio.h> using namespace std; int main() { int x,y,i,n,o,niter,z; cout<<"\ Gauss Jacobi\n";
cout<<"\nEnter the order of the matrix\n"; cin>>n; cout<<"\nEnter number of iteration\n"; cin>>niter; float t,d[n][n],lu[n][n],h[n][n],b[n],c[n],sum,p; cout<<"\nEnter coefficient of matrix A row wise\n"; for(x=0;x<n;x++) { for(y=0;y<n;y++) { if(x==y) { cin>>t; d[x][y]=1/t; lu[x][y]=0; } else { cin>>lu[x][y]; d[x][y]=0; } } } cout<<"\nEnter vector co efficient\n"; for(x=0;x<n;x++) cin>>b[x]; for(x=0;x<n;x++)
} cout<<"\nEnter the approximated solution\n"; float soln[niter+1][n]; for(x=0;x<n;x++) cin>>soln[0][x]; for(x=1;x<=niter;x++) { i=0; o=x-1; for(z=0;z<n;z++) { sum=0; for(y=0;y<n;y++) { p=h[z][y]*soln[o][y]; sum=sum+p; } soln[x][i]=sum+c[i]; i++; } } cout<<"The required Soln is \n"; for(y=0;y<n;y++) cout<<soln[niter][y]<<endl; getch(); return 0; }
Output:
#include<iostream> #include<conio.h> #include<iomanip> #include<cmath> using namespace std; const int SIZE=3; float norm(float x[]); int main() { float a[SIZE][SIZE],b[SIZE],x[SIZE],oldx[SIZE],z[SIZE]; int i,j,k,count=0,n; float eps,sum; cout<<"Enter the cofficient matrix A "<<endl; for(i=0;i<SIZE;i++) for(j=0;j<SIZE;j++) cin>>a[i][j]; cout<<"Enter the vector b "<<endl; for(i=0;i<SIZE;i++) cin>>b[i]; cout<<"Enter the initial solution "<<endl; for(i=0;i<SIZE;i++) cin>>oldx[i]; cout<<"Enter the erroe tolrence"<<endl; cin>>eps; cout<<"Enter number of iteration"<<endl; cin>>n;
do { for(i=0;i<SIZE;i++) { sum=0; for(j=0;j<SIZE;j++) if(j<i) sum=sum+a[i][j]*x[j]; else if(j>i) sum=sum+a[i][j]*oldx[j]; x[i]=(b[i]-sum)/a[i][i]; z[i]=x[i]-oldx[i]; } for(i=0;i<SIZE;i++) oldx[i]=x[i]; count++; } while (norm(z)>eps&&count<n); if (norm(z)<=eps) { cout<<"number of itiration required"; cout<<count<<endl; cout<<"solution is"<<endl; for(i=0;i<SIZE;i++) cout<<" "<<x[i]<<endl;
} else { cout<<"Itiration are not sufficint "<<endl; cout<<"Solution after "<<count<<"Iteration is "<<endl;
for(i=0;i<SIZE;i++) cout<<" "<<x[i]<<endl; } getch(); return 0; } float norm(float x[]) {float max=fabs(x[0]); for(int i=0;i<SIZE;i++) if(max<fabs(x[i])) max=fabs(x[i]); return max; }
Output:
cout<<"Lagrange Interpolation\n"; double x[100],n,y[100],value=0,sum=0; int i,j,m; cout<<"\nEnter the number of points to be entered\n"; cin>>m; cout<<"\nEnter points\n"; for(i=0;i<m;i++) cin>>x[i]; cout<<"\nEnter corresponding function value\n";
for(i=0;i<m;i++) cin>>y[i]; cout<<"\nThe value at which value has to be found\n"; cin>>n; for(i=0;i<m;i++) { sum=y[i]; for(int j=0;j<m;j++) { if(j!=i) sum=((n-x[j])/(x[i]-x[j]))*sum; } value=value+sum; } cout<<"\nThe value at the given point is "<<value; getch(); return 0; }
Output:
#include<iostream> #include<conio.h> using namespace std; int main() { cout<<"Newton Interpolation\n"; double x[100],n,y[100][100],value=0,sum=0; int i,j,m; cout<<"\nEnter the number of points to be entered\n"; cin>>m; cout<<"\nEnter points\n"; for(i=0;i<m;i++) cin>>x[i]; cout<<"\nEnter corresponding function value\n"; for(i=0;i<m;i++) cin>>y[0][i]; cout<<"\nThe value at which value has to be found\n";
cin>>n; i=m; int col=1; while(i>1) { for(int j=0;j<(i-1);j++) y[col][j]=(y[col-1][j+1]-y[col-1][j])/(x[j+col]-x[j]); col++; i--; } for(i=0;i<m;i++) { sum=1; for(int j=0;j<i;j++) sum=sum*(n-x[j]); if(y[i][0]!=0) value=value+sum*y[i][0]; } cout<<"\nThe required value is "<<value; getch(); return 0; }
Output:
#include<iostream> #include<conio.h> using namespace std; float func(float t) { float fun=1.0/(1.0+t); return(fun); } int main() { cout<<"Trapezoidal Rule For the function 1/(1+x)\n"; float a,b,h,sum=0.0,x,trap; int n,i; cout<<"\nEnter limits for the function\n"; cin>>a>>b; cout<<"\nEnter the number of subinterval\n"; cin>>n; h=(b-a)/n; for(i=1;i<n;i++) { x=a+i*h; sum=sum+func(x);
Output:
#include<iostream> #include<conio.h> using namespace std; float func(float t) { float fun=1.0/(1.0+t); return(fun); } int main() { cout<<"Simpson Rule For the function 1/(1+x)\n"; float a,b,h,sum,x,sum1=0,sum2=0,simp; int n,i,n1,n2; cout<<"\nEnter limits for the function\n"; cin>>a>>b; cout<<"\nEnter the number of subinterval\n"; cin>>n; h=(b-a)/n; sum=func(a)+func(b); for(i=1;i<n;i+=2)
{ x=a+i*h; sum1=sum1+func(x); } for(i=2;i<(n-1);i+=2) { x=a+i*h; sum2=sum2+func(x); } simp=h*(sum+4*sum1+2*sum2)/3; cout<<"The value is "<<simp; getch(); return 0; }
Output:
#include<iostream> #include<conio.h>
#include<math.h> using namespace std; int factorial(int a) { int f=1; for(int x=1;x<=a;x++) f=f*x; return f; } int main() { cout<<"Newton Forward Difference\n"; int n,i,j,row; cout<<"\nEnter number of points to be entered\n"; cin>>n; float x[n],y[n+1][n],h,a,b,p,fact,sum,value,xinter,ss; cout<<"\nEnter Points\n"; for(i=0;i<n;i++) cin>>x[i]; cout<<"\nEnter corresponding function value\n"; for(i=0;i<n;i++) cin>>y[0][i]; cout<<"\nEnter point at which the value has to be found\n"; cin>>xinter; h=x[1]-x[0]; row =1; for(i=1;i<=n;i++)
value=y[0][0]; f or(i=1;i<n;i++) { sum=1; for(j=0;j<i;j++) sum=sum*(xinter-x[j]); fact=factorial(i); sum=sum/fact; ss=pow(h,i); sum=sum/ss; sum=sum*y[i][0]; value=value+sum;
getch(); return 0; }
Output:
{ int f=1; for(int x=1;x<=a;x++) f=f*x; return f; } int main() { cout<<"Newton Backward Difference\n"; int n,i,j,row; cout<<"\nEnter number of points to be entered\n"; cin>>n; float x[n],y[n+1][n],h,c,a,b,p,fact,sum,value,xinter,ss; cout<<"\nEnter Points\n"; for(i=0;i<n;i++) cin>>x[i]; cout<<"\nEnter corresponding function value\n"; for(i=0;i<n;i++) cin>>y[0][i]; cout<<"\nEnter point at which the value has to be found\n"; cin>>xinter; for(i=1;i<=n;i++) for(j=0;j<n;j++) y[i][j]=0; h=x[1]-x[0]; row =1; for(i=1;i<=n;i++)
row++; } value=y[0][n-1]; for(i=1;i<(n-1);i++) { sum=1; for(j=0;j<i;j++) sum=sum*(xinter-x[n-j-1]); fact=factorial(i); sum=sum/fact; ss=pow(h,i); sum=sum/ss; sum=sum*y[i][n-i-1]; value=value+sum; } cout<<"\nThe required value is "<<value; getch(); return 0; }
Output:
#include<iostream> #include<cmath> using namespace std; float func(float x); int main() { float p,ya,yb,yp,a,b; int i,n; cout<<"Enter the end points of the interval : "; cin>>a>>b;
cout<<"Enter the number of iterations : "; cin>>n; ya=func(a); yb=func(b); if(ya*yb<0) { for(i=1;i<=n;i++) { p=b-yb*((b-a)/(yb-ya)); yp=func(p); cout<<"The value of p at "<<i<<" iteraon is "<<p; cout<<" and the value of the function at this point is "<<yp<<"...."; cout<<endl; if(ya*yp<0) b=p; else a=p; } cout<<"The function (x^3+2*x^2-3*x-1) has its approximate value of zero at the point "<<p; cout<<" and the value of the function at this point is "<<yp<<"...."; } else { cout<<"This interval doesn't contain zero of the function (x^3+2*x^2-3*x-1)....."; cin.get(); cin.get(); return 0; } cin.get(); cin.get();
Output:
using namespace std; int main () { int i,j,k,n; float A[10][10],L[10][10],U[10][10],sum; cout<<"Enter the order of coefficient matrix"<<endl; cin>>n; cout<<"Enter the elements of coefficient matrix row-wise"<<endl; for(i=0;i<n;i++) for(j=0;j<n;j++) cin>>A[i][j];
for(i=0;i<n;i++) for(j=0;j<n;j++) if(i>j) U[i][j]=0; else if(i==j) L[i][j]=1; else L[i][j]=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { sum=0; if(i<=j) { for(k=0;k<n;k++) if(k!=i) sum=sum+L[i][k]*U[k][j];
U[i][j]=A[i][j]-sum; } else { for(k=0;k<n;k++) if(k!=j) sum=sum+L[i][k]*U[k][j]; L[i][j]=(A[i][j]-sum)/U[j][j]; } } } cout<<setprecision(3); cout<<"\n\n L="; for(i=0;i<n;i++) { for (j=0;j<n;j++) cout<<setw(3)<<L[i][j]; cout<<endl; cout<<" "; } cout<<"\n\n U="; for (i=0;i<n;i++) { for (j=0;j<n;j++) cout<<setw(3)<<U[i][j]; cout<<endl; cout<<" "; } getch(); return 0; }
Output:
TOPICS SIGNATURE
To check the number to be positive, negative or zero. To print the numbers in the ascending order. To print the number if the number is less than or equal to 9, otherwise alphabets.
25. 45-46 26. 48 27. 28. 29. 30. 31. 32. 33. 34. 65 35.
To check whether the triangle is a right triangle. To output the number of boxes and containers. To find the roots of the quadratic equation ax^2+bx+c=0. To indicate the position of the point. To mimic a calculator. To print the digits of the number. To print the number reversed. 58-59 60-61 62-63 54-55 56-57 4749-50 51-53
Program to convert letters to their corresponding telephone digits, To find and print the sum of the even and odd integers.
To find the sum of all the even integers and sum of the squares of the odd integers and number and their squares between 1 and 10 and to print the upper case letters between the two integers:firstNum and secondNum.
66-68
36.
37. 38.
Number guessing game. To print the reverse digit. To compute the Federal Tax.
39.
75-76
TOPICS SIGNATURE
Calculate the absolute value of number. 82 83
46.
84-85
47.
86-88
48.
89-90
49.
91-92
50.
93-94
51.
95-96
52.
97-98
53.
99-101
54.
102-104
55.
Lagrange Interpolation
105-106
56.
Newton Interpolation
107-108
57.
109-110
58.
111-112