Professional Documents
Culture Documents
FAISALABAD
ASSIGNMENT
BSCS-1ST SEMESTER 2018
TITLE:
FUNDAMENTALS OF PROGRAMMING
SUBMIT TO:
SIR MUHAMMAD FAIYAZ
SUBMIT BY:
NAME:
ROLL NO:
DATE:
Problem # 1:
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
char c;
cout<<"enter a character:";
cin>>c;
if(c>='a'&&c<='z')
cout<<"\nEntered character is a lower case letter";
else
cout<<"\nEntered character is not a lower case letter";
return 0;
}
Problem # 2:
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
char status;
cout<<"Enter your status\nEnter \'s\' or \'S\' for
senior person\'s salary\nEnter \'j\' or \'J\' for junior
person\'s salary."<<endl;
cin>>status;
switch (status)
{
case 's':
case 'S':
cout<<endl<<"Your salary is Rs.400 per week.";
break;
case'j':
case 'J':
cout<<endl<<"Your salary is Rs.275 per week.";
break;
default:
cout<<endl<<"Invalid input.";
}
return 0;
}
Problem # 3:
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
int a,b,c;
cout<<"Enter three integer numbers:";
cin>>a>>b>>c;
if(a!=0)
{
if(b%a==0&&c%a==0)
cout<<endl<<"a:"<<a<<" is a common divisor of
b:"<<b<<"and c:"<<c;
else
cout<<endl<<"a:"<<a<<" is not a common divisor of
b:"<<b<<"and c:"<<c;
}
else
cout<<"a is zero.";
return 0;
}
Problem # 4:
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
char c;
float area, side, base, height;
cout<<"Enter the first chracter of figure name:\nEnter S
for square\nEnter T for triangle.";
cin>>c;
if(c=='S'||c=='s')
{
side=46;
area=side*side;
cout<<endl<<"Area of square is:"<<area;
}
else if(c=='T'||c=='t')
{
base=69;
height=58;
area=0.5*base*height;
cout<<endl<<"Area of triangle is:"<<area;
}
else
cout<<endl<<"Invalid input";
return 0;
}
Problem # 5:
Write a program that gets the number and a letter. If the letter is
'f', the program should treat the number entered as temperature
in degrees Fahrenheit and convert it to the temperature in degree
Celsius and print a suitable message. If the letter is 'c', the
program should consider the number as Celsius temperature and
convert it to Fahrenheit temperature and print a suitable
message. The program should display error message and then
exit if the user enters any other letter.
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
float n,C,F;
char c;
cout<<"Enter a number:";
cin>>n;
cout<<"\nEnter a letter:";
cin>>c;
if(c=='f')
{
C=(n-32)*5/9;
cout<<"\nthe temperature after converting into
degree celsius is:"<<C;
}
else if(c=='c')
{
F=9/5*n+32;
cout<<"\nThe temperature after converting into
degree fahrenheit is:"<<F;
}
else
cout<<"\nerror.";
return 0;
}
Problem # 6:
Solution:
#include<iostream.h>
#include<conio.h>
using namespace std;
int main ()
{
int cd;
cout<<"Enter code for disk drive manufacturer:";
cin>>cd;
switch(cd)
{
case 1:
cout<<"\nThe disk drive manufacturer is Western
Digital.";
break;
case 2:
cout<<"\nThe disk drive manufacturer is 3M
Corporation.";
break;
case 3:
cout<<"\nThe disk drive manufacturer is Maxell
Corporation.";
break;
case 4:
cout<<"\nThe disk drive manufacturer is Sony
Corporation.";
break;
case 5:
cout<<"\nThe disk drive manufacturer is Verbatim
Corporation.";
break;
default:
cout<<"Invalid input.";
}
return 0;
}
Problem # 6:
Solution:
#include<iostream.h>
#include<conio.h>
using namespace std;
int main ()
{
int cd;
cout<<"Enter code for disk drive manufacturer:";
cin>>cd;
switch(cd)
{
case 1:
cout<<"\nThe disk drive manufacturer is Western
Digital.";
break;
case 2:
cout<<"\nThe disk drive manufacturer is 3M
Corporation.";
break;
case 3:
cout<<"\nThe disk drive manufacturer is Maxell
Corporation.";
break;
case 4:
cout<<"\nThe disk drive manufacturer is Sony
Corporation.";
break;
case 5:
cout<<"\nThe disk drive manufacturer is Verbatim
Corporation.";
break;
default:
cout<<"Invalid input.";
}
return 0;
}
Problem # 7:
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
char c;
cout<<"Menu for movie categories:\nA for Adventure
movies\nC for Comedy movies\nF for Family movies\nH for
Horror movies\nS for Science fiction movies";
cout<<"\nEnter your code:";
cin>>c;
switch(c)
{
case 'A':
cout<<"\nAdventure movies.";
break;
case'C':
cout<<"\nComedy movies.";
break;
case 'F':
cout<<"\nFamily movies.";
break;
case 'H':
cout<<"\nHorror movies.";
break;
case 'S':
cout<<"\nScience fiction movies.";
break;
default:
cout<<"\nInvalid input.";
}
return 0;
}
Problem # 8:
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int c;
float res, n;
cout<<"Enter the type of conversions:\n1. To convert
into centimeters.\n2. To convert into liters.\n3. To convert
into kilometers.\n4. To convert into kilograms.";
cin>>c;
switch(c)
{
case 1:
cout<<"\nEnter value in inches:";
cin>>n;
res=2.54*n;
break;
case 2:
cout<<"\nEnter value in gallon:";
cin>>n;
res=3.785*n;
break;
case 3:
cout<<"\nEnter the value in mile:";
cin>>n;
res=1.609*n;
break;
case 4:
cout<<"Enter the value in pound:";
cin>>n;
res=0.4536*n;
break;
default:
cout<<"Invalid input.";
}
cout<<"\nThe value after conversion is:"<<res;
return 0;
}
Problem # 9:
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
int year;
cout<<"Enter a year:";
cin>>year;
if(year%4==0 && year%100!=0)
cout<<"It is a leap year.";
else if(year%100==0 && year%400==0)
cout<<"It is a leap year.";
else
cout<<"It is not a leap year.";
return 0;
}
Problem # 10:
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
float T;
cout<<"Enter temperature:";
cin>>T;
if(T>35)
cout<<"Hot day.";
else if(T>=25)
cout<<"Pleasant day.";
else
cout<<"Cool day.";
return 0;
}
Problem # 11:
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
int marks;
float percentage;
cout<<"Enter marks:";
cin>>marks;
percentage = (marks/1100.0)*100.0;
if(marks>=0&&marks<=1100)
{
if(percentage>=80)
cout<<"Grade is A+.";
else if(percentage>=70)
cout<<"Grade is A.";
else if(percentage>=60)
cout<<"Grade is B.";
else if(percentage>=50)
cout<<"Grade is C.";
else if(percentage>=40)
cout<<"Grade is E.";
else
cout<<"Grade is F.";
}
else
cout<<"Invalid input.";
return 0;
}
Problem # 12:
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int t,h,m,H;
cout<<"Enter Military time=";
cin>>t;
h=t/100;
m=t%100;
if((h>=0&&h<=23)&&(m>=0&&m<=59))
{
if(h>12)
{
H=h%12;
cout<<"The standard time
of"<<h<<":"<<m<<"is="<<H<<":"<<m<<"PM";
}
else if(h==0)
{
H=12;
cout<<"The standard time
of"<<h<<":"<<m<<"is="<<H<<":"<<m<<"AM";
}
else
{
H=h;
cout<<"The standard time
of"<<h<<":"<<m<<"is="<<H<<":"<<m<<"AM";
}
}
else
cout<<"wrong input.";
return 0;
}
Problem # 13:
Write a program that will take three values a, b and c and print
the roots, if real, of the quadratic equation ax^2 + bx + c = 0.
Sample input 1: (a=1, b=1, c=-6 the output should be "Roots of the
equation and 2 and -3"). Sample input 2 (if the input is a=1, b=0,
c=9, the output should be "Sorry the roots are not real.")
Solution:
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main ()
{
float a,b,c;
double x,y;
cout<<"Enter three values:";
cin>>a>>b>>c;
if(b*b-4*a*c>=0)
{
x=(-b+sqrt(b*b-4*a*c))/2*a;
y=(-b-sqrt(b*b-4*a*c))/2*a;
cout<<"\nRoots of the equation are "<<x<<" and "<<y;
}
else
cout<<"\nSorry the roots are not real";
return 0;
}
Problem # 14:
Solution:
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main ()
{
int sal;
float tax, netsal;
cout<<"Enter salary";
cin>>sal;
if(sal>30000)
{
tax=(sal*0.02);
netsal=sal-tax;
}
else if(sal>=20000)
{
tax=sal*0.15;
netsal=sal-tax;
}
else
{
tax=sal*0.01;
netsal=sal-tax;
}
cout<<"The salary is:"<<sal<<"\nThe income tax
is:"<<tax<<"\nThe net salary after detucting is:"<<netsal;
return 0;
}
Problem # 15:
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int y,m,month,days;
cout<<"Enter a year and month";
cin>>y>>m;
if(m==1)
cout<<"January "<<y<<" has 31 days.";
else if(m==2)
{
if(y%4!=0)
cout<<"February "<<y<<" has 28 days.";
else
cout<<"February "<<y<<" has 29 days.";
}
else if(m==3)
cout<<"March "<<y<<" has 31 days.";
else if(m==4)
cout<<"April "<<y<<" has 30 days.";
else if(m==5)
cout<<"May "<<y<<" has 31 days.";
else if(m==6)
cout<<"June "<<y<<" has 30 days.";
else if(m==7)
cout<<"July "<<y<<" has 31 days.";
else if(m==8)
cout<<"August "<<y<<" has 31 days.";
else if(m==9)
cout<<"September "<<y<<" has 30 days.";
else if(m==10)
cout<<"October "<<y<<" has 31 days.";
else if(m==11)
cout<<"November "<<y<<" has 30 days.";
else if(m==12)
cout<<"December "<<y<<" has 31 days.";
else
cout<<"The valid input for month is from 1 to 12.";
return 0;
}
Problem # 16:
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char V,x;
int charges;
cout<<"Select your
vehicle:\nM\t=Motorcycle\nC\t=Car\nB\t=Bus";
cin>>V;
cout<<"Enter days to park the vehicle : ";
cin>>x;
switch(V)
{
case 'M':
{
charges=x*10;
cout<<"Charges for Motorcycle is Rs."<<charges ;
break;
}
case 'C':
{
charges=x*20;
cout<<"Charges for Car is Rs."<<charges;
break;
}
case 'B':
{
charges=x*30;
cout<<"Charges for Bus is Rs."<<charges;
break;
}
default:
cout<<"Invalid input";
}
return 0;
}
Problem # 17:
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char V,x;
int charges;
cout<<"Select your
vehicle:\nM\t=Motorcycle\nC\t=Car\nB\t=Bus";
cin>>V;
cout<<"Enter days to park the vehicle : ";
cin>>x;
switch(V)
{
case 'M':
{
charges=x*10;
cout<<"Charges for Motorcycle is Rs."<<charges ;
break;
}
case 'C':
{
charges=x*20;
cout<<"Charges for Car is Rs."<<charges;
break;
}
case 'B':
{
charges=x*30;
cout<<"Charges for Bus is Rs."<<charges;
break;
}
default:
cout<<"Invalid input";
}
return 0;
}