Anda di halaman 1dari 28

Exercise 1.

Big Number Calculator

Pappu is very bad in mathematical calculation, and his job demands to do


addition, subtraction, multiplication and division with really big numbers. Your job
is to help Papu by to create a program which will read two numbers from the
standard input (keyboard) and do all the mathematical operations explained
above.
The input will be separated by a white space. And the output should follow the
format below
Use ADD : [NUM] for addition result
Use SUB : [NUM] for subtraction result
Use MUL : [NUM] for multiplication result
Use DIV : [NUM] for division result

Prefix ADD : , SUB :, MUL :, DIV : to each results as specified. Each result is
displayed in separate lines. Example is given below.

Code:

#include<iostream>
using namespace std;
int main()
{
int b,x,y,i,a,s,m,d;
cout<<"1) Addition\n";
cout<<"2) Subtraction\n";
cout<<"3) multiplication\n";
cout<<"4) division\n";
cout<<"Enter the set of numbers to be entered\n";
cin>>b;
for(i=0;i<b;i++)
{
cout<<"enter the "<<i+1<<"th set of number\n";
cin>>x>>y;
a=x+y;
s=x-y;
m=x*y;
d=x/y;
cout<<"Sum="<<a<<"\n";
cout<<"difference="<<s<<"\n";
cout<<"product="<<m<<"\n";
cout<<"quotient="<<d<<"\n";
}
}
1
Output:

1) Addition
2) Subtraction
3) multiplication
4) division
Enter the set of numbers to be entered
2
enter the 1th set of number
2
3
Sum=5
difference=-1
product=6
quotient=0
enter the 2th set of number
56
44
Sum=100
difference=12
product=2464
quotient=1

2
Exercise 1.2

Prime Number Generator


Mr. Sekhar wants to find all the prime numbers between two numbers, help him
by writing a program that finds all prime numbers between two limits.

Hint: Prime numbers are those numbers which is divisible only by that number
and by one.
Eg: 5 is a prime number where as 4 is not.

Code:

#include<iostream>
using namespace std;
int main()
{
int a,b,i,j,k=0;
cout<<"Enter the two numbers\n";
cin>>a>>b;
cout<<"the prime numbers are:\n";
for(i=a;i<=b;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
{
k++;
}
}
if(k==0)
cout<<i<<"\n";
k=0;
}
}
3
Output:

Enter the two numbers


1
10
the prime numbers are:
1
2
3
5
7

4
Exercise 1.3

Encryption and Decryption


A company wants to transmit data over telephone, but is concerned that its
phones could be tapped. All of the data are transmitted as integers. The company
has asked you to write a program that encrypts the data so that it can be
transmitted more securely. Your program should read a number and encrypt it as
follows: Replace each digit by that digit plus 7 modulus 10. Write a separate
program that inputs an encrypted data and decrypts it to form the original
number.

Code:

#include<iostream>
using namespace std;
#include<math>
int main()
{
int n,i,rem,sum=0;
cout<<"Enter the number\n";
cin>>n;
for(i=0;n>0;i++)
{
rem=n%10;
rem=(rem+7)%10;
sum+=rem*pow(10,i);
n=n/10;
}
cout<<"The encrypted code is:"<<sum<<"\n";
}
Output:
Enter the number
2345
The encrypted code is:9012

5
Exercise 2.1

Conversion of digits to words


A bank wants to help its customers by converting the digits which the customers
enter in their vouchers to its equivalent words, thereby increasing the efficiency
of their software and saving the time of their customers. Help the bank in
achieving this goal.

Code:

#include<iostream>
using namespace std;
int main()
{
int a,n=0,b,c,count=0,i;
cout<<"enter the number:\n";
cin>>a;
for(i=0;a%10==0;i++)
{
count++;
a=a/10;
}
while(a!=0)
{
b=a%10;
n=n*10+b;
a=a/10;
}
while(n!=0)
{
c=n%10;
n=n/10;
switch(c)
{
case 1:
cout<<"ONE";
break;
case 2:
cout<<"TWO";
break;
case 3:

6
cout<<"THREE";
break;
case 4:
cout<<"FOUR";
break;
case 5:
cout<<"FIVE";
break;
case 6:
cout<<"SIX";
break;
case 7:
cout<<"SEVEN";
break;
case 8:
cout<<"EIGHT";
break;
case 9:
cout<<"NINE";
break;
case 0:
cout<<"ZERO";
break;
}
c=0;
cout<<"\t";
}
for(i=0;i<count;i++)
{
cout<<"ZERO\t";
}
}

