0% found this document useful (0 votes)
23 views17 pages

C++ Practical 4.7

Uploaded by

huzaifakadri95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views17 pages

C++ Practical 4.7

Uploaded by

huzaifakadri95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

FYCS02 C++ Programming

FYCS76
Practical-2
1. WAP to accept a number from the user and print if it is positive, negative or zero.
#include<iostream.h>
#include<conio.h>
void main(){
int n;
clrscr();
cout<<"Enter a number"<<endl;
cin>>n;
if(n>0){
cout<<"The number is positive!"<<endl;
}else{
if(n<0){
cout<<"The number is negative!"<<endl;
}
else{
cout<<"The number is zero!"<<endl;
}
}getch();
}
Output:

2. WAP to accept the year from the user. Check if the year is a leap year or not and display
appropriate message.[Hint: if((inputYear%4 == 0 && inputYear % 100 != 0) || (inputYear % 400
== 0)) – if condition is true it’s a leap year, else not]

#include<iostream.h>
#include<conio.h>
void main(){
int y;
clrscr();
cout<<"Enter a year:"<<endl;
cin>>y;
if((y%4==0&&y%100!=0)||(y%400==0)){
cout<<y<<" is a leap year."<<endl;
FYCS02
FYCS76 C++ Programming

}else{
cout<<y<<" is not a leap year."<<endl;
}
getch();
}
Output:

3. WAP to accept two integers. Consider them as coordinates to a point in the Cartesian system
and display the quadrant in which the point will lie. [Hint: I (+,+) ; II ( -,+) ; III (-,-) ; IV(+,-)]
#include<iostream.h>
#include<conio.h>
void main(){
int x,y;
clrscr();
cout<<"Enter the x-coordinate:"<<endl;
cin>>x;
cout<<"Enter the y-coordinate:"<<endl;
cin>>y;
if (x>0&&y>0){
cout<<"The coordinates lies in the first quadrant."
<<endl;
}else
if (x<0&&y>0){
cout<<"The coordinates lies in the second quadrant."
<<endl;
}else
if (x<0&&y<0){
cout<<"The coordinates lies in the third quadrant."
<<endl;
}else
if (x>0&&y<0){
cout<<"The coordinates lies in the fourth quadrant."
<<endl;
}else
if (x==0&&y!=0){
cout<<"The coordinates lies in the y-axis."<<endl;
}else
FYCS76
FYCS02 C++ Programming

if (x!=0&&y==0){
cout<<"The coordinates lies in the x-axis."<<endl;
}else{
cout<<"The coordinates is at the origin."<<endl;
}
getch();
}
Output:
FYCS76
FYCS02 C++ Programming

4. WAP to accept three numbers and print the largest of them.


#include<iostream.h>
#include<conio.h>
void main(){
int n1,n2,n3;
clrscr();
cout<<"Enter the first number:"<<endl;
cin>>n1;
cout<<"Enter the second number:"<<endl;
cin>>n2;
cout<<"Enter the third number:"<<endl;
cin>>n3;
int largest=n1;
if(n2>largest){
largest=n2;
}
if(n3>largest){
largest=n3;
}
cout<<"The largest number is:"<<largest<<endl;
getch();
}
Output:

5. WAP to accept a number and check if it is odd or even. Display appropriate messages.
#include<iostream.h>
#include<conio.h>
void main(){
int n;
clrscr();
cout<<"Enter a number:"<<endl;
cin>>n;
if(n%2==0){
cout<<"The number "<<n<<" is even"<<endl;
}else{
cout<<"The number "<<n<<" is odd"<<endl;
}
getch();
FYCS76
FYCS02 C++ Programming

}
Output:

6. WAP to accept CP and SP from the user. Calculate Profit and Profit% OR Loss and Loss%
depending on the situation. [Hint: Profit = SP – CP, Profit% = (Profit/CP)*100, Loss = CP –SP,
Loss% = (Loss/CP)*100]
#include<iostream.h>
#include<conio.h>
void main(){
int sp,cp,p,l,pp,lp;
clrscr();
cout<<"Enter the Selling Price and Cost Price: "<<endl;
cin>>sp>>cp;
if(sp>cp){
p=sp-cp;
pp=(p/cp)*100;
cout<<"Profit:"<<p<<endl;
cout<<"Profit Percentage:"<<pp<<endl;
}else
if(cp>sp){
l=cp-sp;
lp=(l/sp)*100;
cout<<"Loss:"<<l<<endl;
cout<<"Loss Percentage:"<<lp<<endl;
}else{
cout<<"No Profit,No Loss!"<<endl;
}
getch();
}
Output:
FYCS76
FYCS02 C++ Programming

