Вы находитесь на странице: 1из 11

Data structure & algorithm Lab

Question: -
Create C++ program to exhibit basic calculator take the
number from the user and perform addition, subtraction, multiplication &
division?

#include<iostream>
usingnamespacestd;

int main()
{float num1,num2, sum;
char Alpha;
char x = 'a';
do
{
cout<<"What do you do in this program ?"<<endl;
cout<<"enter both num1 and num2 numbers"<<endl;
cin>> num1 >> num2;
cout<<"please enter capital letter from A for additon,S for subtraction, D for division and M
for multiply"<<endl;
cin>> Alpha;
switch (Alpha) // This is where the switch statement begins
{
case'A':
cout<<"you can add the number as you can do in calculator"<< sum <<endl;
sum = num2 + num1;
cout<<"the answer "<<" "<< sum <<endl;
break;
case'S': cout<<"you can subtract number as you can do in calculator"<<endl;
sum = num2 - num1;
cout<<"the answer "<<" "<< sum <<endl;
break;
case'M': cout<<"you can multiply number as you can do in calculator"<<endl;
sum = num2 * num1;
cout<<"the answer "<<" "<< sum <<endl;
break;
case'D': cout<<"you can divide the number as you can do in calculator"<<endl;
sum = num2 / num1;
cout<<"the answer "<<" "<< sum <<endl;
break;
default: cout<<"You did not enter an A, S, M,or D,"<<endl;
}
cout<<"do you want to continue calculating? Press any key to continue and 'n' if you are done
"<<endl;
cin>> x;
}
while (x != 'n');
{cout<<"have a good day"<<endl;
cout<<" CASIO "<<endl;
}
return 0;
}

Question: -
Create C++ program to find roots of Quadratic Equation take
values of a, b & c from the user and display the roots of Quadratic
Equations?
#include<iostream>
#include<cmath>

usingnamespacestd;
int main()
{
floata,b,c;
longint formula1, formula2;
char x = 'a';
do
{
cout<<"What do you do in this program ?"<<endl;
cout<<"enter a,b,c numbers"<<endl;
cin>> a >> b >>c;
formula1 = ( -b + sqrt(pow(b,2) -(4 * a * c))) / (2 * a);
formula2 = ( -b - sqrt(pow(b,2) -(4 * a * c))) / (2 * a);

cout<<"the answer is "<< formula1 <<" "<< formula2 <<endl;

cout<<"do you want to continue calculating? Press any key to continue and 'n' if you are done
"<<endl;
cin>> x;
}
while (x != 'n');
{cout<<"have a good day"<<endl;
cout<<" CASIO "<<endl;
}
return 0;
}

Question: -
Suppose that you are working for xyz-Tel Company and your boss has asked
you to make the billing software as per the following description:
1. Each call is going to be charged at the rate of 10 Rs per call if number
of calls is less than 100. If the number of calls is greater than 100 then each
call will be charged at 9 Rs.
2. Each sms is going to be charged at the rate of 1 Rs if number of SMS
are less than 100 otherwise they will be charged as 0.9 Rs per sms.
3. The user who have either made more than 200 calls or who have sent
more than 200 sms will get a special 50 Rs discount.
4. The users will have to pay 16.5 % General Sales Tax as well.

Solution: -
#include<iostream>
usingnamespacestd;

int main()
{
int call;
intsms;
floatsm;
floatcal;
float tax;
floattbill;
cout<<"please enter the whole mounth call "<<endl;
cin>> call;

if( call<100 )

{
cal = 10*call;
tax= 16.5/100*cal;
tbill=tax+cal;

cout<<"your total bill of the mouth of call with 16.5% tax is "<<tbill<<endl;
}
elseif( call>100 && call<200 )
{
cal = 9*call;
tax=16.5/100*cal;
tbill=tax+cal;
cout<<"your total bill of the mouth of call with 16.5% tax is "<<tbill<<endl;
}
elseif( call>200 )
{
cal = (9*call)/2;
tax=16.5/100*cal;
tbill=tax+cal;
cout<<"your total bill of the mouth of call with 16.5% tax is "<<tbill<<endl;
}

cout<<"please enter the whole mounthsms"<<endl;


cin>>sms;

if( sms<100 )
{
sm = 1*sms;
tax=16.5/100*sm;
tbill=tax+sm;
cout<<"your total bill of the mouth of sms with 16.5% tax is "<<tbill<<endl;
}
elseif( sms>100 && sms<200 )
{
sm = 0.9*sms;
tax=16.5/100*sm;
tbill=tax+sm;
cout<<"your total bill of the mouth of sms with 16.5% tax is "<<tbill<<endl;
}
elseif( sms>200 )
{
sm = (0.9*sms)/2;
tax=16.5/100*sm;
tbill=tax+sm;
cout<<"your total bill of the mouth of sms with 16.5% tax is "<<tbill<<endl;
}
return 0;
}