Output:

enter the number:


2345
TWO THREE FOUR FIVE

7
Exercise 2.2

Token Ordering Problem


Implement a program for a cashier of Global Bank to order bank transactions by
sorting tokens based on their token numbers. Search for a token, if it is present
remove that token from the list of tokens indicating that the transaction is
processed, and if not, add the new transaction by inserting that as a new token in
proper position to the list so that the new list is also a sorted one.

Code:

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the number of transaction:";
cin>>n;
int a[n],i,b,c,k;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the transaction to be removed and entered:";
cin>>b;
cin>>c;
for(i=0;i<n;i++)
{
if(b==a[i])
{
a[i]=0;
k=i;
}
}
cout<<"Transaction "<<b<<" is found and processed ";
cout<<"the remaining transactions are:\n";
for(i=0;i<n;i++)
{
if(a[i]!=0)
cout<<a[i]<<"\t";
}
cout<<"\nTransaction "<<c<<" is not found and added to the transaction
list\n";
a[k]=c;
cout<<"New list of transaction are:\n";
8
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}

Output:

Enter the number of transaction:5


325
300
120
234
123
Enter the transaction to be removed and entered:234
200
Transaction 234 is found and processed the remaining transactions are:
325 300 120 123
Transaction 200 is not found and added to the transaction list
New list of transaction are:
325 300 120 200 123

9
Exercise 2.3

Word Search Problem


Mr. Joy is searching a word in a sentence. Develop a program that facilitates him
to do the job fast.

Code:

#include<iostream>
#include<stdio>
#include<string>
using namespace std;
int main()
{
char ms[50],ss[20];
int i,j=0,s=0;
cout<<"Enter the mainstring:\n";
gets(ms);
cout<<"Enter the substring\n";
cin>>ss;
for(i=0;i<strlen(ms);i++)
{
if(ms[i]==ss[j])
{
j++;
if(j==strlen(ss))
{
s=1;
break;
}
}
else
j=0;
}
if(s==1)
cout<<"The given word is present.\n";
else
cout<<"The given word is not present.\n";
}

Output:

Enter the mainstring: amritavishwavidyapeetham


Enter the substring : vishwa
The given word is present.
10
Exercise 3.1

Generate Student Grade List with SGPA


Amrita University is planning to develop software to compute SGPA for the
students registered for a course for a particular semester. Take this project and
generate the Student Grade List with computed SGPA. Student Grade List should
contain Student name, Roll Number, Grades for Seven subjects, and SGPA.
Display the output in the order of roll numbers and also in the decreasing order of
SGPA.
Hint: Use Arrays or Structures for storing student information, Use manipulators
to format the output.
Grades Points: A+ - -> 10.0, A - -> 10.0, A- - -> 9
B+ - -> 8.0, B - -> 7.5, B- -->7.0
C+ - -> 6.5, C -->6.0, C- - ->5.5
D - -> 5, F - -> 0.0
Credits: Five subjects each having 4Credits, Two Labs each having
2Credits.

SGPA:
∑(Ci × Gp i ) where C = the credit for ith subject , Gp = Grade
∑Ci
i i

point for that subject.

Code:

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
void rno();
struct stud
{int rno;
char name[20];
char g[7][2];
float SGPA;
}ob[3],temp;

