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

Introduction to Computing

SUBMITTED BY: REGISTRATION #: SECTION: D SUBMITTED TO: SIR AZEEM AHMAD L1S13BSCS0145 L1S13BSCS0152 L1S13BSCS2169 L1S13BSCS2142 L1S13BSCS0170 MUHAMMAD KHIDR ZARA MALIK AQSA SHAHID AMNA ZAHEER SAMIA ZAKIR

WEEK 6 LAB 1:
Program 1: State the order of evaluation of the operators in each of the following C++ statements and find the value of variable, after each statement is performed. Write C++ program to check your answers. a = 12 / 3 * 3 b = 10 % 3 6 / 2 c= 5.0 * 2.0 / 4.0 * 2.0 d = 5.0 * 2.0 / (4.0 * 2.0) e= 5.0 + 2.0 / (4.0 * 2.0) f = ( 3 * 9 * ( 3 + ( 9 * 3 / (3) ) ) ); g = 2 * 3 / 4 + 4 / 4 + 8 2 + 5 / 8; #include <iostream.h> void main () { int a,b,f,g; float c,d,e; a=12/3*3; cout<<"value of a = "<<a<<endl; b=10%3-6/2; cout<<"value of b = "<<b<<endl; c=5.0*2.0/4.0*2.0; cout<<"value of c = "<<c<<endl; d=5.0*2.0/(4.0*2.0);

cout<<"value of d = "<<d<<endl; e=5.0+2.0/(4.0*2.0); cout<<"value of e = "<<e<<endl; f=(3*9*(3+(9*3/(3)))); cout<<"value of f = "<<f<<endl; g=2*3/4+4/4+8-2+5/8; cout<<"value of g = "<<g<<endl;

Program 2: For each of the following, indicate whether the expression is valid or invalid. If the expression is valid, indicate whether the result is integer or floating point. Write a C++ program to check your answers of following expressions.

10.0 / 3.0 + 5 * 2 10 / 4 + 6 / 3 10 % 4 + 6 % 3 (10.0 / 3.0 % 2) / 3 13.25 + (5.0 / (3.0 / 3.5)) -4 * (-5 + 6)

#include <iostream.h> void main () { int c,f,d; float b; double a,e;

a=(10.0/3.0)+(5*2); cout<<a<<endl; cout<<"It is valid"<<endl; cout<<"Answer is floating point"<<endl; b=10/4+6/3; cout<<b<<endl; cout<<"It is valid"<<endl; cout<<"Answer is floating point"<<endl;

c=10%4+6%3; cout<<c<<endl; cout<<"It is valid"<<endl; cout<<"Answer is integer"<<endl;

cout<<"It is invalid"<<endl;

e=13.25+(5.0/(3.0/3.5)); cout<<e<<endl; cout<<"It is valid"<<endl; cout<<"Answer is floating point"<<endl; f=-4*(-5+6); cout<<"It is invalid"<<endl;

Program 3: Identify and correct the errors in each of the following statements. (Note: There may be none or more than one error per statement.) int a=2; cout<< "The value of a is",a; cout<< "The product of a and b is a * b"; firstNumber + secondNumber = sumOfNumbers No error. cout<<The value of a is<<a; No error. SumOfNumbers=firstNumber+secondNumber;

Program 4: If a =10, b = 12, c = 0. Find the final value of a, b and c in the following expression. Write a program to check your answers. Identify the error, if any. a= a++; b= b--; c= a++ + ++a; c= b=b++ + b-- + a++ + ++a

c= (c++ * b-- - ++a) / --c d= (c++ % ++b) + --a + a

#include <iostream.h> void main () { int a=10,b=12,c=0,d=0;

a=a++; cout<<"Value of a = "<<a<<endl;

b=b--; cout<<"Value of b = "<<b<<endl;

c= a++ + ++a; cout<<"Value of c = "<<c<<endl;

c=b++ + b-- + a++ + a++; cout<<"Value of c = "<<c<<endl;

c=(c++*b-- - ++a)/--c; cout<<"Value of c ="<<c<<endl;

d=(c++%++b)+ --a + a--; cout<<"Value of d = "<<d<<endl;

Program 5: Convert the following mathematical formula into C++ expressions and evaluate it through C++ program.

where y a 0. #include <iostream.h> void main () { int x,y,z,a,b,A,B,C,D,M; cout<<"Enter the value of x = "; cin>>x; cout<<endl; cout<<"Enter the value of y = "; cin>>y;

cout<<endl; cout<<"Enter the value of z = "; cin>>z; cout<<endl; cout<<"Enter the value of a = "; cin>>a; cout<<endl; cout<<"Enter the value of b = "; cin>>b; cout<<endl; A=4*x+2*z; B=y-a; C=A/B*b; D=(x*x)+x+4; M=C/D; cout<<"Answer = "<<M<<endl; }

Program 6: Suppose you want to verify the interest calculation used by your bank on a loan. The interest rate is 15.5 percent, stored as .155 in a floating-point variable. The amount of interest, you owe is computed, by multiplying the interest rate by the amount of the loan balance, then multiplying that by the number of days in the year since the loan originated. The following program finds the daily interest rate by dividing the annual interest rate by 365, the number of days in a year. C++ must convert the integer 365 to a floating-point literal automatically, because it is used in combination with a floatingpoint variable.