Question

Write the grade program in which we can enter


number and program tell the grade equivalent to
that number?
Solution: -
#include<iostream>
usingnamespace std;
int main ()
{
int marks;
cout <<"**********************************************"<< endl;
cout <<"**********WELCOME TO OUR PROGRAM**************"<< endl;
cout <<"**********************************************"<< endl <<
endl <<endl;

cout <<"please enter your marks"<< endl;

cin >> marks;

if ( marks >= 91 && marks<= 100 )


{
cout<<"your Grade is A";
}
if ( marks >= 81 && marks<= 90 )
{
cout<<"your Grade is B";
}
if ( marks >= 71 && marks<= 80 )
{
cout<<"your Grade is C";
}
if ( marks >= 61 && marks<= 70 )
{
cout<<"your Grade is D";
}
if ( marks >= 0 && marks<= 60 )
{
cout<<"your Grade is F";
}
if (marks>=60)
{
int reminder = marks%10;
if (reminder >=1 && reminder <=3)
cout <<"-"<<endl;

if (reminder >=4 && reminder <=7)


cout <<" "<<endl;

if (reminder >=8 && reminder <=9)


cout <<"+"<<endl;

}
return 0;
}

Question: -
Make a program that uses a structure variable of type phone to store
those three parts of a phone number separately after taking data from
user and then display the complete number?

#include<iostream>
usingnamespacestd;
structpart
{
intareacode;
intexchangecode;
intusernumber;
};
int main ()
{
part part1;
part1.areacode;
part1.exchangecode;
part1.usernumber;
cout<<" please enter the area code "<<endl;
cin>> part1.areacode;
cout<<" please enter the exchangecode "<<endl;
cin>> part1.exchangecode;
cout<<" please enter the usernumber "<<endl;
cin>> part1.usernumber;
cout<<" the full display "<< part1.areacode<<" - "<< part1.exchangecode <<" - "<<
part1.usernumber <<endl;
return 0;
}
Question -
Make a program that print the odd, even & prime number with loop. You
give the command between two number and program tell the number
are prime, odd & even number?
#include<iostream>
usingnamespacestd;
intnum;
int main()
{
intstart,end;
cout<<" Enter the starting number"<<endl;
cin>> start;
cout<<" Enter the ending number"<<endl;
cin>> end;
cout<<"Please enter E for even, O for odd and P for prime numbers"<<endl;
char choice;
cin>> choice;
switch (choice)
{
case'E':
cout<<" The Even number are"<<endl;
for(num=start;num<= end;num++)
{
if (num%2==0)
cout<<num<<endl;

} break;
case'O':
cout<<" The Odd number are"<<endl;
for(num=start;num<end; num++)
{
if (num%2==1)
cout<<num<<endl;

} break;
case'P':
cout<<" The Prime number are"<<endl;
for ( num=start; num<end; num++)
{
bool prime=true;
for (int j=2; j*j<=num; j++)
{
if (num % j == 0)
{
prime=false;
break;
}
}
if(prime) cout<<num<<endl;
}break;
}
Ahmed"<<endl;
return 0;
}
Data structure & algorithm Lab

Question : -
a) Declare a pointer.
b) Declare an integer array.
c) Assign the values of array.
d) Assign address of integer array to pointer.
e) Display pointer values and their addresses.
#include<iostream>
usingnamespacestd;