int main()
{int i=0,j,k=0;
float gp;
for(i=0;i<3;++i)
{cout<<"Enter details of student "<<i+1<<"\n";
cout<<"Enter rno:";
cin>>ob[i].rno;
11
cout<<"Enter name of the student:";
getchar();
gets(ob[i].name);
cout<<"Enter grades for five subjects and 2 labs:";
for(j=0;j<7;++j)
{//for(k=0;k<2;++k)
gets(ob[i].g[j]);
//getchar();
}

}
for(i=0;i<3;++i)
{ gp=0;
for(j=0;j<7;++j)
{ if(ob[i].g[j][0]=='A'&&ob[i].g[j][1]=='+')
{if(j<5)
gp+=40;
else
gp+=20;
}

else if(ob[i].g[j][0]=='A'&&ob[i].g[j][1]=='-')
{if(j<5)
gp+=36;
else
gp+=18;
}

else if(ob[i].g[j][0]=='A')
{if(j<5)
gp+=40;
else
gp+=20;
}

if(ob[i].g[j][0]=='B'&&ob[i].g[j][1]=='+')
{if(j<5)
gp+=32;
else
gp+=16;
}

if(ob[i].g[j][0]=='B'&&ob[i].g[j][1]=='-')

12
{if(j<5)
gp+=28;
else
gp+=14;
}

else if(ob[i].g[j][0]=='B')
{if(j<5)
gp+=30;
else
gp+=15;
}

if(ob[i].g[j][0]=='C'&&ob[i].g[j][1]=='+')
{if(j<5)
gp+=26;
else
gp+=13;
}

if(ob[i].g[j][0]=='C'&&ob[i].g[j][1]=='-')
{if(j<5)
gp+=22;
else
gp+=11;
}
else if(ob[i].g[j][0]=='C')
{if(j<5)
gp+=24;
else
gp+=12;
}

if(ob[i].g[j][0]=='D')
{if(j<5)
gp+=20;
else
gp+=10;
}

else if(ob[i].g[j][0]=='F')

13
{if(j<5)
gp+=0;
else
gp+=0;
}

}
ob[i].SGPA=gp/24;

rno();
}
void rno()
{int a[3],j,i;
for(i=0;i<3;++i)
{for(j=0;j<3-i-1;++j)
if(ob[j].rno>ob[j+1].rno)
{temp=ob[j];
ob[j]=ob[j+1];
ob[j+1]=temp;
}
}
cout<<"Sorted on the basis of roll no. :\n";
//set iosflags(ios::left);
//---------------------------------------------
cout<<"Roll no"<<setw(24)<<"Name"<<setw(24)<<"SGPA"<<endl;
for(i=0;i<3;++i)
{cout<<"-----------------------------------------------------------------------\
n";
cout<<ob[i].rno<<" "
<<ob[i].name<<setw(24)<<ob[i].SGPA<<endl;
}

cout<<"\nSorted on the basis of SGPA :\n";


for(i=0;i<3;++i)
{for(j=0;j<3-i-1;++j)
if(ob[j].SGPA<ob[j+1].SGPA)
{temp=ob[j];
ob[j]=ob[j+1];
ob[j+1]=temp;
}
}

14
cout<<"Roll no"<<setw(24)<<"Name"<<setw(24)<<"SGPA"<<endl;
for(i=0;i<3;++i)
{cout<<"-----------------------------------------------------------------------\
n";
cout<<ob[i].rno<<" "
<<ob[i].name<<setw(24)<<ob[i].SGPA<<endl;
}
cout<<"\n";

Output:

Enter details of student 1


Enter rno:23
Enter name of the student:sachin
Enter grades for five subjects and 2 labs:A
A
A
A
A
A
A
Enter details of student 2
Enter rno:64
Enter name of the student:vineeth
Enter grades for five subjects and 2 labs:A+
A+
A+
A+
A+
A+
A+
Enter details of student 3
Enter rno:12
Enter name of the student:asish
Enter grades for five subjects and 2 labs:A-
A-
A-
A-
A-
A-

15
A-
Sorted on the basis of roll no. :
Roll no Name SGPA
-----------------------------------------------------------------------
12 asish 9
-----------------------------------------------------------------------
23 sachin 10
-----------------------------------------------------------------------
64 vineeth 10

Sorted on the basis of SGPA :


Roll no Name SGPA
-----------------------------------------------------------------------
23 sachin 10
-----------------------------------------------------------------------
64 vineeth 10
-----------------------------------------------------------------------
12 asish 9

16
Exercise 3.2

Swap using function


Demonstrate the working of Call by value and Call by reference by
implementing a swap function that interchanges the contents of two
memory locations (integer variables).

Code:

#include<iostream>
using namespace std;
void callbyvalue(int x,int y);
void callbyref(int *,int *);

int main()
{
int a,b,ch=0;
while(ch!=3)
{
cout<<"CALL BY VALUE---------> 1\n";
cout<<"CALL BY REFERENCE-----> 2\n";
cout<<"EXIT------------------> 3\n";
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter two integers\na = ";
cin>>a;
cout<<"b = ";
cin>>b;
callbyvalue(a,b);
cout<<"\nAfter function call\na = "<<a<<"\nb = "<<b<<endl;
break;
case 2:
cout<<"Enter two integers\na = ";
cin>>a;
cout<<"b = ";
cin>>b;
17
callbyref(&a,&b);
cout<<"\nAfter function call\na = "<<a<<"\nb = "<<b<<endl;
break;
case 3:
break;
default:
cout<<"Wrong selection!!";
break;
}
}
}

void callbyvalue(int x,int y)


{
x = x + y;
y = x - y;
x = x - y;
cout<<"Inside the function\na = "<<x<<"\nb = "<<y;
}
void callbyref(int *x,int *y)
{
*x = *x + *y;
*y = *x - *y;
*x = *x - *y;
cout<<"Inside the function\na = "<<*x<<"\nb = "<<*y;
}

Output:

CALL BY VALUE---------> 1
CALL BY REFERENCE-----> 2
EXIT------------------> 3
Enter your choice: 2
Enter two integers
a=2
b=3
Inside the function
a=3
b=2
After function call
a=3
b=2
CALL BY VALUE---------> 1
CALL BY REFERENCE-----> 2
EXIT------------------> 3

18
Enter your choice: 1
Enter two integers
a=2
b=3
Inside the function
a=3
b=2
After function call
a=2
b=3
CALL BY VALUE---------> 1
CALL BY REFERENCE-----> 2
EXIT------------------> 3
Enter your choice: 3

19
Exercise 3.3

Tiling Problem
Mr. Sridhar wants to tile his room of specified length and width. Write a program
to help him that finds the number of tiles required to perform the task. Your
program should have the following features.
a) Use a structure Distance to store distances in terms feet and inches (both
are integers).
b) Length and width are the fields of Room structure (each of type
Distance structure).
c) Define a function Count that takes Room structure as argument and
returns the tile-count.