#include <iostream.h> void main () { int days; float principle,daily_intrest,intrest_rate=0.155; cout<<"Enter the days = "; cin>>days; cout<<endl; cout<<"Enter the principle = "; cin>>principle; cout<<endl;

daily_intrest=intrest_rate/365; daily_intrest=principle*daily_intrest*days; principle=principle+daily_intrest; cout<<"Amount you lend = "<<principle<<endl; }

Program 7: Write a program that get your marks and credit hour of 5 courses and calculate your CGPA. #include <iostream.h> void main () { int itc,cal,be,eng,isl,CGPA,ttl_earned,ttl_credit_hours,ch1,ch2,ch3,ch4,ch5; cout<<"Enter itc marks = "; cin>>itc; cout<<"Enter credit hours for itc = "; cin>>ch1;

cout<<"Enter calculus marks = "; cin>>cal; cout<<"Enter credit hours for cal = "; cin>>ch2;

cout<<"Enter english marks = "; cin>>eng; cout<<"Enter credit hours for english = "; cin>>ch3;

cout<<"Enter electronics marks = "; cin>>be; cout<<"Enter credit hours of electronics = "; cin>>ch4;

cout<<"Enter islmiyat marks = "; cin>>isl; cout<<"Enter credit hours for islmiyat = "; cin>>ch5;

ttl_earned=(itc*ch1)+(cal*ch2)+(eng*ch3)+(be*ch4)+(isl*ch5); ttl_credit_hours=(ch1+ch2+ch3+ch4+ch5)/5; CGPA=ttl_earned/ttl_credit_hours/100; cout<<"CGPA = "<<CGPA<<endl;

Program 8: Assume that a video store employee works 50 hours. He is paid $4.50 for the first 40 hours, time-anda-half (1.5 times the regular pay rate) for the first five hours over 40, and double-time pay for all hours over 45. Assuming a 28 percent tax rate, write a program that prints his gross pay, taxes, and net pay to the screen. Label each amount with appropriate titles (using string literals) and add appropriate comments in the program. #include <iostream.h> void main () {

double tax,net_pay,gross_pay,A,B,C; A=4.50; cout<<"pay for first 40 hours = "<<A<<endl; B=6.75; cout<<"pay for 5 hours above 40 = "<<B<<endl; C=9.00; cout<<"pay for 5 hours above 45 = "<<C<<endl; gross_pay=A+B+C; cout<<"gross pay = "<<gross_pay<<endl; tax=0.28*gross_pay; cout<<"taxes = "<<tax<<endl; net_pay=gross_pay-tax; cout<<"net pay = "<<net_pay<<endl;

Week 6 Lab 02
You have written numerous pseudo codes and drawn different flow charts to solve problems in previous lab sessions. Now implement those solutions in C++ programs. There are 5 different problems (below) and you must write the solutions in one C++ file.

Program#1 Obtain two numbers, from user through keyboard, and determine which number (first or second) is greater. Result should be displayed with proper message as shown in expected output table below.

#include <iostream.h> void main () { int a,b; cout<<"enter the first number = "; cin>>a; cout<<"enter the second number = "; cin>>b; if (a<b) { cout<<"b is greater"<<endl; } else { cout<<"a is greater"<<endl;

Program#2 Write a program that takes two integers, from user through keyboard, and determines if the first number is a multiple of the second number. (Hint: assignment operator will be used in this program)

#include <iostream.h> void main () { int a,b,c; cout<<"enter the first number = "; cin>>a; cout<<"enter the second number = "; cin>>b; c=a%b; if (c==0) { cout<<"Multiple of eachother"<<endl; }

else { cout<<"Not multiple of eachother"<<endl; } }

Program#3 Write a program that takes a number from the user, through keyboard, and checks weather the given number is even or odd. #include <iostream.h> void main () { int a,b,c; cout<<"Enter the first number = "; cin>>a; cout<<"Enter the second number = "; cin>>b; c=a%b; if (c==0) { cout<<"Number is even"<<endl; } else

{ cout<<"Number is odd"<<endl; } }

Program#4 Write a program that takes input in form of any year (e.g. 1980) and determine whether the given year is a leap year or not. (Hint: Use the % (modulus) operator.

#include <iostream.h> void main () { int year,a; cout<<"Enter the year = "; cin>>year; a=year%4; if (a==0) {

cout<<"The given year is leap year"<<endl;

} else { cout<<"The given year is not leap year"<<endl; } }

Program #5 Write a C++ code here. IfCGPA is greater than or equal to 3.0 and warning Count is equal to zero than print you are a good student else print You should try hard

#include<iostream.h> void main() { int warningCount=0; float CGPA=0.0; cout<< "Enter your warning Count = "; cin>>warningCount; cout<< "Enter your CGPA = ";

cin>> CGPA; if (CGPA>=3.0||warningCount==0) { cout<<"You are a good student"<<endl; } else { cout<<"You should try hard"; } }

Program #6 A library charges a fine for every book that is returned late. For first 5 days the fine is 50 paisas, for 6-10 days,the fine is one rupee and above 10 days, the fine is 5 rupees. If you return the book after 30 days, your membership will be cancelled. Write a C++ program to enterthe number of days, the member is late to return the book and display the fine or the appropriate message.

#include <iostream.h> void main () { int days; cout<<"Enter the days = "; cin>>days; if (days<=5)