int main()
{
int*ptr1,*ptr2;
int array[2] = {5,8};

ptr1 = &array[0];
ptr2=&array[1];

cout<<"address OF POINTER is "<<ptr1<<endl;

cout<<"value AT POINTER is "<<*ptr1<<endl;

cout<<"address OF POINTER is "<<ptr1<<endl;

cout<<"value AT POINTER IS "<<*ptr2<<endl;

return 0;
}

Question -
Write a program class Rectangular to perform following.
a) Declare variable Length & width.
b) Function insert to enter data.
c) Function Area to calculate area of rectangle[Area= Length
*width]
d) Function Perimeter to calculate area of rectangle[Perimeter =
2(Length+width)
e) Function display to display data.

#include<iostream>
usingnamespacestd;
classRectangal
{
private:
float width;
float length;
float area;
float parameter;
public:
void insert (floatwd,floatln)
{ width = wd;
length= ln;
}
voidgetdata()
{
cout<<"Enter width of rectangle: "; cin>> width;
cout<<"Enter length of rectangle: "; cin>> length;
}
void Area()
{ area= length*width;}
void Parameter()
{ parameter= 2*length+2*width;}
void display()
{ cout<<"The area of the rectangle is: "<<area<<endl;
cout<<"The parameter of the rectangle is: "<<parameter<<endl;}
};
int main()
{
Rectangal R1;
R1.getdata();
R1.Area();
R1.Parameter();
R1.display();
return 0;
}

Question -
To find the factors!!

#include <iostream>
using namespace std;

int main()
{
int x;
cout<<"enter the number you want factors of"<<endl;
cin>>x;
for ( int i=1; i<=x; i++)
{if (x%i == 0)
{
cout <<i<<endl;
}

}
return 0;
}

Question -
To find the coordinates
#include <iostream>
using namespace std;
struct point
{
int x , y;
};
int main()
{ point p1,p2,p3;

cout<< "enter the x of first point"<<endl;


cin>> p1.x;
cout<< "enter y of first point"<<endl;
cin>> p1.y;
cout<< "enter the x of second point"<<endl;
cin>> p2.x;
cout<< "enter the y of second point"<<endl;
cin>> p2.y;
p3.x=p1.x+p2.x;
p3.y=p1.y+p2.y;
cout<<" the x cordinate of point three is"<<p3.x<<endl;
cout<<" the y cordinate of point three is"<<p3.y<<endl;
return 0;
}

Question -

Code for pointer definition


#include<iostream>
using namespace std;

void main()
{

int*ptr;
ptr =NULL;
cout<<"address is "<<endl;
cout<<ptr<<endl;
}
Lab Question -
:
declaring pointer and value
#include<iostream>
using namespace std;

void main()
{
int*ptr1,*ptr2;
int num1,num2;
num1 =5;
num2 =8;
ptr1 = &num1;
ptr2=&num2;
cout<<"address is"<<endl;
cout<<ptr1<<endl;
cout<<"value is"<<endl;
cout<<*ptr1<<endl;
cout<<"address is"<<endl;
cout<<ptr2<<endl;
cout<<"value is"<<endl;
cout<<*ptr2<<endl;
}

Question -

Using pointer with functions and string


#include<iostream>
#include<string>
using namespace std;

void main()
{
string *ptr;
string name="this is a string";
ptr = &name;
display(ptr);
}
void display(string *ptr)
{
cout<<*ptr<<endl;
}

adding two arrays !


#include <iostream>
using namespace std;

int main()
{
int a[5] = {2,3,4,5,6};
int b[5] = {1,3,5,7,9};
int c[5];

for(int i=0;i<5;i++)
{
c[i]=a[i]+b[i];
cout<<c[i]<<endl;
}
return 0;
}

Question -

Asking tp input array size and calculate

#include <iostream>
using namespace std;

int main()
{
int s,*arr;
int v;
cout<<"enter size of array"<<endl;
cin>>s;
arr=new int[s];
for(int i=0;i<s;i++)
{
cout<<"enter value"<<endl;
cin>>v;
arr[i]=v;
}
for(int k=0;k<s;k++)
{
cout<<"element on index"<<k<<"is"<<arr[k]<<endl;

}
delete[]arr;
for(int j=0;j<s;j++)
{
cout<<"element on index"<<j<<"is"<<arr[j]<<endl;
}
return 0;
}

Вам также может понравиться