c++ practical lab

16

Upload: bachagul-ghaljai

Post on 06-Aug-2015

184 views

Category:

Engineering


5 download

TRANSCRIPT

Page 1: C++ practical lab
Page 2: C++ practical lab

Programming whit C++

TABLE OF CONTENTSTABLE OF CONTENTSTABLE OF CONTENTSTABLE OF CONTENTS

Page No

Submitted to: Mr.Kamran Khan

Submitted by: Padshah Gul Course: Bs-Telecom

1. WRITE A PROGRAM TO SHOW YOUR DESIRE TEXT ON SCREEN:…………………………………………………………………… 1

2. FINDING VELOCITY OF DRAM ( ∏∏∏∏R2.H): ……………………………………………………………………………………………………….. 1

3. USING SOME ARITHMETIC OPERATIONS:……………………………………………………………………………………………………… 2

4. TEMPERATURE CALCULATION:……………………………………………………………………………………………………………………… 3

CONVERT CELSIUS TO FAHRENHEIT: ....................................................................................................................................... 3

CONVERT FAHRENHEIT TO CELSIUS: ....................................................................................................................................... 3

WRITE A PROGRAM TO CALCULATE THE TOTAL MARKS, AVERAGE & PERCENTAGE OF 10TH DIFFERENT SUBJECTS: ............................... 4

CREATING DESIGN OF ATM MACHINE DISPLAY IN C++: ............................................................................................................ 5

TIME CONVERTER:.............................................................................................................................................................. 6

LENGTH CONVERTER: .......................................................................................................................................................... 6

WRITE A PROGRAM TO CALCULATE AVERAGE AGE OF A CLASS OF 10TH STUDENTS: ......................................................................... 8

5. INCREMENT:………………………………………………………………………………………………………………………………………………... 9

PREFIX (++I): .................................................................................................................................................................... 9

POSTFIX (I++): ................................................................................................................................................................ 10

6. DECREMENT:………………………………………………………………………………………………………………………………………………… 11

PREFIX (--I): .................................................................................................................................................................... 11

POSTFIX (I--): .................................................................................................................................................................. 11

7. CONDITION STATEMENTS:……………………………………………………………………………………………………………………………. 13

IF CONDITION: ............................................................................................................................................................. 13

IF CONDITION: ............................................................................................................................................................. 13

IF CONDITION: ............................................................................................................................................................. 14

IF-ELSE: ......................................................................................................................................................................... 14

IF-ELSE: ......................................................................................................................................................................... 15

WRITE A PROGRAM TO CHECK WITHER THE GIVEN NUMBER IS ODD OR EVEN. ............................................................................ 16

USING (IF-ELSE) ............................................................................................................................................................... 16

WRITE A PROGRAM TO CHECK A PERSON IS MALE OR FEMALE BY INPUT CHARACTER ....................................................................... 16

USING (IF-ELSE): .............................................................................................................................................................. 16

WRITE A PROGRAM TO CHECK POSITIVE NUMBER AND NEGATIVE NUMBER ................................................................................... 17

USING (IF-ELSE): .............................................................................................................................................................. 17

NESTED IF ELSE: ............................................................................................................................................................... 18

USING OF SIMPLE ARITHMETIC OPERATORS (USING IF ELSE): ..................................................................................................... 19

A PROGRAM IN WHICH MAKERS OBTAINED IN ANY SUBJECT INPUT THROUGH KEYBOARD ................................................................. 20

CALCULATE TOTAL, PERCENTAGE AND AVERAGE USE LOGICAL OPERATORS: ................................................................................. 20

8. SWITCH STATEMENT:…………………………………………………………………………………………………………………………………… 21

-- :( SPORTS REGISTRATION MENU ): -- ........................................................................................................................ 21

9. LOOPS:………………………………………………………………………………………………………………………………………………………… 26

WRITE A PROGRAM TO PRINT FIVE TIMES “I LOVE PAKISTAN" (USING WHILE LOOP): .................................................................... 26

WRITE A PROGRAM TO PRINT NATURAL NUMBER FROM 1 TO 100 IN ASCENDING FORM BY (USING WHILE LOOP): .......................... 26