{ cout<<"Fine is 50 paisas"<<endl; } else { if (days>=6&&days<=10)

{ cout<<"Fine is one rupee"<<endl; } else { cout<<"Fine is 5 rupees"<<endl; } if(days>=30) { cout<<"Membership will be cancelled"<<endl; } } }

Program #7 Write a C++ program thattakes the year of birth as input, from the user, and calculates the age. If the age is between 15 and 20 then the user can take the testotherwise user cannot take any test. Furthermore, if the age is between 15 and 17 then display user can take test A. If the age is between 18 and 20 then displays user can take test B. #include<iostream.h> void main() { int year,age; cout<<"Enter the year = "; cin>>year; age=2013-year; if(age>=15&&age<=20) { cout<<"User can take test"<<endl; if(age>=15&&age<=17) { cout<<"User can take test A"<<endl; } else { cout<<"User can take test B "<<endl; } } }

Program # 8 Write a C++ Program that takes input, from user through keyboard, as single character (a-z) and determine whether the given character is vowel or not. (Hint: Write this program using logical and relational operators. The data type is char).

#include <iostream.h> void main () { char z; cout<<"Enter the alphabet = ";

cin>>z; switch(z) { case 'a':cout<<"vowel";break; case 'e':cout<<"vowel";break; case 'i':cout<<"vowel";break; case 'o':cout<<"vowel";break; case 'u':cout<<"vowel";break; case 'A':cout<<"vowel";break; case 'E':cout<<"vowel";break; case 'I':cout<<"vowel";break; case 'O':cout<<"vowel";break; case 'U':cout<<"vowel";break;

default: cout<<"consonant"<<endl; }

WEEK 7 Lab 1
Program 1: Write a C++ program to allow the user to input his/her age. Then the program will show if the person is eligible or not eligible to vote. A person who is eligible to vote must be older than or equal to 18 years old.

#include <iostream.h> void main () { int age; cout<<"enter the age"<<endl; cin>>age; if (age>=18) { cout<<"person is eligible to vote"<<endl; } else

{ cout<<"person is not eligible for vote"<<endl; } }

Program 2: Read a number from user and print the number is within range, if the number is greater than 5, between 8 & 10 or greater than 33. Otherwise, print the number is not within the range.

#include <iostream.h> void main () { int a; cout<<"enter the # "<<endl;

cin>>a; if (a>5||8<a<10||a>33) { cout<<"the no is in range "<<endl; } else { cout<<"the no is not in range " <<endl; } }

Program 3: Write a program to allow a user to input a number. Print out which category the number belongs: "positive', "negative", or "zero". #include <iostream.h>

void main () { int a; cout<<"enter the # "<<endl; cin>>a; if (a>0) { cout<<"a is positive "<<endl; } else { if (a<0) { cout<<"a is negative"<<endl; } else { cout<<"a is equal to zero"<<endl; }

Program 4: Write a C++ program to take two integers from user and determine/print the relationship between given numbers. The relationship should be measured as follows:

If the first number is less than the second number

If the first number is equal to second number

If the first number is greater than second number

#include <iostream.h> void main () {

int a,b; cout<<"enter the 1st no "<<endl; cin>>a; cout<<"enter the 2nd no "<<endl; cin>>b; if (a<b) { cout<<"1st no is less than 2nd no "<<endl; } else {

if (a==b) { cout<<"1st no is equal to 2nd no "<<endl; } else { cout<<"1st no is greater than 2nd no "<<endl; } } }

Program 5: Develop a C++ program that will determine if a department stores customer has exceeded the credit limit on a charge account. For each customer, take input of the following facts: o o o Account number Previous balance at the beginning of the month Total of all items charged by this customer this month

#include <iostream.h> void main () { int acc,beg_bal,tchrg,tcrdt,crdt_lmt,nw_bal; cout<<"enter the account # "<<endl; cin>>acc;

cout<<"enter the begening balance "<<endl; cin>>beg_bal; cout<<"enter the total charges "<<endl; cin>>tchrg; cout<<"enter the total credit "<<endl; cin>>tcrdt; cout<<"enter the credit limit "<<endl; cin>>crdt_lmt; nw_bal=beg_bal+tchrg-tcrdt; if (nw_bal>crdt_lmt) { cout<<"customers account no = "<<acc<<endl; cout<<"customers credit limit = "<<crdt_lmt<<endl; cout<<"customers new balance = "<<nw_bal<<endl; cout<<"Credit limit excedeed "<<endl; } else { cout<<"Credit limit is not excedeed "<<endl; }

Program 6: Suppose you are an amusements park ride operator. Rides often come with restrictions on who can and cannot ride, usually based on height and weight. If the height of the individual is less than or equal to 54 inches or the weight of the individual is less than 60 pounds, he or she cannot ride. If the height of the individual is greater than 84inches or his/her weight is greater than 300pounds, he or she cannot ride. Otherwise (if the rider does not match with above mentioned conditions), he/she can ride. Write a C++ program, which prompts the user for his/her height and weight (both integers) and outputs a descriptive message indicating their eligibility to ride.

# include <iostream.h> void main () { int h,w; cout<<"enter the height of the person "<<endl; cin>>h; cout<<"enter the weight of the person "<<endl;

cin>>w; if (h<=54||w<60) { cout<<"he\she cannot ride "<<endl; } else { if (h>84||w>300) { cout<<"he\she cannot ride "<<endl; } else { cout<<"he\she can ride"<<endl; } } }

