Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Assignment 31

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Uilyhi

Assignment 3
Q.1 Write a C++ program that inputs experience and age of a person. The salary of
the person is 6000 if the person is experienced and his age is more than 35,
otherwise if the person is experienced and his age is more than 28 but
less than 35 than the salary should be 4800 otherwise for experienced person the
salary should be 3000 and for inexperienced person the salary should be 2000.The
program should be made using ternary operator.
Code:
#include<iostream>
using namespace std;
int main()
{char exp;
int age,sal;
cout<<"Press e for experienced"<<endl<<"Press u for unexperienced"<<endl;
cin>>exp;
if(exp=='e')
{cout<<"Enter age";cin>>age;
if(age<28)
cout<<"Salary=3000";
else if (sal=age>35?sal=6000:sal=4800)
cout<<"Salary="<<sal;}
else cout<<"Salary=2000";
return 0;}
Output:
Uilyhi
Uilyhi

Q.2 A computer programming contest requires teams of five members each. Write
a program that asks for the number of players, and then gives the number of teams
and number of players left over.
Code:
#include <iostream>
using namespace std;
int main(){int lp,pp,tp;
cout<<"Enter total no. of players who need to be divided equally in team of 5
each"<<endl;
cin>>tp;
pp=tp/5;lp=tp%5;
cout<<"Total no.of teams of 5 players formed="<<" "<<pp<<endl<<"Left
Players="<<" "<<lp;
}
Output:
Uilyhi

Q.3 The value of e is known to be 2.71828. Using this value, write a program to
determine the value of the expression: 2 – ye2y + 4y. Obtain the value of y from
the user.
Code:
#include <iostream>
#include<cmath>
using namespace std;
int main(){float y,sol,t,u;
cout<<"Enter any no."<<endl;
cin>>y;
sol=2.0-y*pow(2.71828,2*y)+pow(4,y);
cout<<"Solution"<<" "<<sol;
return 0;
}
Output:
Uilyhi

Q4. Write a program to compute simple interest and compound interest. Take
values from user.
Code:
#include <iostream>
#include<math.h>
using namespace std;
int main(){float p,si,ci,r,t;char choice;
do{
cout<<"Press s for calculating simple interest"<<endl<<"Press c for calculating
monthly compound interest"<<endl<<"Press e to exit"<<" ";
cin>>choice;
switch(choice)
{case 's':
{cout<<"enter principle amount,rate of interest in percentage,time period"<<" ";
cin>>p>>r>>t;
si=(r*p*t)/100;
cout<<"Simple interest="<<" "<<si<<"\n";break;}
case'c':
{cout<<"enter principle amount,rate of interest in percentage,time period"<<" ";
cin>>p>>r>>t;
ci=pow(r/1200+1,12*t)*p-p;
cout<<"Compound interest="<<" "<<ci<<"\n";break;}
case'e':
{cout<<"Exit";break;}
}
} while (choice!='e');}
Uilyhi

Output:
Uilyhi

