[go: up one dir, main page]

0% found this document useful (0 votes)
14 views23 pages

Hssreporter - Com - 06 Introduction To Programming

Introduction to Programming

Uploaded by

Jithin S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views23 pages

Hssreporter - Com - 06 Introduction To Programming

Introduction to Programming

Uploaded by

Jithin S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Chapter 6

Downloaded from hssreporter.com


Structure of a C++ program
#include<iostream>
using namespace std;
int main()
{
cout <<“My first program in C++”;
return 0;
}

Downloaded from hssreporter.com


Pre-processor Directives
 These are statements which gives instruction to
compiler to process the information before actual
program compilation starts.
 Eg:
 #include
 #define
 #undef

 (NB: No Semi colon(;) required for Pre-processor


directive statements)

Downloaded from hssreporter.com


Header files
 Header files contains the information about functions,
objects, data types etc.
 Eg: iostream, cstring, cmath
 iostream file contains information about functions cin
and cout
 Usage:
 #include <iostream>

Downloaded from hssreporter.com


Concept of namespace
 using namespace std;
 The above statement is used to set the namespace as
std(standard) in which cin, cout objects are defined.
 If it is not added in code,we must use std::cin or
std::cout for input/output functions.

Downloaded from hssreporter.com


The main function
 Execution of a c++ program starts from main() and
ends within main().
 All statements are written inside the main function.
 C++ is a free form language since it is not necessery to
write each statement in new line.

int main()
{
Statement codes;
}

Downloaded from hssreporter.com


Sample program
#include<iostream>
using namespace std;
int main()
{
cout <<“Hello, Welcome to C++”;
return 0;
}

Downloaded from hssreporter.com


Guide lines for coding
1. Use suitable naming convention for identifiers:
(it is better to use salary=amount-tax instead a = b – c)

2. Use clear and simple expression


3. Use Comments wherever needed:
1. Single line Comment //………………………
2. Multiline comment /*………………..
1. …….………*/
4. Use Relevant indentation(Spacing):
 use proper spacing between codes makes it better to
communicate

Downloaded from hssreporter.com


Variable initialisation
 Variables are identifiers of memory location.
 That is, named storage location in memory where the
actual values are stored.
 Supplying value to a variable at the time of declaration is
called variable initialisation.
 Eg: int a = 10;
 A variable can also be initialised during execution of the
program and is known as dynamic initialisation.
 Eg: float product = x * y;

Downloaded from hssreporter.com


Const – The access modifier
 The keyword const is used to create symbolic
constants whose value can never be changed during
execution.
 Eg: const float pi = 3.14;
 Here the value of pi cannot be changed during execution.

Downloaded from hssreporter.com


Type Modifiers
 Type modifiers are used to alter the size, range, and
precision of data types.
 The type modifiers of C++ are
 signed
 unsigned
 long
 short
 Eg:- long double salary; short int age;
 double can store upto 8 Bytes.
 long double can store upto 10 bytes
 int - 4 Byte
 short int 2 Bytes

Downloaded from hssreporter.com


Arithmetic assignment operators
 An arithmetic operator(+,-,*,/,%) and Assignment
operator(=) can be mixed to form arithmetic
assignement operators.
 They are +=, -+, *=, /=, %=

 Eg: a = a + 10 can be represented as a += 10


 p=p * 10 can be represented as p *= 10
 x = x / 10 can be represented as x /= 10
 Advantage – faster than the usual method

Downloaded from hssreporter.com


Increment / Decrement Operators
 Increment operator (++)
 increment operator is used for incrementing the content by
one.
 Eg: ++x (pr increment), x++ (post increment)
 Both are equivalent to x = x + 1
 Decrement operator (++)
 decrement operator is used for decrementing the content by
one.
 Eg: --x (pre decrement), x-- (post decrement)
 Both are equivalent to x = x - 1

Downloaded from hssreporter.com


Prefix and Postfix form
 There are two forms for increment and decrement
operators:
 postfix (a++ and b --)
 prefix (++a and --b).
 The statement c=a++; is equivalent to the statement