Program 7: Write a C++ program for auto insurance agency. This program should determine the cost of an automobile insurance premium, based on driver's age and the number of accidents that the driver has had. The basic insurance surcharge is $500. There is a surcharge of $100 if the driver is under 25. Consider the following table in order to calculate the surcharge for accidents: # of accidents 1 2 3 4 5 6 or more Accident Surcharge 50 125 225 375 575 No insurance

#include <iostream.h>

void main () { int age,no_acci,i_prem,basic_in=500,s_chrg=100,a,b; cout<<"enter the age "<<endl; cin>>age; cout<<"enter the no of accidents "<<endl; cin>>no_acci; if (age<25) { a=basic_in+s_chrg;

if (no_acci==1) { b=a+50; cout<<"insurance premium = "<<a<<endl; } else { if(no_acci==2) { b=a+125; cout<<"insurance premium = "<<a<<endl; } else {

if(no_acci==3) { b=a+225; cout<<"insurance premium = "<<a<<endl; } else { if (no_acci==4) { b=a+375; cout<<"insurance premium = "<<a<<endl; } else { if (no_acci==5) { b=a+575; cout<<"insurance premium = "<<a<<endl; } else { cout<<"no insurance "<<endl; } } }

} }

} else { if (no_acci==1) { b=a+50; cout<<"insurance premium = "<<a<<endl; } else { if(no_acci==2) { b=a+125; cout<<"insurance premium = "<<a<<endl; } else { if(no_acci==3) { b=a+225; cout<<"insurance premium = "<<a<<endl; }

else { if (no_acci==4) { b=a+375; cout<<"insurance premium = "<<a<<endl; } else { if (no_acci==5) { b=a+575; cout<<"insurance premium = "<<a<<endl; } else { cout<<"no insurance "<<endl; } } } } } }

Week 7 Lab 02
Program 1: (Dangling Else Problem) Modify the following code to produce the output as shown below. You may not make any changes other than inserting braces at proper places. We eliminated the indentation from the following code to make the problem more challenging. *Note: Its possible that no modification is necessary.] if( y == 8 ) if( x == 5 ) printf( "@@@@@\n" ); else printf( "#####\n" ); printf( "$$$$$\n" ); printf( "&&&&&\n" ); #include <iostream.h> void main () { int x,y; cout<<"enter the value of x = "; cin>>x; cout<<"enter the value of y = "; cin>>y; if( y == 8 ) { if( x == 5 ) { cout<<( "@@@@@\n" ); } } else { cout<<( "#####\n" ); cout<<( "$$$$$\n" ); cout<<( "&&&&&\n" );

} }

Program 2: Write a C++ program that reads the marks obtained by a student in a test (maximum 100 marks) and computes his grade according to the following criteria. Marks>=80 -->grade=A Marks>=70 &<80 -->grade=B Marks>=60 &<70 --> grade=C Marks>=50 &<60 --> grade=D Otherwise grade=F

#include <iostream.h> void main () { int marks; cout<<"Enter the student marks = "; cin>>marks; if (marks>=80)

{ cout<<"Grade A"<<endl; } else { if (marks>=70&&marks<80) { cout<<"Grade B"<<endl; } else { if (marks>=60&&marks<70) { cout<<"Grade C"<<endl; } else { if (marks>=50&&marks<60) { cout<<"Grade D"<<endl; } else { cout<<"Grade F"<<endl; } } } } }

Program 3: Consider the following statement. if age >minAgethen if income >minIncomethen put "Accept" else put "Reject" endif endif What will the statement print, if age >minAge and income <minIncome? What will the statement print, if age <minAge and income <minIncome?

#include <iostream.h> void main () { int age,minAge,income,minIncome; cout<<"enter the age = "; cin>>age; cout<<"enter the minAge = "; cin>>minAge; cout<<"enter the income = ";

cin>>income; cout<<"Enter the minAge = "; cin>>minAge; if (age>minAge) { if (income>minIncome) { cout<<"Accept"<<endl; } } else { cout<<"Reject"<<endl; } }

Program 4: Write a C++ program to select Teaching Assistants (TAs). The program takes as input, the students cgpa and students gpa in programming course and checks whether the student is eligible for TA or not. A student is eligible for TA only if he/she has a CGPA above 3 and his/her GPA in programming course is also above 3. Otherwise he will not be offered any TA.

For all those students, who satisfy the condition for eligibility of TA (cgpa and gpa is greater than 3), There are four types of TA, offered to a student according to following criteria. - Full time TA if he/she is student of MS. - Part time TA if he/she is student of BS. The program takes input, the type of degree in a variable of type char, where M is for MS student and B represents BS student.

#include <iostream.h> void main () { float gpa,cgpa; char degree,M,B; cout<<"enter student gpa = "; cin>>gpa; cout<<"enter student cgpa = "; cin>>cgpa; cout<<"enter the degree = "; cin>>degree; if (gpa>3&&cgpa>3) { cout<<"eligibe for TA"<<endl; } else { cout<<"Not eligible for TA"<<endl; } if (degree=='M') { cout<<"Full time TA"<<endl; } else {

if (degree=='B') { cout<<"Part time TA"<<endl; } } }