WRITE A PROGRAM TO PRINT NATURAL NUMBER FROM 10 TO 1 IN DESCENDING FORM BY (USING WHILE LOOP): ........................... 27

Page 3: C++ practical lab

Programming whit C++

TABLE OF CONTENTSTABLE OF CONTENTSTABLE OF CONTENTSTABLE OF CONTENTS

Page No

Submitted to: Mr.Kamran Khan

Submitted by: Padshah Gul Course: Bs-Telecom

WRITE A PROGRAM TO PRINT FIRST EVEN NUMBER FROM 1 TO 100 BY (USING WHILE LOOP): ..................................................... 28

WRITE A PROGRAM TO PRINT FIRST ODD NUMBER FROM 1 TO 100 BY (USING WHILE LOOP): ....................................................... 28

WRITE A PROGRAM TO FIND THE SUM OF FOLLOWING SERIES BY (USING WHILE LOOP). ................................................................ 29

USING DO-WHILE LOOP: .................................................................................................................................................... 29

WRITE A PROGRAM TO PRINT NATURAL NUMBER FROM 1 TO 10 AND SUM IT BY ........................................................................... 30

(USING DO-WHILE LOOP): .................................................................................................................................................. 30

WRITE A PROGRAM TO PRINT TABLE OF TWO BY (USING FOR LOOP): .......................................................................................... 31

WRITE A PROGRAM TO PRINT TABLE OF ANY NUMBER BY (USING FOR LOOP): ............................................................................... 32

NATURAL NUMBER FROM (1 TO 10 AND FORM 10 TO 1) BY (USING FOR LOOP): .................................................................... 32

NESTED LOOPS: ............................................................................................................................................................... 33

10. ARRAY:………………………………………………………………………………………………………………………………………………………… 34

WRITE A PROGRAM IN ARRAY TO PRINT FIXED NUMBER FROM INPUT: ......................................................................................... 34

WRITE A PROGRAM IN ARRAY TO PRINT VARIABLE NUMBER (USER CHOICE) CALCULATE SUM AND AVERAGE: ..................................... 34

WRITE A PROGRAM IN ARRAY TO ENTER VALUE AND THEN PRINT VALUE IN REVERSE ORDER. .......................................................... 35

WRITE A PROGRAM TO FIND OUT AND PRINT THE MAXIMUM VALUE IN THE ARRAY: ...................................................................... 36

WRITE A PROGRAM TO FIND OUT AND PRINT THE MINIMUM VALUE IN THE ARRAY: ....................................................................... 37

WRITE A PROGRAM IN A CHARACTER TYPE ARRAY TO COPY "FIRST" ARRAY INTO "SECOND" ARRAYS: ................................................ 37

WRITE A PROGRAM TO INPUT DATA INTO TWO DIFFERENT ARRAYS AND THEN SUM INTO A THIRD ARRAY. ........................................... 38

STRING VARIABLE (NULL CHARACTER) .................................................................................................................................. 39

WRITE A PROGRAM IN A STRING TYPE ARRAY TO COPY "FIRST" ARRAY INTO "SECOND" ARRAYS: ...................................................... 39

TWO DIMENSION ARRAY: .................................................................................................................................................. 40

WRITE A PROGRAM TO PRINT THE OUTPUT OF FOLLOWING TABLE. ............................................................................................. 40

WRITE A PROGRAM TO PRINT TABLE OF 4X5(4 ROWS & 5 COLUMNS). ....................................................................................... 41

11. STRUCTURES:……………………………………………………………………………………………………………………………………………….. 42

WRITE A PROGRAM TO INPUT DATA INTO AND THEN PRINT DATA FROM THE MEMBERS OF STRUCTURE: ............................................. 42

WRITE A PROGRAM TO DEFINE A STRUCTURE WITH FIVE MEMBERS. THE FIRST MEMBER BE STUDENT NAME AND THE OTHER BE OBTAINED

MARKS IN FOUR SUBJECTS. ................................................................................................................................................. 43

WRITE A PROGRAM TO INPUT DATA IN STRUCTURE AND THEN TO FIND OUT THE SUM OF MARKS. STORE THE RESULT IN THE MEMBER

“TOTAL” PRINT THE DATA ON THE SCREEN. ............................................................................................................................ 44

