Hello Everyone ! This time ,as i mentioned last time, we will discuss some examples related to what we have done.
1) W.A.P.(Write a Program) to enter 3 numbers and display the highest of them?
Solution:
#include<iostream>
using namespace std;
int main()
{
int a,b,c,max;
cout <<"Enter 3 numbers:";
cin>>a>>b>>c;
if(a>b && a>c)
max=a;
else {
if(b>c)
max=b;
else
max=c;
}
cout<<"\nMaximum = "<< max;
return 0;
}
2) W.A.P. to check whether a date entered in dd/mm/yy format is Valid or not?
#include<iostream>
using namespace std;
int main()
{
int d,m,y,maxdays;
char v='N';
cout<<"Enter a date as dd/mm/yy=";
cin>>d>>m>>y;
if(m>=1 && m<=12)
{ if (m==1 || m==3 || m==5 || m==7 || m==8|| m==10|| m==12)
maxdays=31;
else
{ if(m==2)
{
if((y%4==0 && y%100!=0 ) || (y%400==0))
{
maxdays=29;
}
else
maxdays=28;
}
else
maxdays=30;}
if(d>=1 && d<=maxdays)
valid='Y';
}
if(v == 'Y')
cout<<"\n Valid Date.";
else
cout<<"\n Invalid Date.";
return 0;
}
Both of these Questions could be done in many different ways like using switch case or using a Ternery Operator. Try them yourself and try different ways to do these programs and ask your questions in the comments below. Please Subscribe to my blog by clicking the link next to Subscribe to, to get Regular Updates.
Here are some Homework Questions for you to try along with these two questions :-
1) W.A.P. to display roots of a quadratic equation where coefficients a,b,c are given ?
2) W.A.P. to check whether a given number is prime or not ?
3) W.A.P. to display all the factors of a given number along with the sum of the factors?
Try them and ask your questions in the comments. I Will give you Solutions next time.Till then.
Be Happy and Keep Coding.
1) W.A.P.(Write a Program) to enter 3 numbers and display the highest of them?
Solution:
#include<iostream>
using namespace std;
int main()
{
int a,b,c,max;
cout <<"Enter 3 numbers:";
cin>>a>>b>>c;
if(a>b && a>c)
max=a;
else {
if(b>c)
max=b;
else
max=c;
}
cout<<"\nMaximum = "<< max;
return 0;
}
2) W.A.P. to check whether a date entered in dd/mm/yy format is Valid or not?
#include<iostream>
using namespace std;
int main()
{
int d,m,y,maxdays;
char v='N';
cout<<"Enter a date as dd/mm/yy=";
cin>>d>>m>>y;
if(m>=1 && m<=12)
{ if (m==1 || m==3 || m==5 || m==7 || m==8|| m==10|| m==12)
maxdays=31;
else
{ if(m==2)
{
if((y%4==0 && y%100!=0 ) || (y%400==0))
{
maxdays=29;
}
else
maxdays=28;
}
else
maxdays=30;}
if(d>=1 && d<=maxdays)
valid='Y';
}
if(v == 'Y')
cout<<"\n Valid Date.";
else
cout<<"\n Invalid Date.";
return 0;
}
Both of these Questions could be done in many different ways like using switch case or using a Ternery Operator. Try them yourself and try different ways to do these programs and ask your questions in the comments below. Please Subscribe to my blog by clicking the link next to Subscribe to, to get Regular Updates.
Here are some Homework Questions for you to try along with these two questions :-
1) W.A.P. to display roots of a quadratic equation where coefficients a,b,c are given ?
2) W.A.P. to check whether a given number is prime or not ?
3) W.A.P. to display all the factors of a given number along with the sum of the factors?
Try them and ask your questions in the comments. I Will give you Solutions next time.Till then.
Be Happy and Keep Coding.
Comments
Post a Comment