Lab Report 03
Lab Report 03
Lab Report 03
Lab Report 01
Title:
Program Structure in C++ and different
Data types
1ST SEMESTER
Theory:
Basic C++ Program structure:
• Pre processor directives:
They are the set of instruction given to the compiler
before program initiation. They are also called compiler directives. They add up special
instructions in a C++ program at time of compilation.
That include #include and define.
• Header files:
Header files are C++ source files that define library objects/functions.
They are usually declared after the preprocessor directives. They are included in the
program if the function used in the program is associated with a library.
There are different type of header files in C++ each having specific
library. They are declared by entering header file name in angular brackets < > after the
preprocessor directives. Such as
#include<iostream> has included input/ output stream header
file.
• Main ( ) function:
Main function is used to indicate start of a C++ program. The
statements that are to be included in the program are written in curly brackets { } after the
main function statement.
The control moves towards the main function first at the
execution of the program.
• C++ Statements:
C++ statements are included in the curly brackets of main
function. They end up with a semi colon ; and they are the body of the program.
• Keywords:
Keywords are the system reserved words in the C++ program. They are
assigned special functions and they can’t be declared as variable names.
For example int, float, amin are system reserved variables.
• Tokens:
Tokens are the characters, preprocessor directives, variables and operators
etc used in a C++ statement
In statement #include<iostream>
int main( ) { There is include directive, iostream header and main
function with {<>()} punctuation marks.
• Variables :
Variables are the quantities whose values can be changed during program
execution.
They refer to memory storage or location in system memory. They have a
fixed name but there value can be changed during program.
• Data Types:
There are 5 main data types in C++
1. Int
2. Float
3. char
4. bool
5. double
• Declaration of variable:
Assigning a name and value to a variable is called
declaration of the variable. For this purpose, a statement a declaring statement is written
for the variable to get its value assigned.
For example int a ; shows a variable has an integer data type
• Initialization:
When the variable is declared, a specific memory location is assigned
to it and it gets access to its value. This pres assigned value if not used by mistake can
cause problems in the program. For this purpose, sometimes a value is assigned to
variable when it is declared. It is call initialization.
For example float b=100, shows b float type is given value of 100.
• Constants:
They are the quantities whose values are fixed and they don’t change
during the program execution.
• Const qualifier:
The data item followed by the const term has a specific value
assigned to it which cant be changed during program execution.
• Define directive:
It is a preprocessor directive. It is used to define a constant
quantity.
For example #define identifier constant shows that the character has been assigned a
constant value and constant indicates the value which is assigned.
• Arithmatic Operators:
They are the symbols to perform mathematical operations.
They are used to relate two quantities and operation is applied between them. The
whole statement is called arithmetic expression.
For example ans = a*b;
Questions
1. What are preprocessor directives. Explain with examples.
Ans : The preprocessor directives are the set of instruction given to the compiler
before the compilation of the program. These instructions add up useful codes during
program execution. They are usually written at start of the program (before main( )).
They are written as #include or #define and then a header file
is added afterwards.
2. What is a variable? Write rules to write the variable
Ans : Variable :
A variable is a quantity whose value can be changed during the
program execution.
It is a memory location or storage in system memory. The variable
are given a specific data type and a name by a declaration statement. The name of the
variable is fixed but not its value.
Rules to write its name:
• Its first letter should be an alphabet.
• Underscore can also be used as the first letter of the variable.
• Its cant start with a number.
• No special character can be included in the name of the alphabet such as #
• Variable cant be assigned a name similar to a C++ keyword or reserved words.
• Blank spaces cant be included in the name of the variable.
• A variable used to declare one data type cant be used to declare another.
Lab Task:
1. Program to calculate radius of the circle
2. //To Calculate radius of circle//
3.
4. #include<iostream>
5. using namespace std;
6.
7. int main(){
8.
9. float rad,pi,ans;//declaration
10. cout<<"Enter the value of Radius of circle :";
11. cin>>rad;//input radius
12. cout<<"Applying Formula";
13.
14. pi=3.142857;
15. ans=pi*rad*rad;
16. cout<<"\nThe radius of circle is "<<ans;
17.
18. }
#include<iostream>
using namespace std;
int main(){
float dol,rup;
cout<<"Enter the amount in Rupees :";
cin>>rup;
dol=0.01*rup;
cout<<"The Amount in Dollars(USD) is " <<dol<<" USD.";
3. Program to put your name, address and age and print it on the screen.
4. //Program to print name,age and address//
5. #include<iostream>
6. #include<string>
7. using namespace std;
8.
9. int main(){
10. string nam,addr;
11. int age;
12. cout<<"Please enter your Name :";
13. getline(cin,nam);
14. cout<<"\nEnter your Address :";
15. getline(cin,addr);
16. cout<<"\nPlease enter your Age :";
17. cin>>age;
18. cout<<"\n*************MY INTRODUCTION**************";
19. cout<<"\nMy name is "<<nam<<" ."<<"\nMy Address is "<<addr<<"
."<<"\nMy Age is "<<age<<" .";
20. }
21.
4. Program to convert years age into months, days, hours, minutes and seconds
5. //Program to convert age into months,days,hours and seconds
6. #include<iostream>
7. #include<cmath>
8. using namespace std;
9.
10. int main(){
11.
12. float yr,day,mon,hr;
13. float min,sec;//Declaration
14. cout<<"Please Enter your Age in Years :";
15. cin>>yr;
16. mon=12*yr;
17. cout<<"Your Age in Months is "<<mon<<" months.";
18. day=365*yr;
19. cout<<"\nYour Age in Days is "<<day<<" days.";
20. hr=8760*yr;
21. cout<<"\nYour Age in Hours is "<<hr<<" hrs.";
22. min=525600*yr;
23. cout<<"\nYour Age in Minutes is "<<min<<" min.";
24. sec=3.1536*pow(10,7)*yr;
25. cout<<"\nYour Age in Seconds is "<<sec<<" sec.";
26.
27. }