7. WAP to accept three numbers and check if they are a Pythagorean triplet or not. Print
appropriate messages [Hint: For 3 numbers to be a Pythagorean triplet, the square of the largest
number should be equal to the sum of the squares of the other two numbers]
#include<iostream.h>
#include<conio.h>
void main(){
int a,b,c;
clrscr();
cout<<"Enter the three sides of triangle:"<<endl;
cin>>a>>b>>c;
int maxnum=a;
if(b>maxnum){
maxnum=b;
}
if(c>maxnum){
maxnum=c;
}
if(maxnum==a){
if(a*a==b*b+c*c)
cout<<"The numbers form a pythagorean triplet."<<endl;
else
cout<<"The numbers do not form a pythagorean triplet."<<endl;
}else
if(maxnum==b){
if(b*b==c*c+a*a)
cout<<"The numbers form pythagorean triplet."<<endl;
else
cout<<"The numbers do not form a pythagorean triplet."<<endl;
}else
if(maxnum==c){
if(c*c==a*a+b*b)
cout<<"The numbers form pythagorean triplet."<<endl;
else
cout<<"The numbers do not form a pythagorean triplet."<<endl;
}else{
cout<<"The numbers do not form a pythagorean triplet."<<endl;
}
getch();
FYCS76
FYCS02 C++ Programming

}
Output:

8. WAP to accept the three sides of a triangle and check if the triangle is possible [Hint: In a
triangle the sum of two sides should be greater than the third side.]
#include<iostream.h>
#include<conio.h>
void main(){
float s1,s2,s3;
clrscr();
cout<<"Enter the three sides of triangle:"<<endl;
cin>>s1>>s2>>s3;
if((s1+s2>s3)&&(s1+s3>s2)&&(s2+s3>s1)){
cout<<"The triangle is possible!"<<endl;
}else{
cout<<"The triangle is not possible!"<<endl;
}getch();
}
Output:

9. WAP to accept the three angles of a triangle and check if the triangle is possible [Hint: In a
triangle the sum of three angles should be equal to 180.]
#include<iostream.h>
#include<conio.h>
void main(){
int a1,a2,a3;
clrscr();
cout<<"Enter the three angles of the triangle:"<<endl;
cin>>a1>>a2>>a3;
if(a1+a2+a3==180){
cout<<"The triangle is possible!"<<endl;
}else{
FYCS76
FYCS02 C++ Programming

cout<<"The triangle is not possible!"<<endl;


}getch();
}
Output:

10. WAP to accept 2 numbers and an arithmetic operator. Perform the operation on the numbers
accepted and display the result.
#include<iostream.h>
#include<conio.h>
void main(){
float n1,n2,r;
char op;
clrscr();
cout<<"Enter the first number:"<<endl;
cin>>n1;
cout<<"Enter the second number:"<<endl;
cin>>n2;
cout<<"Enter an arithmetic operator(+,-,*,/):"<<endl;
cin>>op;
switch(op){
case '+':
r=n1+n2;
cout<<"Result: "<<r<<endl;
break;
case '-':
r=n1-n2;
cout<<"Result: "<<r<<endl;
break;
case '*':
r=n1*n2;
cout<<"Result: "<<r<<endl;
break;
case '/':
if(n2!=0){
r=n1/n2;
cout<<"Result: "<<r<<endl;
}else{
cout<<"Error: Division by zero is not allowed."<<endl;
FYCS76FYCS02 C++ Programming

}
break;
default:
cout<<"Error: Invalid operator entered."<<endl;
break;
}getch():
}
Output:

11. WAP to accept a letter and display whether it is in upper or lower case. Display an error in
case it is not a valid letter.
#include<iostream.h>
#include<conio.h>
void main(){
char l;
clrscr();
cout<<"Enter a letter:"<<endl;
cin>>l;
if(l>='A'&&l<='Z'){
FYCS76
FYCS02 C++ Programming

cout<<l<<" is an uppercase letter!"<<endl;


}else
if(l>='a'&&l<='z'){
cout<<l<<" is a lowercase letter!"<<endl;
}else{
cout<<"Error:It is not a valid letter"<<endl;
}
getch();
}
Output:

12. WAP to calculate the roots of a quadratic equation: ax2+bx+c=0 [Hint: Accept a, b and c
from the user]
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main(){
float a,b,c,d,r1,r2;
clrscr();
cout<<"Enter coefficients a,b,c:"<<endl;
cin>>a>>b>>c;
d=b*b-4*a*c;
if(d>0){
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
cout<<"Roots are real: "<<r1<<" and "<<r2<<endl;
}else
if(d==0){
r1=-b/(2*a);
cout<<"Root is: "<<r1<<endl;
}else{
cout<<"Roots are complex: "<<-b/(2*a)<<"+"<<
sqrt(-d)/(2*a)<<"i and ";
cout<<-b/(2*a)<<"-"<<sqrt(-d)/(2*a)<<"i";
}
getch();
FYCS76
FYCS02 C++ Programming

}
Output:

