By using this programyou can find average of maximum 50 numbers. c++ code #include <iostream.h> void main() { int A[50],n; float sum=0; cout<<"How many numbers do you wish to enter : "; cin>>n; cout<<"\nEnter numbers : "; for(int i=0;i<n;i++) { cin>>A[i]; sum+=A[i]; } cout<<"\nAverage = "<<sum/n; } OUTPUT - How many numbers do you wish to enter :5 Enter numbers :5 4 8 7 6 Average =6
Posts
If there is any length given in feet and inches then you can use this code to convert it into cm. ex-if length is 3 feet and 8 inches then your fwill be=3.8 c++ code #include <iostream.h> #include <conio.h> void main() { clrscr(); float f; cout<<"Enter height in feet and inches : "; cin>>f; cout<<"\nHeight in cm = "<<f*12*2.54; getch(); } OUTPUT- Enter height in feet and inches : 3.8 Height in cm =115.824
If you want to run this program for more then five no.,then you have to modify A[5] and i<5. C++ code for array of five numbers. #include <iostream.h> void main() { int A[5],n=0,m=0; cout<<"Enter numbers : "; for(int i=0;i<5;i++) { cin>>A[i]; if(A[i]%2==0) n++; else m++; } cout<<"\nNo. of odd numbers = "<<m; cout<<"\nNo. of even numbers = "<<n; } OUTPUT- Enter numbers : 6 2 3 4 5 no. of odd numbers =2 no. of even numbers =3
Absolute value means only how far a number is from zero. or we can say, absolute value of a=|a| c++ code #include <iostream.h> #include <math.h> void main() { int a; cout<<"Enter number : "; cin>>a; cout<<"\nAbsolute value = "<<sqrt(a*a); //OR use - cout<<"\n"<<fabs(a); } OUTPUT- Enter number :-3 Absolute value =3
Here a and b are two no. and we are swapping them.It means we are interchanging the values of a and b. // #include<iostream.h> #include<conio.h> void main() {clrscr(); int *a,*b,*c,p,q; cout<<"enter 2 nos.="; cin>>p>>q; a=&p; b=&q; *c=*a; *a=*b; *b=*c; cout<<"swapping nos.="<<*a<<'\n'<<*c<<'\n'; getch(); } OUTPUT enter 2 nos.=7 5 swapping nos.=5 7
#include<iostream.h> #include<conio.h> void main() { clrscr(); intm,n,i,j,k; cout<<"enter the integer\n"; cin>>n; for(i=0;i<=n;i++) { for(j=0;j<=n-i;j++) cout<<" "; for(k=i;k>=0;k--) cout<<"*"; for(m=1;m<=i;m++) cout<<"*"; cout<<"\n"; } getch(); } OUTPUT enter the integer 5 * *** ***** ******* ********* ***********
c++ code is given below #include<iostream.h> #include<conio.h> int bsearch(int[],int,int); void main() {clrscr(); int ar[50],item,n,index; cout<<"\n enter the size of array :"; cin>>n; cout<<"\n enter the elements of the array:"; for(int i=0;i<n;i++) cin>>ar[i]; cout<<"\n enter the elements to be searched:"; cin>>item; index= bsearch(ar,n,item); if(index==-1) cout<<"\n sorry the no. is not found"; else cout<<"\n element found at index:"<<index<<" position"<<index+1; getch(); } int bsearch(int ar[],int n,int item) {int beg,last,mid; beg=0; last=n-1; while(beg<=last) { mid=(beg+last)/2; if(item==ar[mid])...
newton raphson method is a method to solve equations in numerical techniques. #include<iostream.h> #include<conio.h> #include<math.h> const double epsilon=0.00001; float f(float x) { float fx1; fx1=pow(x,4)+pow(x,2)-80; return fx1; } float f_dash(float x) { float fx2; fx2=4*pow(x,3)+2*x; return fx2; } void main() { cout.setf(ios::fixed); cout.setf(ios::showpoint); clrscr(); float x0,c; cout<<"Enter initial approximation\n"; cout<<"x0="; cin>>x0; cout<<"\nx0\t\tf(x0)\t\tf'(x0)\t\tc\n"; cout<<endl; c=x0-(f(x0)/f_dash(x0)); while(fabs(x0-c)>=epsilon) { cout<<x0<<"\t"<<f(x0)<<"\t"<<f_dash(x0)<<"\t"<<c<<endl; ...
#include<iostream.h> #include<conio.h> #include<math.h> float f(float x) { float f=pow(x,3)-18; return(f); } void main() { clrscr(); cout<<"the given equation is x^3-18:"<<endl; float x1,x2,x0,c,xm,n; cout<<"enter the interval(a,b)"<<endl; cout<<"\n enter x1="; cin>>x1; cout<<"\n enter x2="; cin>>x2; cout<<"\n the value of f("<<x1<<"):"<<f(x1); cout<<"\n the value of f("<<x2<<"):"<<f(x2); cout<<"\n the value of f("<<x0<<"):"<<f(x0); if (f(x1)*f(x2)<0) {do {x0=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1)); c=f(x1)*f(x0); if(c<0) x1=x0; else if(c>0) x2=x0; n++; if(c==0) break; xm=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1)); } while(fabs(xm-x0)>=0.0001); cout<<"root of the given equation on given tolerance is"<<x0<<endl; cou...
#include<iostream.h> #include<conio.h> #include<math.h> float f(float x) { float f=pow(x,3)+x-1; return(f); } void main() { clrscr(); cout<<"the given equation is x^3+x-1:"<<endl; float x1,x2,x0,c,xm,n; cout<<"enter the interval(a,b)"<<endl; cout<<"\n enter x1="; cin>>x1; cout<<"\n enter x2="; cin>>x2; cout<<"\n the value of f("<<x1<<"):"<<f(x1); cout<<"\n the value of f("<<x2<<"):"<<f(x2); cout<<"\n the value of f("<<x0<<"):"<<f(x0); if (f(x1)*f(x2)<0) {do {x0=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1)); c=f(x1)*f(x0); x1=x2; x2=x0; n++; if(c==0) break; xm=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1)); } while(fabs(xm-x0)>=0.0001); cout<<"root of the given equation on given tolerance is"<<x0<<endl; cout<<"no. of iter...