12. FUNCTION:………………………………………………………………….……………………………………………………………..……………….. 46

WRITE A PROGRAM TO PRINT A MESSAGE ON THE SCREEN USING A FUNCTION. ............................................................................ 46

WRITE A PROGRAM TO PASS THREE VARIABLES AS PARAMETERS FROM INPUT TO THE FUNCTION TO CALCULATE THEIR SUM. .................. 46

WRITE A PROGRAM TO PASS THREE VARIABLES AS PARAMETERS FROM USER TO THE FUNCTION TO CALCULATE THEIR SUM. ................... 47

WRITE A PROGRAM TO PRINT A TABLE OF A GIVEN NUMBER USING FUNCTION. ............................................................................. 48

WRITE A PROGRAM FOR CALCULATOR BY PASSING THREE ARGUMENTS TO THE FUNCTION. ............................................................. 49

1ST AND 3RD ARE NUMERIC NUMBERS AND 2ND IS ARITHMETIC OPERATORS. .............................................................................. 49

WRITE A PROGRAM TO FIND THE MAXIMUM NUMBER FROM AN ARRAY BY USING THE FUNCTION. .................................................... 50

LOCAL VARIABLES IN FUNCTION: ......................................................................................................................................... 51

GLOBAL VARIABLES IN FUNCTION: ....................................................................................................................................... 52

Page 4: C++ practical lab

Chapter: 1Chapter: 1Chapter: 1Chapter: 1

Programming whit C++

--------------------------------------------------------------------------------------------------------------------------------------------------------- Submitted to: Mr.Kamran Khan

Submitted by: Padshah Gul Course: Bs-Telecom Page No: - 1 of 52

Write a program to show your Desire text on screen:

#include <iostream.h> #include <conio.h> void main (void) { clrscr(); cout<<"My name is Padshah Gul."<<endl; cout<<"I am student of Sarhad University."<<endl; cout<<"I am from Afghanistan."<<endl; getch(); } Output of Program: My name is Padshah Gul. I am student of Sarhad University. I am from Afghanistan.

Finding velocity of dram ( ∏∏∏∏r2.h): #include <iostream.h> #include <conio.h> void main (void) { clrscr(); float h=8; float r=5; float pi=3.14; float v=h*r*r*pi; cout<<"Result is "<<v; getch();

Height= 8 cm

2r=10 cm

Page 5: C++ practical lab

Chapter: 1Chapter: 1Chapter: 1Chapter: 1

Programming whit C++

--------------------------------------------------------------------------------------------------------------------------------------------------------- Submitted to: Mr.Kamran Khan

Submitted by: Padshah Gul Course: Bs-Telecom Page No: - 2 of 52

}

Using some arithmetic operations:

Like ( +, -, *, /, %)

#include <iostream.h> #include <conio.h> void main (void) { clrscr(); int p,s,m,d,r; p=7+5; s=10-3; m=4*2; d=10/2; r=11%5; cout<<"output of arithmetic operators"<<endl; cout<<endl<<"Addition of 8 and 2 is = "<<p<<endl; cout<<"Subtraction of 10 and 3 is = "<<s<<endl; cout<<"Multiplication of 4 and 2 is = "<<m<<endl; cout<<"Division of 10 and 2 is = "<<d<<endl; cout<<"Remainder of 5/2 is = "<<r<<endl; getch(); } Output of the program: Addition of 8 and 2 is = 10

Subtraction of 10 and 3 is = 7

Multiplication of 4 and 2 is = 8

Division of 10 and 2 is = 5

Page 6: C++ practical lab

Chapter: 1Chapter: 1Chapter: 1Chapter: 1

Programming whit C++

--------------------------------------------------------------------------------------------------------------------------------------------------------- Submitted to: Mr.Kamran Khan

Submitted by: Padshah Gul Course: Bs-Telecom Page No: - 3 of 52

Remainder of 5/2 is = 1

Temperature Calculation:

Convert Celsius to Fahrenheit:

Convert Fahrenheit to Celsius:

#include <iostream.h> #include <conio.h> void main(void) { clrscr(); cout<<endl<<endl<<"\t\t\t Temperature Calculation"< <endl; cout<<"\t\t\t"<<endl; cout<<endl<<endl<<"\t0-------------0"<<endl; cout<<"\tl Well Come!! l"<<endl; cout<<"\t0-------------0"<<endl; cout<<endl<<endl<<"\t\convert Fahrenheit to Celsius "<<endl; float c,f; cout<<endl<<endl<<"\tPlease Enter you Temperature i n Fahrenheit = "; cin>>f; c=(f-32)*5/9; cout<<endl<<endl<<"\tYour Temperature in Celsius is = "<<c; cout<<endl<<endl<<"\t============================== ================================ "<<endl; cout<<endl<<endl<<"\t\ it "<<endl; cout<<endl<<endl<<"\tPlease Enter you Temperature i n Celsius = "; cin>>c; f=(c*9/5)+32; cout<<endl<<endl<<"\tYour Temperatures in Fahrenhei t is = "<<f; cout<<endl<<endl<<endl<<"\t\tO=========== 0"<<endl; cout<<"\t\tl Thank you l"<<endl; cout<<"\t\t0===========0"<<endl;

Page 7: C++ practical lab

Chapter: 1Chapter: 1Chapter: 1Chapter: 1

Programming whit C++

--------------------------------------------------------------------------------------------------------------------------------------------------------- Submitted to: Mr.Kamran Khan

Submitted by: Padshah Gul Course: Bs-Telecom Page No: - 4 of 52

getch(); }

Write a Program to Calculate the Total Marks, Average & Percentage of 10th Different Subjects:

#include <iostream.h> #include <conio.h> void main (void) { clrscr(); float i,c,b,p,f,t,avg,tot,per; cout<<endl<<endl<<"\t\t============================ ================="<<endl; cout<<"\t --:( I Sarhad University of Information Technology I ):--"<<endl; cout<<"\t\t======================================== ====="<<endl; cout<<endl<<"\t\tEnter the Marks of 6th Subjects"<< endl<<endl; cout<<"\t\t1.\tIslamic Study = "; cin>>i; cout<<"\t\t2.\tCaculus & Analytical Geometry = "; cin>>c; cout<<"\t\t3.\tIntroduction to Cs = "; cin>>b; cout<<"\t\t4.\tBasic Communication Skill = "; cin>>t; cout<<"\t\t5.\tProgramming Fundamentals = "; cin>>f; cout<<"\t\t6.\tPhysics-1 = "; cin>>p; tot=p+f+c+t+b+i; cout<<endl<<endl<<"\t-->\tTotal of 10th Subjects ar e = "<<tot<<endl; avg=(p+t+b+c+f+i)/6; cout<<endl<<"\t-->\tAverage of 10th Subjects are = "<<avg; per=(tot*100)/600; cout<<endl<<"\t-->\tPesentage of 10th Subjects are = "<<per; cout<<" %";

Page 8: C++ practical lab

Chapter: 1Chapter: 1Chapter: 1Chapter: 1

Programming whit C++

--------------------------------------------------------------------------------------------------------------------------------------------------------- Submitted to: Mr.Kamran Khan

Submitted by: Padshah Gul Course: Bs-Telecom Page No: - 5 of 52

getch(); }

Creating Design of ATM Machine Display in C++:

#include <iostream.h> #include <conio.h> void main (void) { clrscr(); cout<<endl<<" ,-*^ *-._.-*^*-,"<<endl; --->(34)Space cout<<" ( ATM MACH INE )"<<endl; --->(33)Space cout<<" ` '"< <endl; --->(34)Space cout<<" `-.,,_,HBL,_,,.-'"< <endl; --->(34)Space cout<<" *-._.-* "<<end l; --->(39)Space cout<<" "<<endl; -- ->(42)Space cout<<" 0-----------0 0-----------0"<<endl; ---(13-30)? cout<<" I ONLINE I I ACCONT NO I"<<endl; cout<<" 0-----------0 0-----------0"<<endl; cout<<endl<<" 0-----------0 0-----------0"<<endl; cout<<" I CASH.WITH I I PAYMENT I"<<endl; cout<<" 0-----------0 0-----------0"<<endl; cout<<endl<<" 0-----------0 0-----------0"<<endl; cout<<" I BILL TRMS I I OTHERS I"<<endl; cout<<" 0-----------0 0-----------0"<<endl; cout<<" THANKS"<<endl; ---(42 ) cout<<" =============================== ========================="<<endl; ---(13-56) cout<<" Submitted by: - Pad shah Gul"<<endl; cout<<" Submitted to: - Mr. Kamran khan"<<endl; cout<<" Submitted Date :- 2 0/11/2012"<<endl; cout<<endl<<" Sarhad University, Departmen t of Computer Science, Bs.Telecom"; -- (10) getch();