Program 5: Write a C++ program that displays water bills. Your program should prompt the user to enter an integer account number, a code in character, and a real number representing the gallons of water being used. The output from your program should include the account number, message indicating the type of usage, and the amount of money due from the user. The water rates vary depending on the type of usage. A code of H means home use, a code of C means commercial use, and a code of I means industrial use. An y other code value should be treated as an error. Water rates are computed as follows: Code H: first 1 hundredgallons: $5.00 $0.005 for each additional gallon used Code C: first 4 hundredgallons:$1000.00 $0.025 for each additional gallon used

Code I:

first 4 hundred gallons: $1500 $0.125 for each additional gallon used

#include<iostream.h> void main() { float an,g,r,rr,rrr,rrrr; char c; cout<<"enter account number = "; cin>>an; cout<<"enter code = "; cin>>c; cout<<"enter number of gallons used = "; cin>>g; if(c=='h') { if(g>100) { r=g-100; rr=r*0.005; rrrr=r+rr; cout<<"dollars = "<<rrrr; } else

{ cout<<"dollars = "<<"5"; } } else {

if(c=='c') { if(g>400) { r=g-400; rr=r*0.025; rrr=r+rr; cout<<"dollars = "<<rrr<<endl; } else { cout<<"dollars = "<<1000<<endl; } } else {

if(c=='i') { if(g>400) { r=g-400; rr=r*0.125; rrr=r+rr; cout<<"dollars = "<<rrr<<endl; } } else { cout<<"dollars = "<<1500<<endl; }

} }

WEEK 8 LAB 1
Program 1:

Write a C++ program that: 1. 2. 3. 4. Declare two variables. Initialize the second variable to be 1000. Gets a value of the first variable from the user. If the first variable is 0, the program increments the first variable. If the first variable is 1, the program decrements the first variable and increments the second variable. 5. If the first variable is -100, the program increments the first variable and decrements the second integer. 6. In all other cases, the program sets both variables to 0. 7. Print the final result (both variable) on screen.
#include <iostream.h> void main () { cout<<"\t\t\tMuhammad L1S13BSCS0145\n"; cout<<endl; cout<<endl; cout<<endl; Khidr \n\t\t\t

int a,b=1000; cout<<"Value Of The First Number : "; cin>>a; if(a==0) { a++;

cout<<"Incremented value of 'a' : "<<a; } else if(a==1) { a--; b++; cout<<"Decremented value of 'a' : "<<a<<endl; cout<<"Incremented value of 'b' : "<<b<<endl; } else if(a==-100) { a++; b--; cout<<"Incremented value of 'a' : "<<a<<endl; cout<<"Decremented value of 'b' : "<<b<<endl; } else { b=0; a=0;

cout<<"Setting Both variables to zero \n"<<"'a' : "<<a<<endl<<"'b' : "<<b<<endl;

Program 2:

Write a nested if statement to print the appropriate activity depending on the value of a variable (temperature and humidity) as in the table below: Assume that the temperature can only be warm and cold, and the humidity can only be dry and humid. If temperature is if humidity is Print this activity Warm Warm Cold Cold Dry Humid Dry Humid "Play tennis" "Swim" "Play basketball" "Watch TV"

#include <iostream.h> void main ()

{ cout<<"\t\t\tMuhammad Khidr \n\t\t\t L1S13BSCS0145\n"; cout<<endl; cout<<endl; cout<<endl;

int temp,humid; cout<<"Temperature : "<<endl; cout<<"Select \n 1 - Warm \n 2 - Cold "<<endl<<endl; cin>>temp; cout<<"Humidity Level : "; cout<<"Select \n 3 - Dry \n 4 - Humidity "<<endl<<endl; cin>>humid;

if(temp==1&&humid==3) { cout<<"Play Tennis "<<endl; } else if (temp==1&&humid==4) { cout<<"Swim "<<endl; } else if (temp==2&&humid==3) { cout<<"Play Basketball "<<endl; }

else if (temp==2&&humid==4) { cout<<"Watch Television "<<endl; } }

Program 3:

A company insures its drivers in the following cases: If the driver is married. If the driver is unmarried, male & above 30 years of age. If the driver is unmarried, female & above 25 years of age.

In all other cases the driver is not insured. If the marital status, sex and age of the driver are the inputs, write a program to determine whether the driver is to be insured or not. Here after checking a complicated set of instructions the final output of the program would be one of the twoEither the driver should be ensured or the driver should not be ensured.
#include <iostream.h> void main () { cout<<"\t\t\tMuhammad Khidr \n\t\t\t L1S13BSCS0145\n"; cout<<endl; cout<<endl; cout<<endl; int maritalstatus,gender,age; cout<<"Marital Status of Driver :"<<endl; cout<<"1- Married \n2 - Unmarried"<<endl; cin>>maritalstatus; if (maritalstatus==1) { cout<<"Driver is Insured "<<endl; } else { cout<<"Driver Is Unmarried\n\n"; cout<<"Gender Of The Driver : "<<endl; cout<<"1 - Male \n2- Female "<<endl; cin>>gender; cout<<"Age Of The Driver : "; cin>>age;

if(gender==1&&age>=30) { cout<<"Male Driver is Insured "<<endl; } else if (gender==2&&age>=25) { cout<<"Female Driver is Insured "<<endl;

} else { cout<<"Driver is not Insured "<<endl; } }

Program 4:

In a company, worker efficiency is determined on the basis of the time required for a worker to complete a particular job. If the time taken by the worker is between 2 3 hours, then the worker is said to be highly efficient. If the time required by the worker is between 3 4 hours, then the worker is ordered to improve speed. If the time taken is between 4 5 hours, the worker is given training to improve his speed, and if the time taken by the worker is more than 5 hours, then the worker hasto leave the company. If the time taken by the worker is input through the keyboard, find the efficiency of the worker.

