Course C++
Course C++
practical
Variable in C++
Variables in C++ is a name given to a memory location. It is the
basic unit of storage in a program.
type variable_name;
Declaring multiple variables:
type variable1_name, variable2_name, variable3_name;
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
- 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
- C++ if Statement
if (condition) { // body of if statement }
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;
}
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;
}
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++) {
for (int n : { 0, 1, 2, 3, 4, 5 })
practical
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;
cin>>num;
while(num != -1){
if (num % 2 == 0) even_n++;
elseodd_n++;
cin>>num;
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) {
} }
}while(condition) {
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};
#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;}
int arr[4][2] = {1234, 56, 1212, 33, 1434, 80, 1312, 78};
practical