Page 9: C++ practical lab

Chapter: 1Chapter: 1Chapter: 1Chapter: 1

Programming whit C++

--------------------------------------------------------------------------------------------------------------------------------------------------------- Submitted to: Mr.Kamran Khan

Submitted by: Padshah Gul Course: Bs-Telecom Page No: - 6 of 52

}

Time Converter:

#include <iostream.h> #include <conio.h> void main (void) { clrscr(); float age,month,day,hour,min; long int sec; cout<<"Enter your age in Years = "; cin>>age; month=age*12; cout<<endl<<"Your age in Months = "<<month; day=age*365; cout<<endl<<"Your age in Days = "<<day; hour=age*8766; cout<<endl<<"Your age in Hours = "<<hour; min=age*525960; cout<<endl<<"Your age in Minutes = "<<min; sec=age*31557600; cout<<endl<<"Your age in Second = "<<sec; getch(); }

Length Converter:

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

Page 10: C++ practical lab

Chapter: 1Chapter: 1Chapter: 1Chapter: 1

Programming whit C++

--------------------------------------------------------------------------------------------------------------------------------------------------------- Submitted to: Mr.Kamran Khan

Submitted by: Padshah Gul Course: Bs-Telecom Page No: - 7 of 52

clrscr(); float k,m,cm; cout<<endl<<endl<<"\t\t\t\Length Converter"<<endl; cout<<"\t\t\t\t"; cout<<endl<<endl<<" 0==========0 0==========0"<<endl; cout<<" l Peshawar l-----------There is distance B /W two city-----------l Nowshera l"<<endl; cout<<" 0==========0 0==========0"; cout<<endl<<endl<<"\t\t\t\tKilometer = "; cin>>k; m=k*1000; cout<<endl<<"\t\t\t\tMeter = "<<m; cm=k*100000; cout<<endl<<"\t\t\t\tCentimeter = "<<cm; cout<<endl<<"====================================== =========================================="; cout<<"\t\t\t\tMeter = "; cin>>m; k=m/1000; cout<<endl<<"\t\t\t\tKilometer = "<<k; cm=m*100; cout<<endl<<"\t\t\t\tCentimeter = "<<cm; cout<<endl<<"====================================== =========================================="; cout<<"\t\t\t\tCentimeter = "; cin>>cm; k=cm/100000; cout<<endl<<"\t\t\t\tKilometer = "<<k; m=cm/100; cout<<endl<<"\t\t\t\tMeter = "<<m; cout<<endl<<endl<<"\t\t\t\t*Thank You*"; getch(); }

Page 11: C++ practical lab

Chapter: 1Chapter: 1Chapter: 1Chapter: 1

Programming whit C++

--------------------------------------------------------------------------------------------------------------------------------------------------------- Submitted to: Mr.Kamran Khan

Submitted by: Padshah Gul Course: Bs-Telecom Page No: - 8 of 52

Write a program to calculate Average age of a class of 10th students:

#include <iostream.h> #include <conio.h> void main (void) { clrscr(); float avg; int a,b,c,d,e,f,g,h,i,j; cout<<endl<<endl<<"\t Enter the Age of 1st Student = "; cin>>a; cout<<endl<<"\t\Enter the Age of 2nd Student = "; cin>>b; cout<<endl<<"\t\Enter the Age of 3rd Student = "; cin>>c; cout<<endl<<"\t\Enter the Age of 4th Student = "; cin>>d; cout<<endl<<"\t\Enter the Age of 5th Student = "; cin>>e; cout<<endl<<"\t\Enter the Age of 6th Student = "; cin>>f; cout<<endl<<"\t\Enter the Age of 7th Student = "; cin>>g; cout<<endl<<"\t\Enter the Age of 8th Student = "; cin>>h; cout<<endl<<"\t\Enter the Age of 9th Student = "; cin>>i; cout<<endl<<"\t\Enter the Age of 10th Student = "; cin>>j; avg=(a+b+c+d+e+f+g+h+i+j)/10; cout<<endl<<"\t\Average the Age of 10 Students are = "<<avg; getch(); }