#include <iostream.h> void main () {

cout<<"\t\t\tMuhammad Khidr \n\t\t\t L1S13BSCS0145\n"; cout<<endl; cout<<endl;

cout<<endl; float time_taken; cout<<"Time taken for the worker to complete the Task (hours) : "; cin>>time_taken; if (time_taken>=2&&time_taken<=3) { cout<<"Highly Efficient "<<endl; } else if (time_taken>3&&time_taken<=4) { cout<<"Needs to Improve Speed "<<endl; } else if (time_taken>4&&time_taken<=5) { cout<<"Needs Training "<<endl; } else if (time_taken>5) { cout<<"The Employee is FIRED "<<endl; }

Program 5:

Write a program using SWITCH structure to determine the total weight of chocolates being sold. The program takes as input, the number of chocolates being sold, weight of one chocolate in ounces and the choice of weighing i.e. ounces, pounds, grams or kilograms. User enters O as a choice to calculate weight in ounces, P for pounds, G for grams and K for kilograms. Depending on the choice entered by the user the program calculates the weight of chocolates. Display an appropriate message if invalid choice is entered. Use the following formulas to calculate total weight of chocolates. For weighing in Ounces total_weight = number_of_chocolates * weight_of_Chocolate For weighing in Pounds total_weight = number_of_chocolates * weight_of_Chocolate / 16 For weighing in Grams total_weight = number_of_chocolates*weight_of_Chocolate * 28.349 For weighing in Kilograms total_weight = number_of_chocolates*weight_of_Chocolate*28.349/1000;

#include <iostream.h> void main () { cout<<"\t\t\tMuhammad Khidr \n\t\t\t L1S13BSCS0145\n"; cout<<endl; cout<<endl; cout<<endl;

double sold,weight; char i; cout<<"Enter the number of chocolates being sold : "; cin>>sold; cout<<"Enter the weight of one chocolate : "; cin>>weight; cout<<endl; cout<<"Choice for weighing is as following"<<endl<<endl; cout<<"Enter 'o' for ounces\nEnter 'p' for pounds\nEnter 'g' for grams\nEnter 'k' for kilograms "<<endl<<endl; cin>>i; cout<<endl; cout<<endl; switch(i) { case 'o' :cout<<"Total weight is : "<<sold*weight<<" ounces"<<endl;break; case 'p' :cout<<"Total weight is : "<<sold*weight/16<<" pounds"<<endl;break; case 'g' :cout<<"Total weight is : "<<sold*weight*(28.349)<<" grams"<<endl;break; case 'k' :cout<<"Total weight is : "<<sold*weight*(28.249/1000)<<" kilograms"<<endl;break;

} }

Program 6:

In the Chinese calendar, every year is associated with a particular animal. The 12-year animal cycle is rat, ox, tiger, rabbit, dragon, snake, horse, goat, monkey, rooster, dog, and boar. The year 1900 is a year of the rat; thus 1901 is a year of the ox and 1912 is another year of the rat. If you know in what year a person was born, you can compute the offset from 1900 and determine the animal associated with that persons year of birth. (! First, think of the formula) Create a program using SWITCH structure that determines the animal corresponding to an input year of birth. The program should take as input the year of birth in a variable of type int. The program should then display the corresponding animal.

#include <iostream.h>

void main () { cout<<"\t\t\tMuhammad Khidr \n\t\t\t L1S13BSCS0145\n"; cout<<endl; cout<<endl; cout<<endl;

int year,m; cout<<"Enter the year : "; cin>>year;

m=year%12; cout<<m;

switch(m) { case 0 :cout<<"Rat"<<endl;break; case 1:cout<<"Ox"<<endl;break; case 2:cout<<"tiger"<<endl;break; case 3:cout<<"rabbit"<<endl;break; case 4:cout<<"dragon"<<endl;break; case 5:cout<<"snake"<<endl;break; case 6:cout<<"horse"<<endl;break; case 7:cout<<"goat"<<endl;break; case 8:cout<<"monkey"<<endl;break; case 9:cout<<"rooster"<<endl;break; case 10:cout<<"Dog"<<endl;break;

case 11:cout<<"boar"<<endl;break; } }

Program 7:

Write a program to find the total grace marks for a student using switch statement. The user should enter the class obtained by the student and the number of subjects he has failed in. if the student gets first class and the number of subjects, he failed in, is greater than 3, then he does not get any grace marks. If the number of subjects, he failed in, is less than or equal to 3 then the grace marks are 5 per subject. if the student gets second class and the number of subjects, he failed in, is greater than 2, then he does not get any grace marks. If the number of subjects, he failed in, is less than or equal to 2 then the grace marks are 4 per subject. if the student gets third class and the number of subjects, he failed in, is greater than 1, then he does not get any grace marks. If the number of subjects, he failed in, is equal to 1 then the grace marks are 5 per subject

#include <iostream.h> void main () { cout<<"\t\t\tMuhammad Khidr \n\t\t\t L1S13BSCS0145\n"; cout<<endl; cout<<endl; cout<<endl;

