You are on page 1of 28

GOVERNMENT MUNICIPEL DEGREE COLLEGE

FAISALABAD

ASSIGNMENT
BSCS-1ST SEMESTER 2018
TITLE:
FUNDAMENTALS OF PROGRAMMING
SUBMIT TO:
SIR MUHAMMAD FAIYAZ
SUBMIT BY:
NAME:
ROLL NO:
DATE:
Problem # 1:

Writing a program that accepts a character and determines


whether the character is a lowercase letter. A lowercase letter is
any character that is greater than equal to 'a' and less than or
equal to 'z'. If the entered character is a lowercase letter, display
the message "Entered character is a lowercase letter", otherwise
display the message "Entered character is not a lowercase
letter".

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:

Senior salesperson is paid Rs. 400 a week, and a junior


salesperson is paid Rs. 275 a week. Write a program that accepts
as input a salesperson's status in the character variable status. If
status is 's' or 'S', the senior person's salary should be displayed;
if status is 'j' or 'J', the junior person's salary should be displayed,
otherwise display error message.

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:

Write a program to get three numbers from user for integer


variables a, b and c. If a is not zero, find out whether it is the
common divisor of b and c.

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:

Write a program that contains an if statement that may be used


to compute the area of a square (area = side * side) or a triangle
(area = 1/2 * base * height) after prompting the user to type the
first character of the figure names (S or T).

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:

Write a program that accepts the code number as an input and


display the correct disk drive manufacture as follows:
Code        disk drive manufacture
1            Western Digital
2            3M Corporation
3            Maxell Corporation
4            Sony Corporation
5            Verbatim Corporation

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:

Write a program that accepts the code number as an input and


display the correct disk drive manufacture as follows:
Code        disk drive manufacture
1            Western Digital
2            3M Corporation
3            Maxell Corporation
4            Sony Corporation
5            Verbatim Corporation

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:

Write a program that uses the following categories of movies:


A for Adventure movies
C for Comedy movies
F for Family movies
H for Horror movies
S for Science Fiction movies
The program inputs code for movie type and display its category.
For examples if the user enters H, it displays "Horror Movies".
The program should also display a menu of movie categories.

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:

Write a program that inputs a value and type of conversion. The


program should then output the value after conversion. The
program should include the following conversions:
1 inch = 2.54 centimeters
1 gallon = 3.785 liters
1 mile = 1.609 kilometers
1 pound = 0.4536 kilograms
Make sure that program accepts only valid choices for type of
conversion to perform.

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:

A year is a leap year if it is divisible by four, except that any year


divisible by 100 is a leap year only if it is divisible by 400. Write a
program that inputs a year such as 1996, 1800 and 2010 and
display "Leap year" if it is a leap year otherwise displays "Not a
leap year".
Solution:

#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:

Write a program that inputs temperature and displays a message


as follows:
    Temperature                                Message
    Greater than 35                           Hot day
    Between 25 and 35 (inclusive)    Pleasant day
    Less than 25                                 Cool day

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:

Write a program that inputs obtained marks of a student,


calculates the percentage (assuming total marks are 1100) and
displays grade according to the following rules:
    Percentage                                   Grade
    More than or equal to 80            A+
    Between 70 (inclusive) and 80    A
    Between 60 (inclusive) and 70    B
    Between 50 (inclusive) and 60    C
    Between 40 (inclusive) and 50    D
    Between 33 (inclusive) and 40    E
    Less than 33                                    F

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:

Write a program that converts MILITARY time to STANDARD time.

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:

Write a program that inputs the salary of an employee from the


user. It deducts the income tax from the salary on the following
basis:
20% income tax if the salary is above Rs. 30000.
15% income tax if the salary is between Rs.20000 and Rs.30000.
10% income tax if the salary is below Rs.20000.
The program finally displays salary, income tax and the net
salary.

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:

Write a program that inputs year and month. It displays the


number of days in the month of the year entered by the user. For
example, if the user enters 2010 in year and 3 in month, the
program should display "March 2010 has 31 days".

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:

Write a program that displays the following menu for a parking


area:
M    =Motorcycle
C    =Car
B    =Bus
The program inputs the type of vehicle and number of days to
park the vehicle. It 
finally displays the total charges for the parking according to the
following:
Motorcycle    Rs. 10 per day
Car                 Rs. 20 per day
Bus                 Rs. 30 per day

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:

Write a program that inputs a value and type of conversion. The


program should 
then displays the output after conversion. The program should
include the following 
conversions:
1 cm = .394 inches
1 liter = .264 gallons
1 kilometer = .622 miles
1 kilogram = 2.2 pounds

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;
}

You might also like