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

Course C++

This document provides an overview of variables in C++. It defines what a variable is, how to declare variables, and some basic rules for naming variables. It explains that a variable is a name given to a memory location used to store values. It also discusses declaring single and multiple variables, initializing variables, and some valid and invalid variable name examples. The key points are that variables store values in memory locations, they are declared with a data type, and there are naming convention rules for variables in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Course C++

This document provides an overview of variables in C++. It defines what a variable is, how to declare variables, and some basic rules for naming variables. It explains that a variable is a name given to a memory location used to store values. It also discusses declaring single and multiple variables, initializing variables, and some valid and invalid variable name examples. The key points are that variables store values in memory locations, they are declared with a data type, and there are naming convention rules for variables in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

2- Basic: Output

- C++ output In C++, cout sends formatted output to standard output


devices, such as the screen. We use the cout object along with the
<< operator for displaying output.

practical

Task1: Write C++ program to print the following figure

How program works?


• We first include the iostream header file that allows us to display
output.
• The cout object is defined inside the std namespace. To use the std
namespace, we
used the using namespace std; statement.
• Every C++ program starts with the main() function. The code execution
begins from the
start of the main() function.
• cout is an object that prints the string inside quotation marks " ". It is
followed by the <<
operator.
• return 0; is the "exit status" of the main() function. The program ends
with this
statement, however, this statement is not mandatory.
- Note: If we don't include the using namespace std; statement,
we need to
use std::cout instead of cout

Variable in C++
Variables in C++ is a name given to a memory location. It is the
basic unit of storage in a program.

 The value stored in a variable can be changed during


program execution.
 A variable is only a name given to a memory location, all the
operations done on the variable effects that memory
location.
How to Declare Variables?
 Declaring a single variable

type variable_name;
 Declaring multiple variables:
type variable1_name, variable2_name, variable3_name;

Initialization of a variable in C++

In the above diagram,


datatype: Type of data that can be stored in this variable.
variable_name: Name given to the variable.
value: It is the initial value stored in the variable.
Rules For Declaring Variable
 The name of the variable contains letters, digits, and underscores.
 The name of the variable is case sensitive (ex Arr and arr both are different).
 The name of the variable does not contain any whitespace and special