int failed,c_; cout<<"Number of Subjects failed : "; cin>>failed; cout<<"Select\n1 - First Class\n2 - Second Class\n3 - Third Class\n"; cin>>c_; switch(c_) {

case 1:cout<<"First Class "<<endl; if(failed>3) { cout<<"No Grace marks awarded"<<endl; } else if (failed<=3) { cout<<"Per subject 5 Grace marks awareded "<<endl;

};break; case 2:cout<<"Second Class "<<endl; if(failed>2) { cout<<"No Grace marks awarded"<<endl; } else if (failed<=2) { cout<<"Per subject 4 Grace marks awareded "<<endl; }; break; case 3:cout<<"Third Class "<<endl; if(failed>1) { cout<<"No Grace marks awarded"<<endl; } else if (failed==1) { cout<<"Per subject 5 Grace marks awareded "<<endl; }; break; } }

WEEK 8 LAB 2
Program 1:

Write the answers on the answer sheet: 1) After execution of the following code, what is stored in num? (All variables are of type int.) Hint: use i=3, j=5, and k=7 as sample values. if (j > k) if (i > j) num = i; else num = j; else if (i > k) num = i; else num = k; a. b. c. e. 3 5 7 9

#include <iostream.h> void main () { cout<<"\t\t\tMuhammad Khidr \n\t\t\t L1S13BSCS0145\n"; cout<<endl; cout<<endl; cout<<endl;

cout<<"PART 1 "<<endl<<endl; int i=3,j=5,k=7,num;

if (j > k) if (i > j) { num = i; cout<<num<<endl; } else { num = j; cout<<"Value Of 'J' : "<<num<<endl; }

else if (i > k) { num = i;

cout<<"Value Of 'I' : "<<num<<endl; } else {

num = k; cout<<"Value Of 'K' :"<<num<<endl<<endl<<endl;

cout<<"PART 2 "<<endl<<endl<<endl;

int x=10, y=0;

if (x > 0) { y = y + 1; cout<<"Value Of 'Y' : "<<y<<endl; }

else if ( x < 0) { y = y + 2; cout<<"Value Of 'Y' : "<<y<<endl; } else

{ y = y + 5;

cout<<"Value Of 'Y' : "<<y<<endl; } }

Program 2:

2) After execution of the following code, what will be the value of y? int x=10, y=0; if (x > 0) y = y + 1; else if ( x < 0) y = y + 2; else y = y + 5;

a. b. c. d. e.

1 2 3 5 8

#include <iostream.h> void main () { cout<<"\t\t\tMuhammad Khidr \n\t\t\t L1S13BSCS0145\n"; cout<<endl; cout<<endl; cout<<endl;

cout<<"PART 1"<<endl<<endl;

int angle = 13; if (angle <= 10) { angle = angle + 10; cout<<"Angle :: "<<angle<<endl; } else if (angle <= 20) { angle = angle + 5; cout<<"Angle : "<<angle<<endl; } else

{ angle = angle + 3; cout<<"Angle = "<<angle<<endl; }

cout<<"PART 2 "<<endl<<endl;

int x = 5; if (x > 5) {x = x + 10; cout<<"Value Of X : "<<x<<endl; } else if ( x < 3) { x = x - 5; cout<<"Value Of X : "<<x<<endl; } else { x = 100; cout<<"Value Of X : "<<x<<endl; }

Program 3:

1) After execution of the following code, what will be the value of angle? int angle = 13; if (angle <= 10) angle = angle + 10; else if (angle <= 20) angle = angle + 5; else angle = angle + 3; a. b. c. d. e. 16 18 20 23 33

#include <iostream.h> void main ()

{ cout<<"\t\t\tMuhammad Khidr \n\t\t\t L1S13BSCS0145\n"; cout<<endl; cout<<endl; cout<<endl; cout<<"PART 1 "<<endl<<endl;

int i = 3, j = 5; if (i = j) { cout << "Same"<<endl<<endl; } else { cout << "Different"<<endl<<endl; }

cout<<"PART 2 "<<endl<<endl; int alpha = 10;

if (alpha < 5) { cout << "Apple ";

cout << "Orange "; cout << "Banana";

} else {

cout<<"No output; there is a compile-time error"<<endl; } }

WEEK 9 LAB 1
Program 1: While loop that is counting Type the given code and observe the output 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. #include <iostream.h> int main() { int counter = 0; // initialize the condition while(counter < 5) // test condition still true { counter++; // body of the loop cout << "counter: " << counter << "\n"; } cout << "Complete. Counter: " << counter << ".\n"; return 0; }

#include <iostream.h> int main()

{ int counter = 0; // initialize the condition // test condition still true

while(counter < 5) { counter++;

// body of the loop

cout << "counter: " << counter << "\n"; } cout << "Complete. Counter: " << counter << ".\n"; return 0; }

Program 2: Type the given code and observe the output 1. 2. 3. 4. 5. 6. 7. 8. #include<iostream.h> main() {

int x = 1; while (x <= 10) { cout<< "C++ CONTROL STRUCTURES \n"; x = x + 1; 9. }

#include<iostream.h> main() { int x = 1; while (x <= 10)

{ cout<< "C++ CONTROL STRUCTURES \n"; x = x + 1; } }

Program 3:

Complete the given code 1. loop_response = 'y'; 2. while (loop_response == 'y'); 3. { 4. cout << "\nWhat is your age? "; 5. cin >> age_user; 6. cout << "\nWhat is your friend's age? "; 7. cin >> age_friend; 8. cout<< "\nTogether your ages add up to: "; 9. cout << (age_user + age_friend); cout << "\nDo you want to do it again? y or n "; 10. cin >> loop_response; 11. }

