OOP Chapter 1 Solve
Programming Exercise 3.1 :
#include <iostream>
using namespace std;
void swap_val(int &a,int &b);
int main()
{
int a,b;
cout <<
"Enter two value: " << endl;
cin >> a
>> b;
swap_val(a,b);
}
void swap_val(int &a,int &b)
{
int temp;
temp = a;
a = b;
b = temp;
cout <<
"Exchange Value is \nA = " << a <<"\nB = "
<< b << endl;
}
Programming Exercise 3.2 :
#include <iostream>
using namespace std;
void set_val(int m);
int main()
{
int m;
cout <<
"Enter value for M: ";
cin >> m;
set_val(m);
return 0;
}
void set_val(int m)
{
int *var = new
int[m];
if(var!=0)
{
cout
<< "Memory allocation successful.......";
}
else
{
cout
<< "Memory allocation unsuccessful...... ";
}
}
Programming Exercise 3.3 :
#include <iostream>
using namespace std;
int main()
{
int i,m=1;
for(i=1;i<6;i++)
{
for(int
j=1;j<=i;j++)
{
cout
<< i;
}
cout
<< endl;
}
return 0;
}
Programming Exercise 3.4 :
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int p=1000,n=1;
float r =
.10,v;
for(int
i=1;i<11;i++)
{
v=p*(pow((1.0+r),n));
cout
<< "The value of V = " << v << endl;
p+=1000;
r+=.01;
n++;
}
return 0;
}
Programming
Exercise 3.5 :
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int
cont[5]={},i;
char c = 'n';
while(c=='n' ||
c=='N')
{
cout
<< "Enter your choice :\n1 for 1st Candidate\n2 for 2nd Candidate\n3
for 3rd Candidate\n4 for 4th Candidate\n5 for 5th Candidate\n";
cin
>> i;
switch(i)
{
case 1:
cont[0]++;break;
case 2:
cont[1]++;break;
case 3:
cont[2]++;break;
case 4:
cont[3]++;break;
case 5:
cont[4]++;break;
default:
cout << "Spoilt Ballot\n";break;
}
cout
<< "Press n or N to Exit\n";
c = getch();
}
cout <<
"\n\nResult : \n1st Candidate = " << cont[0]
<<
"\n2nd Candidate = " << cont[1]
<<
"\n3rd Candidate = " << cont[2]
<<"\n4th Candidate = " << cont[3]
<<
"\n5th Candidate = " << cont[4] << endl;
return 0;
}
Programming
Exercise 3.10 :
#include <iostream>
using namespace std;
int main()
{
string name;
int units;
cout <<
"Enter Your Name: ";
getline(cin,name);
cout <<
"Enter Usages in Units: ";
cin >>
units;
if(units>0&&units<101)
{
units=(units*60/100);
}
else
if(units>100&&units<301)
{
units=(units*80/100);
}
else
if(units>300)
{
units=(units*90/100);
if(units>300)
{
units+=(units*15/100);
}
}
cout <<
"Name : " << name << endl << "Bill : "
<< units;
return 0;
}
No comments:
Post a Comment