0% found this document useful (0 votes)
13 views

Practical 1: Introduction of C++ Programming: Firstly, We Know About WHAT IS C++ PROGRAMMING LANGUAGE ?

This document provides an introduction to C++ programming. It discusses what C++ is, how it supports object-oriented programming, and some basic concepts like using cout and cin for input/output as well as header files like iostream. It then covers algorithms and flowcharts, basic programs, conditional statements like if/else, loops, arrays, and pointers. Practical examples are provided for each topic to demonstrate C++ code and output.

Uploaded by

Simran Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Practical 1: Introduction of C++ Programming: Firstly, We Know About WHAT IS C++ PROGRAMMING LANGUAGE ?

This document provides an introduction to C++ programming. It discusses what C++ is, how it supports object-oriented programming, and some basic concepts like using cout and cin for input/output as well as header files like iostream. It then covers algorithms and flowcharts, basic programs, conditional statements like if/else, loops, arrays, and pointers. Practical examples are provided for each topic to demonstrate C++ code and output.

Uploaded by

Simran Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

PRACTICAL 1: INTRODUCTION OF C++ PROGRAMMING

Firstly, we know about WHAT IS C++ PROGRAMMING LANGUAGE ?


 C++ is a general-purpose programming language that was developed as an extension
of the C programming language.
 It was develop by Bjarne Stroustrup in the early 1980s at Bell Labs
 In c ++ programming language , it was the addition of object in this type of
programming language.
 The Programs written in C++ need to be translated into machine code before they
can be executed.
 The most popular programming languages due to its versatility and efficiency.
 IT IS COMBINATION FOR BOTH LEVEL LANGUAGES (low level and high level)

Object-Oriented Programming (OOP): C++ supports the principles of OOP, developers


Organized the code into classes and objects.

Basic important points before to make a innovative program:

 Cout: In C++, cout is an object used for standard output, which is typically the
console or terminal.
 Cin: In c++ , cin is an object used for standard input, which is typically the keyboard.
 HEADER FILES ARE USED :
IOSTREAM: 1. Iostream header is used to read input from the user.
2. Iostream header is also in used to display the data.
PRACTICAL 2: ALGORITHM AND FLOWCHART
ALGORITHM: Algorithm is a set of well-defined instructions to solve a particular problem. It
takes a set of input(s) and produces the desired output.
IMPORTANT POINTS:
1. Each step in the algorithm should be clear.
2. Input and output is precise.
3. Normal English is used in it.
4. It is effective code to solve the problems.
EXAMPLE ARE :
Ques To solve the problem of addition of two numbers:

STEP1: START
STEP2: Input a,b
STEP3: SUM= a+b
STEP4: Output SUM
STEP5: End

FLOWCHART: A flowchart is a diagrammatic representation of an algorithm. A


flowchart can be helpful for both writing programs and explaining the program to
others.

Qualities of flowchart
 Effective way to represent the code.
 Diagrams make easier the beginneers to understand
the codes.
 Diagrams are best way to make a simple program.
 Normal English is apply inside the diagrams.
Here, the following table are given:
EXAMPLE:
START

Input a,b

SUM= a+b

SUM

END
PRACTICAL 3: BASIC PROGRAM
Write a program to get text message.

Source of code :
#include<iostream.h>
#include<conio.h>
void main()
{
char name;

cout<<"javastpoint";
cin>>name;
getch();
}
OUTPUT:
PRACTICAL 4: WRITE A PROGRAM OF SUM OF NUMBERS.
SOURCE OF CODE:

#include<iostream.h>

#include<conio.h>
void main()
{
clrscr();
int a,b,c,d,e,f,sum;

cout<<"enter any 6 number";


cin>>a>>b>>c>>d>>e>>f;
sum=a+b+c+d+e+f;
cout<<"sum is"<<sum;
getch();

}
OUTPUT:
PRACTICAL 5:
CONDITIONAL CONTROL STATEMENTS:
 The conditional statements (also known as decision control structures) such as if, if