characters (ex #,$,%,*, etc).
 All the variable names must begin with a letter of the alphabet or an
underscore(_).
 We cannot used C++ keyword(ex float,double,class)as a variable name.

Valid variable names: Invalid variable names:


int x; //can be letters int 89;// Should not be a number

int _yz; //can be underscores int a b; //Should not contain any whitespace
int z40;//can be letters int double;// C++ keyword CAN NOTBEUSED

note:

Example:
// defining integer constant using const keyword

const int int_const = 25;


// defining character constant using const keyword
const char char_const = 'A';

- C++ input
In C++, cin takes formatted input from standard input devices such as the keyboard. We use the cin object
along with the >> operator for taking input

- Note:
If we don't include the using namespace std; statement, we need to
use std::cin instead of cin

practical
1- Data Types

2- Modified Data Types


if, if...else and Nested if…else

- C++ if Statement
if (condition) { // body of if statement }

- C++ if ..else Statement


if (condition) {
// block of code if condition is true}
else {
// block of code if condition is false}
- C++ if...else...else if statement
if (condition1) {
// code block 1 }
else if (condition2){
// code block 2 }
else {
// code block 3 }
- C++ nested if statement
if (condition1) {
if (condition2)
{ // code block2}
else
{ // code block 3 }

else{ // code block4 }


practical

1. Write a program that reads in five integers and determines and prints the largest integers in
the group.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, num3, num4, num5, largest,
smallest;
cout << "Enter five integers: ";
cin >> num1 >> num2 >> num3 >> num4 >> num5;
largest = num1;
if ( num1 > largest )
largest = num1;
if ( num2 > largest )
largest = num2;
if ( num3 > largest )
largest = num3;
if ( num4 > largest )
largest = num4;
if ( num5 > largest )
largest = num5;
cout << "Largest is " << largest ;
return 0;
}
2. Write a program that reads in two integers and determines and prints if the first is a
multiple of the seconds operator.)
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
if ( num1 % num2 == 0 )
cout << num1 << " is a multiple of " << num2
<< endl;
else
cout << num1 << " is not a multiple of " <<
num2 << endl;
return 0;
}
Write a Program to check whether an integer is positive, negative or zero

#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
}
else if (number < 0) {
cout << "You entered a negative integer: " << number << endl;
}
else {
cout << "You entered 0." << endl;
}
cout << "This line is always printed.";
return 0;
}
Write a program to find if an integer is positive, negative or zero using nested if
statements

#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter an integer: ";
cin >> num;
// outer if condition
if (num != 0) {
// inner if condition
if (num > 0) {
cout << "The number is positive." << endl;
}
// inner else condition
else {
cout << "The number is negative." << endl;
}
}
// outer else condition
else {
cout << "The number is 0 and it is neither positive nor negative." << endl;
}
cout << "This line is always printed." << endl;
return 0;
}
Switch
What is a 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.
Rules of the switch case statement in C++
There are some rules that we need to follow when using switch statements in C++. They
are as follows:
1. The case value must be either int or char type.
2. There can be any number of cases.
3. No duplicate case values are allowed.
4. Each statement of the case can have a break statement. It is optional.
5. The default Statement is also optional.
Syntax of Switch Statement in C++ Flow chart of switch Statement in C++

switch (expression) {
case value_1:
// statements_1;
break;
case value_2:
// statements_2;
break;
.....
.....
default:
// default_statements;
break;
}

// Program to build a simple calculator using switch Statement


#include <iostream>
using namespace std;
int main() {
char oper;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
switch (oper) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! The operator is not correct";
break; } return 0;}

loops
Loops come into use when we need to repeatedly execute a block of statements.
For example:
Suppose we want to print “Hello World” 5 times. This can be done in two ways as shown
below:
Manual Method (Iterative Method) Using Loops
int main() int main()
{ {
cout << "Hello World\n"; for(int i=0;i<=5;i++)
cout << "Hello World\n"; {
cout << "Hello World\n"; cout << "Hello World\n";
cout << "Hello World\n"; }
cout << "Hello World\n"; }
return 0;
}

There are mainly two types of loops:


1. Entry Controlled loops: the test condition is tested before entering the loop
body. For Loop and While Loop is entry-controlled loops.
2. Exit Controlled Loops: the test condition is tested or evaluated at the end of
the loop body. Therefore, the loop body will execute at least once, irrespective
of whether the test condition is true or false. the do-while loop is exit controlled
loop

1-For Loop
Syntax:
for (initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}
example:
for (int i = 1; i <= 5; i++) {

cout << i<<' ';

Ranged Based for Loop:


for ( range_declaration : range_expression )
loop_statement
example:

for (int n : { 0, 1, 2, 3, 4, 5 })

std::cout << n << ' ';

practical

Write a program in C++ to find the factorial of a number.


Sample output:
Input a number to find the factorial: 5
The factorial of the given number is: 120
#include <iostream>
using namespace std;
int main()
{
int num1,factorial=1;
cout << "\n\n Find the factorial of a number:\n";
cout << "------------------------------------\n";
cout << " Input a number to find the factorial: ";
cin>> num1;
for(int a=1;a<=num1;a++)
{
factorial=factorial*a;
}
cout<<" The factorial of the given number is: "<<factorial<<endl;
return 0;}

While Loop
 while loops are used in situations where we do not know the exact number of
iterations of the loop beforehand. The loop execution is terminated on the basis of
the test conditions.
 while loops are used in situations where we do not know the exact number of
iterations of the loop beforehand. The loop execution is terminated on the basis of
the test conditions.
Syntax:
initialization expression;
while (test_expression)
{
// statements

update_expression;
}

practical

Write a Program to find out no. of Even & Odd no. in a given Data Series and terminate if
entered value is -1.
#include <iostream.h>

int main()

{inteven_n = 0; intodd_n = 0;

intnum;

cout<< "Enter a positive number: ";

cin>>num;

while(num != -1){

if (num % 2 == 0) even_n++;

elseodd_n++;

cout<< " input enter a positive number: ";

cin>>num;

cout<< "\nEven No.: " <<even_n<< "\n";

cout<< "Odd No.: " <<odd_n<< "\n";return 0;}

Do/While Loop

the main difference between a do-while loop and a while loop is in the do-while loop the
condition is tested at the end of the loop body, i.e do-while loop is exit controlled whereas
the other two loops are entry-controlled loops.
Syntax:
do
{
// loop body

update_expression;
}
while (test_expression);

practical
C++ Program to Display English Alphabets from A-Z

#include<iostream>
using namespace std;
int main(){
char character='A';
do
{
cout<<character<<" ";
character++;}
while(character<='Z');
cout<<endl;return 0;}
Nested Loops

means a loop statement inside another loop statement. That is why nested loops are also
called as “loop inside loop“.

Syntax for Nested For loop: Syntax for Nested While loop: Syntax for Nested Do-While loop :
for ( initialization; condition; increment ) while(condition) { do{

{ while(condition) { do{

for ( initialization; condition; increment ) // statement of inside loop // statement of inside loop
{ } }while(condition) {

// statement of inside loop // statement of inside loop // statement of outer loop

} }

}while(condition) {

// statement of outer loop

practical C++ program to display a triangular pattern


#include <iostream>
using namespace std;

int main() {
int i, j, n;
cout << "Enter Number : ";
cin >> n;
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
} return 0;}

Array
What is Array in C?
An array in C is a fixed-size collection of similar data items stored in contiguous memory
locations.

Types of Array in C
There are two types of arrays based on the number of dimensions it has. They are as
follows:
1. One Dimensional Arrays (1D Array)
2. Two Dimensional Arrays (2D Array)
3. Multidimensional Arrays
One Dimensional Array in C
it's possible to initialize an array during declaration.
For example,
// declare and initialize and array
int x[6] = {19, 10, 8, 17, 9, 15};

or
int x[ ] = {19, 10, 8, 17, 9, 15};

Array With Empty Members


For example,

// store only 3 elements in the array


int x[6] = {19, 10, 8};
practical
Write a program that display the data of the array.

#include<iostream>
using namespace std;
int main() {
int num[] = { 2,7,4,3,8 };
for (int i = 0; i < 5 ; i++) {
cout << "num" << i << " = " << num[i] << endl;
}
return 0;}

Two Dimensional Array:


Is array of array

 Initializing a 2D array in C++

int arr[4][2] = {1234, 56, 1212, 33, 1434, 80, 1312, 78};
practical

Write a program that insert elements in 2d array.


#include<iostream>
using namespace std;
int main() {
int num[3][3];
for (int r = 0; r < 3; r++) {
for (int c=0; c < 3; c++) {
cout << “enter num [ "<< r << " ] [ " << c <<" ] " ;
Cin>>num[r][c];
}
}
return 0;

You might also like