Q.5 Write a program that reads in a character <char> from the keyboard and then
displays one of the following messages: Hint. You will need to refer to a table of
ASCII characters.
(i) If <char> is a lower case letter, the message
“The upper case character corresponding to <char> is …”
(ii) If <char> is an upper case letter , the message
“The lower case character corresponding to <char> is …”
(iii) If <char> is not a letter, the message <char> is not a letter”.
Code:
#include <iostream>
using namespace std;
int main(){char ch,ch1,ch2;
cout<<"Enter any single character <char>="<<endl;
cin>>ch;
if(ch<=122&&ch>=97)
{ch1=ch-32;cout<<"The upper case letter corresponding to <char> is"<<"
"<<ch1;}
else if(ch<=90&&ch>=65)
{ch2=ch+32;cout<<"The lower case letter corresponding to <char> is"<<"
"<<ch2;}
else cout<<"<char>is not a letter";
return 0;
}
Output:
Uilyhi
Uilyhi

Q6.Write a program to input three integers and print the largest of three.
Code:
#include <iostream>
using namespace std;
int main(){int i1,i2,i3;
cout<<"Enter any three integers"<<endl;
cin>>i1>>i2>>i3;
if(i1>i2&&i1>i3)
cout<<"greatest integer is"<<" "<<i1;
else if(i2>i1&&i2>i3)
cout<<"greatest integer is"<<" "<<i2;
else cout<<"greatest integer is"<<" "<<i3;
return 0;
}
Output:
Uilyhi

Q7. Write a C++ program to input a number. If the number n is odd and positive,
print its square root otherwise print n^3.
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main(){int n,r;
cout<<"enter any integer n"<<endl;
cin>>n;r=n%2;
if(r!=0&&n>=0)
cout<<"Given no. is odd,Sqrt(n)="<<" "<<pow(n,0.5);
else cout<<"Given no. is not odd,n^3="<<" "<<n*n*n;
return 0;
}
Output:
Uilyhi

Q8. Write a C++ program to create the equivalent of a four –function calculator.
The program requires the user to enter two numbers and an operator. It then carries
out the specified arithmetical operation: addition
Code:
#include<iostream>
using namespace std;
int main() {int n1,n2,sum,sub,mult;
float div;char opt;
do{
cout<<"Press + for addtion"<<endl<<"Press - for substraction"<<endl<<"Press *
for Multiplication"<<endl<<"Press / for division"<<endl<<"Press e to turn off the
calculator"<<" ";
cin>>opt;
switch(opt)
{case'+':
{cout<<"Enter any two integers"<<" "<<endl;
cin>>n1>>n2;
sum=n1+n2;
cout<<"Sum="<<" "<<sum<<endl<<endl;
break;}
case'-':
{cout<<"Enter any two integers"<<" "<<endl;
cin>>n1>>n2;
sub=n1-n2;
cout<<"Substraction="<<" "<<sub<<endl<<endl;
break;}
case'*':
Uilyhi

{cout<<"Enter any two integers"<<" "<<endl;


cin>>n1>>n2;
mult=n1*n2;
cout<<"Multiplication="<<" "<<mult<<endl<<endl;
break;}
case'/':
{cout<<"Enter any two integers"<<" "<<endl;
cin>>n1>>n2;
div=(float)n1/n2;
cout<<"Quotient="<<" "<<div<<endl<<endl;
break;}
case'e':
{ cout<<"Exit";
break;}
default:
{cout<<"Invalid operator";
break;
}
}
}while(opt!='e');
}
Output:
Uilyhi
Uilyhi

Q9. Program to input a character and to print whether a given character is an


alphabet, digit or any other character
Code:
#include <iostream>
using namespace std;
int main(){char ch;
cout<<"Enter any character"<<" ";
cin>>ch;
if(ch>='a'&&ch<='z')
cout<<"the input is an lower case alphabet";
else if(ch>='A'&&ch<='Z')
cout<<"the input is an upper case alphabet";
else if(ch>='0'&&ch<='9')
cout<<"The input is a digit";
else cout<<"The input is any other character than a digit or alphabet";
return 0;
}
Output:
Uilyhi

Q.10 Program to calculate commission for the salesmen. The commission is


calculated according to following rates
Sales Commission rate
30001 onwards 15 %
22001-30000 10 %
12001-22000 7%
5001-12000 3%
0-5000 0%
Code:
#include <iostream>
using namespace std;
int main(){float s,comm;
cout<<"Enter sales made by Salesman"<<"\n";
cin>>s;
if(s>0&&s<5000)
{comm=0*s;
cout<<"Commission = "<<comm;}
else if(s>5001&&s<12000)
{comm=0.03*s;
cout<<"Commission = "<<comm;}
else if(s>12001&&s<22000)
{comm=0.07*s;
cout<<"Commission = "<<comm;}
else if(s>22001&&s<30000)
{comm=0.1*s;
cout<<"Commission = "<<comm;}
Uilyhi

else {comm=0.15*s;cout<<"Commission = "<<comm;}


return 0;
}
Output:
Uilyhi

You might also like