else, switch, etc. are used for decision-making purposes in C++ programs.
 They are also known as Decision-Making Statements and are used to evaluate one or
more conditions and make the decision whether to execute a set of statements or
not.
 There are 4 types of conditional statements or decision-making statements in C
language:

if Statement
if-else Statement
if-else-if Ladder
switch Statement
1. IF STATEMENT :
The if statement is a program control statement that is used to execute a part of code
based on some condition.

Advantages of if Statement :
Following are the main advantages of the if statement in C:

 It is the simplest decision-making statement.


 It is easy to use and understand.
 It can evaluate expressions of all types such as int, char, bool, etc.

WRITE A PROGRAM OF IF STATEMENT ONE NUMBER IS GREATER THAN OTHER ?

SOURCE OF CODE:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();

int a,b,c,d,e,f,sum;
cout<<"enter any 6 number";
cin>>a>>b>>c>>d>>e>>f;
sum=a+b+c+d+e+f;
cout<<"sum is"<<sum;

getch();
}
OUT PUT:

2. IF ELSE STATEMENT:
 if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition is false it won’t.
 We can use the else statement with the if statement to execute a block of code
when the condition is false.
 The if-else statement consists of two blocks, one for false expression and one for
true.
Write a program of if else statement ?

Source of code:
#include<iostream.h>

#include<conio.h>
void main()
{
clrscr();
int a,b;

cout<<"enter the value of a";


cin>>a;
cout<<"enter the value of b";
cin>>b;
if(a>b)

{
cout<<"a is greatest";
}
else
{

cout<<"b is greatest";
}
getch();
}

OUTPUT:

3. LADDER ELSE IF STATEMENT:


 if else if ladder in C programming is used to test a series of conditions
sequentially.
 if a condition is tested only when all previous if conditions in the if-else ladder
are false. If any of the conditional expressions evaluate to be true, the
appropriate code block will be executed, and the entire if-else ladder will be
terminated.

Write a program of ladder elseif statements:

Source of code :
#include<iostream.h>
#include<conio.h>
void main()
{

clrscr();
int a,b,c;
cout<<"enter value of a";
cin>>a;
cout<<"enter value of b";

cin>>b;
cout<<"enter the value of c";
cin>>c;
if(a>b>c)
{
cout<<"a is largest";
}
else if(b>a && b>c)
{

cout<<"b is largest";
}
else
{
cout<<"c is largest";

}
getch();
}
OUTPUT:

4. SWITCH STATEMENT:
The switch statement in C++ is a flow control statement that is used to execute the
different blocks of statements based on the value of the given expression. We can create
different cases for different values of the switch expression. We can specify any number
of cases in the switch statement but the case value can only be of type int or char.

SOURCE OF CODE :
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int day;
cout<<"enter number between 1-7=";
cin>>day;
switch(day)
{case1:
cout<<"today is monday";
break;
case2:
cout<<"today is tuesday";
break;
case3:
cout<<"today is wednesday";
break;
case4:
cout<<"today is thursday";
break;
case5:
cout<<"today is friday";
break;
case6:
cout<<"today is saturday";
break;
case7:
cout<<"today is sunday";
break;
default:
cout<<"invalid input";
getch();
}

OUTPUT:
PRACTICAL 6: LOOPS
LOOPS : Loops in programming are used to repeat a block of code until the specified
condition is met. A loop statement allows programmers to execute a statement or group of
statements multiple times without repetition of code.
TYPES OF LOOPS:

 FOR LOOP
 WHILE LOOP
 DO WHILE LOOP
1. FOR LOOP: is a repetition control structure that allows programmers to write a loop
that will be executed a specific number of times. for loop enables programmers to
perform n number of steps together in a single line.
SOURCE OF CODE :
#include<iostream.h>
#include<conio.h>
void main()

{
clrscr();
int i;
for(i=1;i<=15;i++)
{

cout<<i;
}
getch();
}
OUTPUT:
2. WHILE LOOP: In Entry controlled loops the test condition is checked before entering
the main body of the loop. While Loop is Entry-controlled loops.