Code:

#include<iostream>
using namespace std;
struct dist
{
int ft;
int inch;
};

struct room
{
dist length,width;
};

int tilecount(int lf,int li,int wf,int wi);


dist sum(int af, int ai,int bf,int bi);

int main()
{
room r;
int n;
dist d1,d2,fi;
20
cout<<"Enter the length opf the room: ";
cout<<"Feet: ";
cin>>r.length.ft;
cout<<"Inch: ";
cin>>r.length.inch;
cout<<"Enter the width of the room: ";
cout<<"Feet: ";
cin>>r.width.ft;
cout<<"Inch: ";
cin>>r.width.inch;

n=tilecount(r.length.ft,r.length.inch,r.width.ft,r.width.inch);
cout<<"The no of tiles needed for the specified room is "<<n;

cout<<"\n\nEnter two lengths\nFirst length\nFeet: ";


cin>>d1.ft;
cout<<"Inch: ";
cin>>d1.inch;
cout<<"Second length\nFeet: ";
cin>>d2.ft;
cout<<"Inch: ";
cin>>d2.inch;
fi=sum(d1.ft,d1.inch,d2.ft,d2.inch);
cout<<"\nThe total length is "<<fi.ft<<" feet "<<fi.inch<<" inches\n";
}

int tilecount(int lf,int li,int wf,int wi)


{
int tile,n,l,w,a;
int rf,ri;
cout<<"\nSpecify the length of the side of the tile in inches: ";
cin>>tile;
l=((lf*12)+li);
w=((wf*12)+wi);
a=l*w;
n=a/(tile*tile);
return n;
}

dist sum(int af, int ai,int bf,int bi)


{
dist x;
x.ft=0;
int sum;
sum=((af+bf)*12)+(bi+ai);

21
for(int i=0;i<sum;i++)
{
x.inch++;
if(x.inch==12)
{ x.inch=0;
x.ft++;
}

}
return(x);
}

Output:

Enter the length opf the room: Feet: 5


