C++ File.m
C++ File.m
C++ File.m
//(a)C++ program
#include<iostream.h>
void main()
float x,y,z;
cout<<"enter x,y,z";
cin>>x>>y>>z;
if(x>y)
if(x>z)
cout<<"largest is"<<x;
else
cout<<"largest is"<<z;
#include<iostream.h>
void main()
float x,y,z,max;
cout<<"enter x,y,z";
1
cin>>x>>y>>z;
max=x;
if(y>max)
max=y;
if(z>max)
max=z;
cout<<"largest is"<<max;
#include<iostream.h>
void main()
float x,y,z,min;
cout<<"enter x,y,z";
cin>>x>>y>>z;
if(x<y)
{if(x<z)
cout<<"smallest is"<<x;
else
cout<<"smallest is"<<z;
else if(y<z)
cout<<"smallest is"<<y;
#include<iostream.h>
2
void main()
float x,y,z,max;
cout<<"enter x,y,z";
cin>>x>>y>>z;
max=x;
if(y>max)
max=y;
if(z>max)
max=z;
cout<<"largest is"<<max;
#include<iostream.h>
void main()
float x,y,z,min;
cout<<"enter x,y,z";
cin>>x>>y>>z;
min=x;
if(y<min)
min=y;
if(z<min)
min=z;
cout<<"smallest is"<<min;
}
3
//Write C++ programs to coefficients of a quadratic equation
//ax2+bx+c=0, the display roots of the equation (assume that the
//equation has real roots, such that √(b2-4ac)>=0)
#include<iostream.h>
#include<math.h>
void main ()
float a,b,c,x1,x2;
cout<<"enter a: ";
cin>>a;
cout<<"enter b: ";
cin>>b;
cout<<"enter c: ";
cin>>c;
x1=(-b+sqrt(b*b-4*a*c))/2*a;
x2=(-b-sqrt(b*b-4*a*c))/2*a;
cout<<"x1= "<<x1<<endl;
cout<<"x2= "<<x2;
//a. x+y ,
//b. x-y,
4
//c. x/y,
//d. x*y,
//e. x%y,
//C++ program
#include<iostream.h>
void main()
int x;
int y;
int a;
int b;
float c;
int d;
int e;
cout<<"x: ";
cin>>x;
cout<<"y: ";
cin>>y;
a=x+y;
cout<<"a= "<<a<<endl;
b=x-y;
cout<<"b= "<<b<<endl;
c=x/y;
cout<<"c= "<<c<<endl;
d=x*y;
cout<<"d= "<<d<<endl;
e=x%y;
5
cout<<"e= "<<e;
//Write C++ program to read a real number x and then evaluate the
//following functions
a) f(x)=x3+3x2-4x+2,
b) f(x)=(x2+5)(x2-5)
x2 +3
c) f(x)=x2√(x3+4)
d) f(x) = x-3
√(x^1.5-23)
C++ program
#include<iostream.h>
#include<math.h>
void main()
float x,f1,f2,f3,f4;
cout<<"x: ";
cin>>x;
f1=(x*x*x)*3*(x*x)-4*x+2;
f2=(x*x*+5)*(x*x-5)/(x*x+3);
f3=(x*x)*sqrt(x*x*x+4)/8;
f4=fabs(x-3)/sqrt(fabs(sqrt(x*x*x+4)-25));
cout<<"f1= "<<f1<<endl;
cout<<"f2= "<<f2<<endl;
6
cout<<"f3= "<<f3<<endl;
cout<<"f4= "<<f4;
#include<iostream.h>
#include<math.h>
void main()
int a=20;
int b=15;
int c=19;
float s,A0,A,B,C;
s=(a+b+c)/2;
A0=sqrt(s*(s-a)*(s-b)*(s-c));
A=asin(2*A0/(b*c));
B=asin(2*A0/(a*c));
C=asin(2*A0/(a*b));
cout<<"A0= "<<A0<<endl;
cout<<"A= "<<A<<endl;
cout<<"B= "<<B<<endl;
cout<<"C= "<<C;
A supplier has only one customer for a product. The customer purchases
the product only once a week. When the order is received, the supplier
7
meets the demand, if the sufficient stock is available. Otherwise, the
customer either lifts the entire stock available with the supplier or
refuses any purchases and goes elsewhere. Write a program to read the
existing stock with the supplier, the demand and the customer options
in case the demand exceeds the supply and to display the closing stock.
#include<iostream.h>
void main()
int openingstock;
int closingstock;
int demand;
int choice;
cout<<"enter openingstock";
cin>>openingstock;
cout<<"ente demand";
cin>>demand;
if(openingstock>=demand)
closingstock=openingstock-demand;
cout<<"closingstock= "<<closingstock;
else
cout<<"enter 1 to take all, else enter any other key to take none";
cin>>choice;
if(choice==1)
closingstock=0;}
8
else
closingstock=openingstock;
cout<<"closiningstock= "<<closingstock;
A cinema currently sells tickets for a full price of $6,but always sells
tickets for half price to people who are less than 16years old, and for a
third of the price for people who are 60years old or more. Use “cin” and
“if…else…”statements to complete the following program.
#include<iostream.h>
Void main ()
return 0;
#include<iostream.h>
void main()
9
{
int n=22;
cout<<"n="<<n<<endl;
n+=9;
cout<<"n+=9,n="<<n<<endl;
n-=5;
cout<<"n-=5,n="<<n<<endl;
n*=2;
cout<<"n*=2,n="<<n<<endl;
n/=3;
cout<<"n/=3,n="<<n<<endl;
n%=7;
cout<<"n%=7,n="<<n;
#include<iostream.h>
void main ()
float fahrenheit,celicius;
cin>>fahrenheit;
//compute
celicius=ratio*(fahrenheit-freeze);
#include<iostream.h>
void main()
int var1=10;
int var2;
int var3=50;
cout<<"var1: "<<var1<<endl;
cout<<"var3: "<<var3<<endl;
var2=var3-var1;
cout<<"var3-var1= "<<var2<<endl;
#include<iostream.h>
void main ()
int n=22;
cout<<"n= "<<n<<endl;
n+=9;
cout<<"after n+=9,n="<<n<<endl;
n-=5;
cout<<"after n-=5,n="<<n<<endl;
n*=2;
cout<<"after n*=2,n="<<n<<endl;
11
n/=3;
cout<<"after n/=3,n="<<n<<endl;
n%=7;
cout<<"after n%=7,n="<<n<<endl;
#include<iostream.h>
void main()
char c='A';
cout<<"c= "<<c<<endl;
c='B';
cout<<"c= "<<c<<endl;
c='t';
cout<<"c= "<<c<<endl;
#include<iostream.h>
void main ()
int m;
int n;
cout<<"two integers";
cin>>m>>n;
12
double a,b,c;
cin>>a>>b>>c;
char c1,c2,c3,c4;
cout<<"four charater";
cin>>c1>>c2>>c3>>c4;
#include<iostream.h>
void main()
int openingstock;
int closingstock;
int demand;
int choice;
cout<<"enter openingstock";
cin>>openingstock;
cout<<"ente demand";
cin>>demand;
if(openingstock>=demand)
closingstock=openingstock-demand;
cout<<"closingstock= "<<closingstock;
else
13
{
cout<<"enter 1 to take all, else enter any other key to take none";
cin>>choice;
if(choice==1)
closingstock=0;}
else
closingstock=openingstock;
cout<<"closiningstock= "<<closingstock;
#include<iostream.h>
void main()
float x0,x1,x2,x3,fx1,fx2;
cin>>x0;
cin>>x1;
x2=(x0+x1)/2;
fx1=600*x1*x1*x1*x1-500*x1*x1*x1-200*x1*x1-20*x1-1;
fx2=600*x2*x2*x2*x2-550*x2*x2-20*x2-1;
x3=(x2+x1)/2;
if (fx1*fx2<0)
14
{
cout<<"x3=(x1+x2)/2"<<endl;
else
cout<<"x3=(x0+x2)/2"<<endl;
//fabs(fx3)<0.0000001;
#include<iostream.h>
void main()
long int i;
i=10;
while(i<100)
i=i+1;
if((i%2!=0)&&(i%3!=0)&&(i%5!=0)&&(i%7!=0))
15
cout<<"the prime number is"<<i<<endl;
#include<iostream.h>
void main()
cout<<"enter day1";
cin>>day1;
cout<<"enter nodays";
cin>>nodays;
cout<<endl<<endl;
cout<<"mon\ttue\twen\tthu\tfr\tsat\sun";
for(i=1;i<=nodays;i++)
cout<<i<<"\t";
if(dcount%7==0)
cout<<endl;
dcount++;
#include<iostream.h>
void main()
16
{
int number,i;
cin>>number;
fact=1;
i=1;
fact=fact*i;
while(i<number)
i++;
fact=fact*i;
#include<iostream.h>
void main()
int n,i=1;
cin>>n;
long sum=0;
while(i<=n)
sum=sum+i++;
17
{
#include<iostream.h>
void main()
int number,i;
cin>>number;
fact=1;
i=1;
do
fact=fact*i;
i++;
while(i<=number);
#include<iostream.h>
void main()
{
18
int n,i=0;
cin>>n;
long sum=0;
do
sum+=i++;
while(i<=n);
#include<iostream.h>
void main()
int n;
for(n=1;n<=20;n++)
#include<iostream.h>
void main()
int n;
19
for(n=1;n<=10;n++)
//Use for
#include<iostream.h>
void main()
int i;
long int s;
s=0;
for(i=0;i<=6;i+=2)
s=s+i;
Use while
#include<iostream.h>
void main()
int i;
long int s;
i=0;
20
s=0;
while(i<6)
i+=2;
s=s+i;
#include<iostream.h>
void main()
int i=0;
int t=1;
while(i<4)
i++;
t=2*i+1;
s=s+t;
#include<iostream.h>
void main()
int i=0;
int t=1;
21
long int s=1;
while(i<4)
i++;
t=2*i+1;
s=s+t;
//WRITE A C++ PROGRAM THAT FIND THE SUM OF ODD NUMBERS BTN 0
AND 1000.
#include<iostream.h>
void main()
int i;
long int s;
i=1;
s=1;
for(i=1;i<999;i+=2)
s=s+i;
cout<<"sum is"<<s;
22
//THE PROGRAM TO CALCULATE THE FACTORIAL OF A NUMBER
#include<iostream.h>
void main()
cin>>number;
fact=1;
i=1;
while(i<number)
i++;
fact=fact*i;
cout<<"factorial of "<<number<<"="<<fact;
#include<iostream.h>
void main()
int i;
long int s;
s=0;
for(i=0;i<=1000;i+=2)
23
{
s=s+i;
#include<iostream.h>
void main()
int i;
long int s;
i=1;
s=1;
while(i<999)
i+=2;
s=s+i;
//3.PRIME NUMBER
#include<iostream.h>
void main()
int i;
i=10;
24
cout<<" "<<2<<" "<<3<<" "<<5<<" "<<7;
do
if((i%2!=0)&&(i%3!=0)&&(i%5!=0)&&(i%7!=0))
cout<<" "<<i;
i++;
while(i<97);
cout<<" "<<i;
//FOR LOOP
#include<iostream.h>
void main()
float x,s,mean;
int i;
int n;
s=0;
cin>>n;
for(i=1;i<=n;i++)
cin>>x;
s=s+x;
}
25
mean=s/n;
//DO WHILE
#include<iostream.h>
void main()
int i,n;
double x,s,mean;
i=1;
s=0;
cin>>x;
cin>>n;
do
i++;
s=s+x;
while(i<n);
mean=s/n;
//program to read x for the series of e^x and display the sum
#include<math.h>
void main()
double x,t, E;
int i;
i=0;
t=1;
E=1;
cin>>x;
do
i++;
t=x*t/i;
E=E+t;
while(fabs(t)>(0.0000001));
cout<<"exp("<<x<<")= "<<E;
#include<iostream.h>
#include<math.h>
float x1,x2;
x1=0.1;
x2=x1-F(x1)/DF(x1);
do
x1=x2;
x2=x1-F(x1)/DF(x1);
while(fabs(x1-x2)>0.0000001);
float F(float x)
return 600*(x*x*x*x)-550*(x*x*x)+200*(x*x)-20*(x)-1;
float DF(float x)
return 2400*(x*x*x)-1650*(x*x)+400*x-20;
#include<iostream.h>
void main()
int t;
28
//load the array
for(t=0;t<10;t++)
sample[t]=t;
for(t=0;t<10;t++)
//ARROYS.
#include<iostream.h>
void main()
int nums[5];
int i;
for(i=0;i<5;i++)
nums[i]=i;
cout<<"nums[i]: "<<nums[i]<<endl;
READ MATRICES A AND B AND FIND THE SUM OF A NAD B WHICH EQUAL
C
29
#include<iostream.h>
int n,m;
void main()
float A[10][10],B[10][10],C[10][10];
cout<<"enter row";
cin>>n;
cout<<"enter column";
cin>>m;
read(A);
read(B);
findsum(A,B,C);
display(C);
int i,j;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
cin>>M[i][j];
}
30
void findsum(float A[10][10],float B[10][10],float C[10][10])
int i,j;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
C[i][j]=A[i][j]+B[i][j];
int i,j;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
cout<<C[i][j]<<"\t";
cout<<endl;
SOME ARRAY
#include <iostream.h>
int main()
31
{
cout << "SomeArray[" << i << "][" << j << "]: ";
return 0;
ILLUSTRATIONS OF ARRAY
#include<iostream.h>
int main()
int i,number[10];
for(i=0;i<10;++i)
cin>>number[i];
for(i=9;i>=0;--i)
cout<<number[i]<<"\t";
32
}
return 0;
#include<iostream.h>
void read_data();
void sort_data();
void display_data();
int i,j;
int number[100];
int store,n;
void main()
read_data();
sort_data();
display_data();
void read_data()
cin>>n;
for(i=0;i<n;++i)
cin>>number[i];
void sort_data()
{
33
for(i=0;i<n;++i)
for(j=0;j<n;++j)
if(number[j]>number[j+1])
store=number[j];
number[j]=number[j+1];
number[j+1]=store;
void display_data()
for(i=0;i<n;++i)
cout<<number[i]<<" ";
#include<iostream.h>
int avg,range;
int sum;
void main()
int i,minvalue,maxvalue;
int nums[10];
nums[0]=10;
nums[1]=18;
34
nums[2]=75;
nums[3]=0;
nums[4]=1;
nums[5]=56;
nums[6]=100;
nums[7]=12;
nums[8]=-19;
nums[9]=88;
avg=0;
sum=0;
for(i=0;i<10;i++)
sum+=nums[i];
minvalue=maxvalue=nums[0];
for(i=0;i<10;++i)
if(nums[i]< minvalue)
minvalue=nums[i];
if(nums[i]> maxvalue)
maxvalue=nums[i];
35
}
range=maxvalue-minvale;
#include<iostream.h>
int main()
char str[80];
cout<<str;
return 0;
//Sorting data.cpp
//the program read the maximum 100 numbers and arrange them in
ascending order
#include<iostream.h>
void readdata();
void sortdata();
36
void displaydata();
int i,j;
int number[100];
int store,n;
void main()
readdata();
sortdata();
displaydata();
void readdata()
cin>>n;
cout<<"enter"<<n<<"number: ";
for(i=0;i<n;i++)
cin>>number[i];
void sortdata()
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(number[j]>number[j+1])
store=number[j];
number[j]=number[j+1];
number[j+1]=store;
37
}
void displaydata()
for(j=0;j<n;j++)
cout<<number[j]<<" ";
//Sorting data.cpp
//the program read the maximum 100 numbers and arrange them in
descending order
#include<iostream.h>
void readdata();
void sortdata();
void displaydata();
int i,j;
int number[100];
int store,n;
void main()
readdata();
sortdata();
displaydata();
void readdata()
for(i=0;i<n;i++)
cin>>number[i];
void sortdata()
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(number[j]<number[j+1])
store=number[j];
number[j]=number[j+1];
number[j+1]=store;
void displaydata()
for(j=0;j<n;j++)
cout<<number[j]<<" ";
#include<iostream.h>
39
#include<string.h>
void main()
char Firstname[20],Lastname[20];
cin>>Firstname;
cin>>Lastname;
cout<<endl<<endl;
#include<iostream.h>
void main()
int i,j;
int X[6][6];
for(i=0;i<6;i++)
for(j=0;j<6;j++)
if(i==j)
X[i][j]=0;
else if(i<j)
40
X[i][j]=3*i+j;
else
X[i][j]=2*i+3*j;
for(i=0;i<6;i++)
for(j=0;j<6;j++)
cout<<X[i][j]<<"\t";
cout<<endl<<endl;
#include<iostream.h>
#include<string.h>
void main()
char Firstname[20],Lastname[20];
cin>>Firstname;
cin>>Lastname;
cout<<endl<<endl;
41
cout<<"THE FIRST NAME HAS:"<<strlen(Firstname)<<"letters"<<"\n";
#include<iostream.h>
#include<string.h>
void main()
char Str1[50],Str2[50],Str3[50] ;
cin>>Str1;
cin>>Str2;
cout<<endl<<endl;
cout<<endl<<endl;
cout<<endl<<endl;
#include<iostream.h>
void main()
int i,j,n,m;
cin>>n;
cin>>m;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
if(i==j)
X[i][j]=0;
else if(i<j)
X[i][j]=3*i+j;
else
X[i][j]=2*i+3*j;
43
for(i=0;i<n;i++)
for(j=0;j<m;j++)
cout<<X[i][j]<<"\t";
cout<<endl<<endl;
#include<iostream.h>
#include<string.h>
void main()
char Str1[50],Str2[50];
cin>>Str1;
cin>>Str2;
//str1=str3
cout<<"Display Str3:"<<Str1<<endl;
45