13. WAP to calculate and print the Mobile bill of a given user. The subscriber id., name, mobile
number and minutes used for calling by the subscriber should be taken from the keyboard and
display the total amount to pay to the subscriber. Use the following table for calculation:

If bill exceeds Rs. 400 then a surcharge of 15% will be charged and the minimum bill should be
of Rs. 100/-(i.e. if the bill calculates to less than 100/- then print Rs. 100/- as payable amount)
#include<iostream.h>
#include<conio.h>
void main(){
int sub_id,min;
char name[50],mobile[10];
float bill;
clrscr();
cout<<"Enter the Subscriber Id: "<<endl;
cin>>sub_id;
cout<<"Enter the Mobile number: "<<endl;
cin>>mobile;
cout<<"Enter the name: "<<endl;
cin>>name;
cout<<"Enter the minute used: "<<endl;
cin>>min;
if(min<200){
FYCS76 FYCS02 C++ Programming

bill=min*1.00;
}else
if(min>=200&&min<400){
bill=min*1.50;
}else
if(min>=400&&min<600){
bill=min*1.80;
}else{
bill=min*2.00;
}
if(bill>400){
bill+=bill*0.15;
}if(bill<100){
bill=100;
}
cout<<"The total bill is: "<<bill<<endl;
getch();
}
Output:
FYCS76FYCS02 C++ Programming

14. WAP to accept a number from the user and display the day of the week. [Eg if user enters 1,
you print Sunday.]
#include<iostream.h>
#include<conio.h>
void main(){
int n;
clrscr();
cout<<"Enter a number(1-7): "<<endl;
cin>>n;
switch (n){
case 1:
cout<<"Sunday"<<endl;
break;
case 2:
cout<<"Monday"<<endl;
break;
case 3:
cout<<"Tuesday"<<endl;
break;
case 4:
cout<<"Wednesday"<<endl;
break;
case 5:
cout<<"Thursday"<<endl;
break;
case 6:
cout<<"Friday"<<endl;
break;
case 7:
cout<<"Saturday"<<endl;
break;
default:
cout<<"Invalid Input"<<endl;
}
getch();
}
Output:
FYCS76 FYCS02 C++ Programming

15. WAP to accept a number from the user and display the month of the year. [Eg if user enters 1,
you print January.]
#include<iostream.h>
#include<conio.h>
void main(){
int n;
clrscr();
cout<<"Enter a number(1-12): "<<endl;
cin>>n;
switch (n){
case 1:
cout<<"January"<<endl;
break;
case 2:
cout<<"February"<<endl;
break;
case 3:
cout<<"March"<<endl;
break;
case 4:
cout<<"April"<<endl;
break;
case 5:
cout<<"May"<<endl;
break;
case 6:
cout<<"June"<<endl;
break;
case 7:
cout<<"July"<<endl;
break;
case 8:
cout<<"August"<<endl;
break;
case 9:
cout<<"September"<<endl;
break;
case 10:
FYCS76
FYCS02 C++ Programming

cout<<"October"<<endl;
break;
case 11:
cout<<"November"<<endl;
break;
case 12:
cout<<"December"<<endl;
break;
default:
cout<<"Invalid Input"<<endl;
}
getch();
}
Output:

16. WAP to accept a number from user and print its Roman Number equivalent. Assume numbers
from 1-20
#include<iostream.h>
#include<conio.h>
void main(){
int n;
clrscr();
cout<<"Enter a number(1-20): "<<endl;
cin>>n;
switch (n){
case 1:
cout<<"I"<<endl;
break;
case 2:
cout<<"II"<<endl;
break;
case 3:
cout<<"III"<<endl;
break;
case 4:
cout<<"IV"<<endl;
break;
case 5:
cout<<"V"<<endl;
FYCS76
FYCS02 C++ Programming

break;
case 6:
cout<<"VI"<<endl;
break;
case 7:
cout<<"VII"<<endl;
break;
case 8:
cout<<"VIII"<<endl;
break;
case 9:
cout<<"IX"<<endl;
break;
case 10:
cout<<"X"<<endl;
break;
case 11:
cout<<"XI"<<endl;
break;
case 12:
cout<<"XII"<<endl;
break;
case 13:
cout<<"XIII"<<endl;
break;
case 14:
cout<<"XIV"<<endl;
break;
case 15:
cout<<"XV"<<endl;
break;
case 16:
cout<<"XVI"<<endl;
break;
case 17:
cout<<"XVII"<<endl;
break;
case 18:
cout<<"XVIII"<<endl;
break;
case 19:
cout<<"XIX"<<endl;
break;
FYCS76
FYCS02 C++ Programming

case 20:
cout<<"XX"<<endl;
break;
default:
cout<<"Invalid Input"<<endl;
}
getch();
}
Output:

You might also like