Inch: 9
Enter the width of the room: Feet: 8
Inch: 6

Specify the length of the side of the tile in inches: 10


The no of tiles needed for the specified room is 70

Enter two lengths


First length
Feet: 12
Inch: 9
Second length
Feet: 12
Inch: 9

The total length is 25 feet 6 inches

22
Exercise 4.1

Classes and Objects


Implement a system that performs addition and multiplication of two Complex
numbers using a class Complex which has real-part and imaginary-part as
private data members and add and mul as two public member functions to add
and multiply two complex numbers. Also use two member functions to read and
display the complex numbers.

Code:

#include<iostream>
using namespace std;
class Complex
{
private:
int real;
int img;
public:
void read();
void display();
void add(Complex);
void mul(Complex);
};
void Complex::read()
{
cout<<"Enter the real part: ";
cin>>real;
cout<<"Enter the imaginary part: ";
cin>>img;
}
void Complex::display()
{
cout<<"The complex no. is "<<real<<"+i"<<img<<endl;
}
23
void Complex::add(Complex c)
{
Complex s;
s.real=real+c.real;
s.img=img+c.img;
s.display();
}
void Complex::mul(Complex c)
{
Complex p;
p.real=(real*c.real)-(img*c.img);
p.img=(img*c.real)+(real*c.img);
p.display();
}
int main()
{
Complex c1,c2;
c1.read();
c2.read();
cout<<"\tSUM"<<endl;
c1.add(c2);
cout<<"\tPRODUCT"<<endl;
c1.mul(c2);
}

Output:

Enter the real part: 4


Enter the imaginary part: 5
Enter the real part: 1
Enter the imaginary part: 3
SUM
The complex no. is 5+i8
PRODUCT
The complex no. is -11+i17

24
Exercise 4.2

Constructors and Destructors


Create a class Rectangle. It has two private data members to hold the length
and the breadth and one function is to calculate the area of a rectangle.
Calculate the area for three rectangles with three sets of value. Use default
constructor, parameterized constructor, default copy constructor, and a destructor.

Code:

#include<iostream>
using namespace std;
class Rectangle
{
private:
int length;
int breadth;
public:
Rectangle();
Rectangle(int l,int b);
void setvalue();
void area();
};
Rectangle::Rectangle()
{
length = 0;
breadth = 0;
}
Rectangle::Rectangle(int l,int b)
{
length = l;
breadth = b;
}
void Rectangle::setvalue()
{
cout<<"Enter the length of rectangle: ";
25
cin>>length;
cout<<"Enter the breadth of rectangle: ";
cin>>breadth;
}
void Rectangle::area()
{
cout<<"The area of the rectangle is "<<length*breadth<<endl;
}
int main()
{
Rectangle r1,r2,r3,r4;
cout<<"Rectangle 1"<<endl;
r1.area();
r2 = Rectangle(3,4);
cout<<"Rectangle 2"<<endl;
r2.area();
cout<<"Rectangle 3"<<endl;
r3.setvalue();
r3.area();
cout<<"Rectangle 4"<<endl;
r4 = r2;
r4.area();
}

Output:

Rectangle 1
The area of the rectangle is 0
Rectangle 2
The area of the rectangle is 12
Rectangle 3
Enter the length of rectangle: 5
Enter the breadth of rectangle: 2
The area of the rectangle is 10
Rectangle 4
The area of the rectangle is 12

26
Exercise 4.3

Counter Problem
Create a class called Counter. The counter class has one private data member to
hold the counter value. It has three member functions. Init to initialize the data
member, count to increment the counter whenever needed and display to
display the value of the counter. The counter works until the user wants to count.
Design a menu driven program.

Code:

#include<iostream>
using namespace std;
class Counter
{
private:
int counter;
public:
void init();
void count();
void display();
};
void Counter::init()
{
cout<<"Enter a value for the counter: ";
cin>>counter;
}
void Counter::count()
{
counter++;
}
void Counter::display()
{cout<<"The present value of counter is "<<counter<<endl;}

int main()
{
Counter c;
27
c.init();
c.count();
c.display();
}

Output:

Enter a value for the counter: 3


The present value of counter is 4

28

Anda mungkin juga menyukai