# include <iostream.h>

void main ()

int age_user,age_friend;

char loop_response = 'y';

while (loop_response == 'y')

cout << "\nWhat is your age? = ";

cin >> age_user;

cout << "\nWhat is your friend's age? = ";

cin >> age_friend;

cout<< "\nTogether your ages add up to: ";

cout << (age_user + age_friend)<<endl;

cout << "\nDo you want to do it again? y or n = ";

cin >> loop_response;

Program 4: Write a program using while loop to repeatedly print the value of the variable xValue, decreasing it by 0.5 each time, as long as xValue remains positive.

#include <iostream.h> void main () { float a; cout<<"enter the # "<<endl; cin>>a;

while (a>0) { a=a-0.5; cout<<a<<endl;

} }

Program 5:

Write a program using while loop to find factorial of a positive integer?

#include <iostream.h> void main () { int a,b,fact=1;

cout<<"enter the value"<<endl; cin>>b; for (a=b; a>=1; a--) { fact=fact*a;

cout<<fact<<endl; }

Program 6: Write a program that finds and prints all of the prime numbers between 3 and 100. A prime number is a number such that one and itself are the only numbers that evenly divide it(example: 3,5,7,11,13,17,..)

#include<iostream.h> void main() { int c,a=1,b; cout<<"prime # b/w 1 to 1000"<<endl; while(b<=100) { c=0; a=1; while(a<=b) { if(b%a==0) c++; a++; } if(c==2||c==1) cout<<"\n"<<b<<endl; b++; } }

program 7:

Write a program to print the multiplication table of the number entered by the user.
# include <iostream.h>

void main () { int a,b=1; cout<<"enter the no "<<endl; cin>>a; while(b<=10) { cout<<a<<"*"<<b<<"="<<a*b<<endl; b++; }

WEEK 9 LAB 2
Program 1: Write the statements, including a While loop, required to calculate y (t) from the equation:
3t2 +5 t 0

3t2 +5

t<0

For values of t between -9 and 9. Display each value of t and y(t) #include <iostream.h> void main () { cout<<"\t\t\tMuhammad Khidr \n\t\t\t L1S13BSCS0145\n"; cout<<endl; cout<<endl; cout<<endl; int t=0,y=0;

cout<<"When y= -3*(t*t)+5 "; while(t<=9) { y=-3*(t*t)+5; cout<<"Value Of 'Y' : "<<y<<endl; t++; } cout<<endl;

cout<<"When y= 3*(t*t)+5 "<<endl<<endl;

while(t>=0) { y=3*(t*t)+5; cout<<"Value Of 'Y' : "<<y<<endl; t--; }

Program 2: Write a program that accepts numbers from the user and computes their sum. The numbers should be entered one at a time and the output of all the numbers entered thus far should then be displayed. The program should halt when the user enters the number m 2:

#include <iostream.h> void main () { cout<<"\t\t\tMuhammad Khidr \n\t\t\t L1S13BSCS0145\n"; cout<<endl; cout<<endl; cout<<endl;

int sum=0,num=0;

cout<<"Enter The Number : "; cin>>num; while(num!=0) { sum=sum+num;

cout<<"Cumulative sum : "<<sum<<endl;

cout<<"Enter The Number : "; cin>>num;

} cout<<endl<<endl<<endl; cout<<"Thank you for using this program.\n\n\n\n\t\t\t***** PROGRAM TERMINATE *****"<<endl; }

Program 3: Write a program that uses while loop to perform the following steps:

a) Prompts user to input two integers Num1 and Num2. Num1 must be less than Num2. If not then Swap values of both numbers. b) Output all numbers between Num1 and Num2. c) Output Sum of all numbers between Num1 and Num2. d) Output numbers and their squares between Num1 and Num2. e) Output sum of squares of odd numbers between Num1 and Num2.

#include <iostream.h>

void main () { cout<<"\t\t\tMuhammad Khidr \n\t\t\t L1S13BSCS0145\n"; cout<<endl; cout<<endl; cout<<endl;

int a,num1=0,num2=0,sum=0,sq=1,odd=1; cout<<"Number 1 : "; cin>>num1; cout<<"Number 2 : "; cin>>num2; cout<<endl<<endl;

if(num1>num2) { num1=num1+num2; num2=num1-num2; num1=num1-num2; cout<<"Swapped Values "<<endl; cout<<"Number 1 : "<<num1<<endl; cout<<"Number 2 : "<<num2<<endl; } cout<<"Numbers Between num1 and num2 : "<<endl<<endl; cout<<"Numbers \t\t Square "<<endl;

while(num1<=num2) {

sq=(num1*num1); cout<<num1<<"\t\t\t\t"<<sq<<endl; sum=sum+num1; if(num1%2!=0) { a=num1*num1; odd=odd+a; } num1++; } cout<<"\n\n\nSum Of The Numbers Between num1 and num2 "<<endl<<endl; cout<<sum<<endl<<endl;

cout<<"Sum Of Odd Square Numbers "<<odd-1<<endl<<endl;

Program 4:

Write a program that takes as input time expressed in 2400 format (e.g. 23:05 hours), convert it to 1200 hours format (e.g. 11:05 p.m) and display the new time on the screen. Your program should repeat the processing until a sentinel time of -99 is entered.

You might also like