Page 12: C++ practical lab

Chapter: 1Chapter: 1Chapter: 1Chapter: 1

Programming whit C++

--------------------------------------------------------------------------------------------------------------------------------------------------------- Submitted to: Mr.Kamran Khan

Submitted by: Padshah Gul Course: Bs-Telecom Page No: - 9 of 52

OR

#include <iostream.h> #include <conio.h> void main (void) { clrscr(); float avg; int a,b,c,d,e,f,g,h,i,j; a=24; b=23; c=25; d=38; e=18; f=14; g=31; h=30; i=37; j=19; avg=(a+b+c+d+e+f+g+h+i+j)/10; cout<<"Average the Age of 10 Students are = "<<avg ; getch(); }

INCREMENT:

Prefix (++i):

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

Page 13: C++ practical lab

Chapter: 1Chapter: 1Chapter: 1Chapter: 1

Programming whit C++

--------------------------------------------------------------------------------------------------------------------------------------------------------- Submitted to: Mr.Kamran Khan

Submitted by: Padshah Gul Course: Bs-Telecom Page No: - 10 of 52

float a, b, c, sum; a=7; b=6; c=5; sum=a+b+(++c); cout<<"Sum of A+B+C ="<<sum; cout<<"\tC="<<c; getch(); } Output of Program: Sum=19 C=6

Postfix (i++):

#include <iostream.h> #include <conio.h> void main (void) { clrscr(); float a, b, c, sum; a=7; b=6; c=5; sum=a+b+(c++); cout<<"Sum of A+B+C ="<<sum; cout<<"\tC="<<c; getch(); } Output of Program: Sum=18

Page 14: C++ practical lab

Chapter: 1Chapter: 1Chapter: 1Chapter: 1

Programming whit C++

--------------------------------------------------------------------------------------------------------------------------------------------------------- Submitted to: Mr.Kamran Khan

Submitted by: Padshah Gul Course: Bs-Telecom Page No: - 11 of 52

C=6

DECREMENT:

Prefix (--i):

#include <iostream.h> #include <conio.h> void main (void) { clrscr(); float a, b, c, sum; a=4; b=10; c=7; sum=a+b+(--c); cout<<"Sum of A+B+C ="<<sum; cout<<"\tC="<<c; getch(); } Output of Program: Sum=20 C=6

Postfix (i--):

#include <iostream.h> #include <conio.h> void main (void) { clrscr(); float a, b, c, sum; a=4;

Page 15: C++ practical lab

Chapter: 1Chapter: 1Chapter: 1Chapter: 1

Programming whit C++

--------------------------------------------------------------------------------------------------------------------------------------------------------- Submitted to: Mr.Kamran Khan

Submitted by: Padshah Gul Course: Bs-Telecom Page No: - 12 of 52

b=10; c=7; sum=a+b+(c--); cout<<"Sum of A+B+C ="<<sum; cout<<"\tC="<<c; getch(); } Output of Program: Sum=21 C=

Page 16: C++ practical lab

Chapter: Chapter: Chapter: Chapter: 2222

Programming whit C++

--------------------------------------------------------------------------------------------------------------------------------------------------------- Submitted to: Mr.Kamran Khan

Submitted by: Padshah Gul Course: Bs-Telecom Page No: - 13 of 52

Condition Statements:

IF CONDITION:

#include <iostream.h> #include <conio.h> void main (void) { clrscr(); float age1,age2; age1=12; age2=18; if(age1>age2) cout<<"Student-1 is Older"<<endl; if(age2>age1) cout<<"Student-2 is Older"; getch(); }

IF CONDITION:

#include <iostream.h> #include <conio.h> void main (void) { clrscr(); int a,b; a=100; b=50; if(a>b)//if a>b than show Islamabad the statement i s true or Islamabad not showed the function is fals e cout<<"Islamabad"<<endl; cout<<"ok"<<endl; getch(); }