SOURCE OF CODE :
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
while(i<=15)
{
cout<<i;
i=i++;
}
getch();
}
OUTPUT:

DO WHILE: In Exit controlled loops the test condition is evaluated at the end of the loop
body. The loop body will execute at least once, irrespective of whether the condition is true
or false. do-while Loop is Exit Controlled loop.
SOURCE OF CODE:
#include<iostream.h>
#include<conio.h>
void main()

{
clrscr();
int i=1;
do
{

cout<<i;
i=i++;
}
while(i<=5
);

getch();
}
OUTPUT:
PRACTICAL 7: ARRAY
ARRAY : An array is a collection of items of same data type stored at contiguous memory
locations.
TYPES OF ARRAY:
1. 1D( ONE DIMENSIONAL) – when the single subscript is used in the program. This is
called one dimensional.
2. Multi dimensional ( 2D,3D etc….) – when the two or more subscript are used in the
program. This is called are two dimensional.

1.ONE DIMENSIONAL ARRAY:


SOURCE OF CODE:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[2]={22,3};
cout<<a[0]<<endl;
cout<<a[1];
getch();
}OUTPUT:
2.TWO DIMENSIONAL ARRAY:

SOURCE OF CODE:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int row_size,col_size;
cout<<"Enter the row size of the matrix";
cin>>row_size;
cout<<"Enter the col size of the matrix";
cin>>col_size;
int[10][10];
int i,j;
cout<<"Enter the elements of matrix";
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;i++)
{
cin>>matrix[i][j];
}
}
getch();
}

OUTPUT:

PRACTICAL 8: POINTER
DEFINATIONS: A pointer can be used to store the memory address of other
variables, functions, or even other pointers.
SYNTAX:
The syntax of pointers is similar to the variable declaration in C, but we use the ( * )
dereferencing operator in the pointer declaration.

DATATYPE *ptr
Where,
 ptr is the name of the pointer.
 datatype is the type of data it is pointing to.

SOURCE OF CODE :
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num=30;
int *p;
p=&num;
cout<<"address of num"<<&num<<endl;
cout<<"address of p"<<&p<<endl;
cout<<"value of p"<<*p<<endl;
getch();
}

OUTPUT:
PRACTICAL 9: STRUCTURE
STRUCTURE:
 STRUCTURE are user defined data types which are used to store
group of items of non-similar data types.
 A structure creates a data type that can be used to group items of
possibly different types into a single type.
 The ‘struct’ keyword is used to create a structure.

SYNTAX:
struct structureName
{
member1;
member2;
member3;
.
.
.
memberN;
};
SOURCE OF CODE :
#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int i;
struct student

{
int roll;
char name[30];
float marks;
};
struct student s[5];
for(i=0;i<=5;i++)
{

cout<<"enter roll";
cin>>s[i].roll;
cout<<"enter name";
cin>>s[i].name;
cout<<"enter marks";

cin>>s[i].marks;
}
for(i=0;i<5;i++)
{
cout<<"roll"<<s[i].roll;

cout<<"name"<<s[i].name;
cout<<"marks"<<s[i].marks;
}getch

OUTPUT:
PRACTICAL 10: CLASSES
CLASSES: is the building block that leads to Object-Oriented programming.(OOP)
 A C++ class is like a blueprint for an object.
 An Object is an instance of a Class. When a class is defined, no memory is
allocated but when it is instantiated (i.e. an object is created) memory is
allocated.

SOURCE OF CODE:
#include<iostream.h>
#include<conio.h>
class student
{
public:
int rollno;
char name[20];
float marks;
};
void main()
{
student s1;
cout<<"Enter your rollno";
cin>>s1.rollno;
cout<<"Enter your name";
cin>>s1.name;
cout<<"Enter your marks";
cin>>s1.marks;
cout<<s1.rollno<<endl;
cout<<s1.name<<endl;
cout<<s1.marks;
getch();
}

OUTPUT:

You might also like