C++ Code Examples and Lab Practice
C++ Code Examples and Lab Practice
1
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
2
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
cout << "(3 == 5) || (3 > 5) is " << result << endl; if (number > 0) {
result = !(5 == 2); // true cout << "You entered a positive integer: " << number << endl;
cout << "!(5 == 2) is " << result << endl; }
result = !(5 == 5); // false cout << "This statement is always executed.";
cout << "!(5 == 5) is " << result << endl; return 0;
return 0; }
} C++ if...else
C++ if Statement The if statement can have an optional else clause. Its syntax is:
The syntax of the if statement is: if (condition) {
if (condition) { // block of code if condition is true
// body of if statement }
} else {
// block of code if condition is false
Example 1: C++ if Statement }
// Program to print positive number entered by the user Example 2: C++ if...else Statement
// If the user enters a negative number, it is skipped // Program to check whether an integer is positive or negative
#include <iostream> // This program considers 0 as a positive number
using namespace std; #include <iostream>
int main () { using namespace std;
int number; int main () {
cout << "Enter an integer: "; int number;
cin >> number; cout << "Enter an integer: ";
// checks if the number is positive cin >> number;
3
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
4
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
// inner if statement }
if (condition2) { }
// statements // outer else condition
} else {
} cout<<"The number is 0 and it is neither even nor odd."<< endl;
Example 4: C++ Nested if }
// C++ program to find if an integer is even or odd or neither (0) cout << "This line is always printed." << endl;
// using nested if statements }
#include <iostream> Return 0;
using namespace std; }
int main () { C++ for loop
int num; The syntax of for-loop is:
cout << "Enter an integer: "; for (initialization; condition; update) {
cin >> num; // body of-loop
// outer if condition }
if (num != 0) { Example 1: Printing Numbers From 1 to 5
// inner if condition #include <iostream>
if ((num % 2) == 0) { using namespace std;
cout << "The number is even." << endl; int main () {
} for (int i = 1; i <= 5; ++i) {
// inner else condition cout << i << " ";
else { }
cout << "The number is odd." << endl; return 0;
5
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
6
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
7
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
8
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
// second loop
example1: for (int j = 1; j <= 3; j++) {
#include <iostream> if (j == 2) {
using namespace std; continue;
int main () { }
for (int i = 1; i <= 5; i++) { cout << "i = " << i << ", j = " << j << endl;
// condition to continue }
if (i ==3) { }
continue; return 0;
} }
cout << i << endl; C++ switch...case Statement
} The syntax of the switch statement in C++ is:
return 0; switch (expression) {
} case constant1:
Example 2: continue with Nested loop // code to be executed if
#include <iostream> // expression is equal to constant1;
using namespace std; break;
int main () { case constant2:
int number; // code to be executed if
int sum = 0; // expression is equal to constant2;
// nested for loops break;
// first loop .
for (int i = 1; i <= 3; i++) { .
9
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
. case '*':
default: cout << num1 << " * " << num2 << " = " << num1 * num2;
// code to be executed if break;
// expression doesn't match any constant case '/':
} cout << num1 << " / " << num2 << " = " << num1 / num2;
Example: Create a Calculator using the switch Statement break;
// Program to build a simple calculator using switch Statement default:
#include <iostream> // operator is doesn't match any case constant (+, -, *, /)
using namespace std; cout << "Error! The operator is not correct";
int main () { break;
char oper; }
float num1, num2; return 0;
cout << "Enter an operator (+, -, *, /): "; }
cin >> oper; C++ goto Statement
cout << "Enter two numbers: " << endl; Syntax of goto Statement
cin >> num1 >> num2; goto label;
switch (oper) { ... .. ...
case '+': ... .. ...
cout << num1 << " + " << num2 << " = " << num1 + num2; ... .. ...
break; label:
case '-': statement;
cout << num1 << " - " << num2 << " = " << num1 - num2; ... .. ...
break; Example: goto Statement
10
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
11
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
12
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
13
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
using namespace std; From the ASCII table: Digit character (48-57), uppercase character
(65-90), lowercase character (97-122) and others are special
int main() characters.
{
float root1,root2,a,b,c,d,imaginaryPart,realPart; #include<iostream>
cout<<"Quadratic Equation is ax^2+bx+c=0"; using namespace std;
cout<<"\nEnter values of a,b and c:"; int main()
cin>>a>>b>>c; {
d=(b*b)-(4*a*c); char ch;
if(d>0) cout<<"Enter any character: ";
{ cin>>ch;
cout<<"\nTwo real and distinct roots"; if(ch>=65&&ch<=90){
root1=(-b+sqrt(d))/(2*a); cout<<endl<<"You entered an uppercase character\n";
root2=(-b-sqrt(d))/(2*a); }
cout<<"\nRoots are "<<root1<<" and "<<root2; else if(ch>=48&&ch<=57)
} {
else if(d==0) cout<<endl<<"You entered a digit\n";
{ }
cout<<"\nTwo real and equal roots"; else if(ch>=97&&ch<=122)
root1=root2=-b/(2*a); {
cout<<"\nRoots are "<<root1<<" and "<<root2; cout<<endl<<"You entered a lowercase character\n";
} }
else{ else
cout<<"\nRoots are complex and imaginary"; {
realPart = -b/(2*a); cout<<endl<<"You entered a special character\n";
imaginaryPart = sqrt(-d)/(2*a); }
14
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
15
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
In the following C++ program, the user can enter the number of rows
to print the inverted half star pyramid pattern as he wishes, then the
result will be displayed on the screen:
#include <iostream>
using namespace std;
int main()
{
int i, j, n;
cout << "Enter number of rows: "; Example 3- Program in C++ to print star pyramid pattern
In the following program, the user can enter the number of rows to
cin >> n;
print the star pyramid pattern as he wishes, then the result will be
for(i = n; i >= 1; i--) displayed on the screen:
{ #include<iostream>
for(j = 1; j <= i; j++) using namespace std;
{ int main()
cout << "* "; {
} int n, s, i, j;
// ending line after each row cout << "Enter number of rows: ";
cout << "\n"; cin >> n;
} for(i = 1; i <= n; i++)
return 0; {
} //for loop for displaying space
or(s = i; s < n; s++)
{
16
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
17
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
18
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
cout << "Enter number of rows: "; In the following program, the user can enter the number of rows for
the diamond dimension to print the diamond pattern as he wishes,
cin >> n; then the result will be displayed on the screen:
for(i = 1; i <= n; i++) #include<iostream>
{ using namespace std;
//for loop to put space int main()
for(s = i; s < n; s++) {
cout << " "; int n, s, i, j;
//for loop for displaying star cout << "Enter number of rows: ";
for(j = 1; j <= i; j++) cin >> n;
cout << "* "; for(i = 0; i <= n; i++)
// ending line after each row {
cout << "\n"; for(s = n; s > i; s--)
} cout << " ";
return 0; for(j=0; j<i; j++)
} cout << "* ";
cout << "\n";
}
for(i = 1; i < n; i++)
{
for(s = 0; s < i; s++)
cout << " ";
for(j = n; j > i; j--)
Example 7 – Program to print full star diamond pattern in C++
19
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
20
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
21
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
22
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
23
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.
24