sequence c=a; a=a+1; (use then change method)
 But the statement b=++a; is equivalent to the statement
sequence a=a+1; c=a; (change then use method)
 Eg: a =5; b= 10;
c = ++a; Here the value of c will be 6.
c = a++; Here the value of c will be 5.
c = --b; Here the value of c will be 9.
c = b--; Here the value of c will be 10.

Downloaded from hssreporter.com


Precedence of operators
 ++ (post increment), -- (post decrement)
 ++ (pre increment), -- (pre decrement), sizeof(), ! (not), - (unary
minus), + (unary plus)
 (multiply), / (divide), % (modulus)
 + (add), -(subtract)
 < (less than), <= (less or equal), > (greater than), >= (greater than or
equal)
 = = (equal), ! = (not equal)
 && (Logical AND)
 || (logical OR)
 ? : (Conditional expression)
 =(simple assignment) and arithmetic assignment opetors (+=, -
=,*=,/=,%=)
 , (comma operator)

Downloaded from hssreporter.com


Type Conversion
 In expressions where different types of data are
involved, the data type of one operand will be
converted to another. It is called type conversion.

 Type conversion is performed in two ways


1. Implicit Conversion (Type Promotion)
2. Explicit Conversion (Type Casting)

Downloaded from hssreporter.com


Implicit Conversion
 Implicit conversion is performed by C++ compiler
internally.
 Here C++ converts the lower sized operands to the
higher sized operands.
 Since conversion is from lower to higher, it is called
type promotion.
 Eg: 5 / 2 * 3 + 2.5
 Step 1: 5 / 2 = 2 (Integer division)
 Step 2: 2 * 3 = 6 (Integer multiplication)
 Step 3: 6 + 2.5 = 8.5 (floating point addition, 6 is
converted to 6.0)

Downloaded from hssreporter.com


Explicit type conversion
(type casting)
 Here the programmer is responsible for the conversion to a
specific type.
 Programmer explicitly casts the data to the desired data
type.
int a=5, b=2;
float ans;
ans=a/b;
cout << ans;
 Here the result become 2.0
 To get the floating point result,
 ans = (float)a / b; or ans = a / (float)b;
 Then the result become 2.5

Downloaded from hssreporter.com


Sample Programs
Write a program to find sum and average of 3 numbers

#include<iostream>
using namespace std;
int main()
{
int a, b, c, sum, avg;
cout<<"Enter 3 Numbers:";
cin >> a >> b >> c;
sum = a +b + c;
avg = sum / 3;
cout<<"Sum="<<sum;
cout<<"\nAverage="<< avg;
return 0;
}
Downloaded from hssreporter.com
Program to find simple interest
#include<iostream>
using namespace std;
int main()
{
int p, n, r, si;
cout << "Enter Principal Amount:";
cin >> p;
cout << "Enter Number of years:";
cin >> n;
cout << "Enter Rate of Interest:";
cin >> r;
ci = p*n*r/100;
cout << "Simple interest="<< si;
return 0;
}

Downloaded from hssreporter.com


Program to print ASCII code of a
inputted number
#include<iostream>
using namespace std;
int main()
{
int a;
char ch;
cout<<"ENTER A NUMBER:";
cin>>a;
ch=a;
cout<<"ASCII VALUE OF "<<a<<"="<<ch;
return 0;
}

Downloaded from hssreporter.com


To find the area and perimeter of a circle
#include<iostream>
using namespace std;
int main()
{
const float pi = 3.14;
float radius, area, perimeter;
cout << “Enter the Radius”;
cin >> radius;
area = pi * radius * radius;
perimeter = 2 * pi * radius;
cout << “Area = “ << area;
cout << “Perimeter = “ << perimeter;
return 0;
}

Downloaded from hssreporter.com


Program to find biggest from 2 numbers by using
conditional operator
#include<iostream>
using namespace std;
int main()
{
int a, b, big;
cout<<"Enter two numbers:";
cin>>a>>b;
big=(a>b)?a:b;
cout<<"Biggest number is: "<<big;
return 0;
}

Downloaded from hssreporter.com

You might also like