0% found this document useful (0 votes)
26 views24 pages

C++ Code Examples and Lab Practice

The document provides a comprehensive overview of various C++ programming concepts, including arithmetic, assignment, relational, logical operators, and control flow statements like if, for, while, and do...while loops. It also covers the use of break, continue, switch-case statements, and the goto statement with examples. The content is compiled for educational purposes at Ambo University, Hachalu Hundessa Campus for the academic year 2023/2024.
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)
26 views24 pages

C++ Code Examples and Lab Practice

The document provides a comprehensive overview of various C++ programming concepts, including arithmetic, assignment, relational, logical operators, and control flow statements like if, for, while, and do...while loops. It also covers the use of break, continue, switch-case statements, and the goto statement with examples. The content is compiled for educational purposes at Ambo University, Hachalu Hundessa Campus for the academic year 2023/2024.
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/ 24

Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.

Example 2: Arithmetic Operators #include <iostream>


#include <iostream> using namespace std;
using namespace std; int main ()
int main () {
{ int a = 10, b = 100, result_a, result_b;
int a, b; // incrementing a by 1 and storing the result in result_a
a = 7; result_a = ++a;
b = 2; cout << "result_a = " << result_a << endl;
// printing the sum of a and b // decrementing b by 1 and storing the result in result_b
cout << "a + b = " << (a + b) << endl; result_b = --b;
// printing the difference of a and b cout << "result_b = " << result_b << endl;
cout << "a - b = " << (a - b) << endl; return 0;
// printing the product of a and b }
cout << "a * b = " << (a * b) << endl; Example 3: Assignment Operators
// printing the division of a by b #include <iostream>
cout << "a / b = " << (a / b) << endl; using namespace std;
// printing the modulo of a by b int main ()
cout << "a % b = " << (a % b) << endl; {
return 0; int a, b;
} a = 2; // 2 is assigned to a
b = 7; // 7 is assigned to b
Example 2: Increment and Decrement Operators cout << "a = " << a << endl;
// Working of increment and decrement operators cout << "b = " << b << endl;

1
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.

cout << "\nAfter a += b;" << endl; result = a >= b; // false


// assigning the sum of a and b to a cout << "3 >= 5 is " << result << endl;
a += b; // a = a +b result = a <= b; // true
cout << "a = " << a << endl; cout << "3 <= 5 is " << result << endl;
return 0; return 0;
} }
Example 4: Relational Operators Example 5: Logical Operators
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main () int main ()
{ {
int a, b; bool result;
a = 3; result = (3 != 5) && (3 < 5); // true
b = 5; cout << "(3 != 5) && (3 < 5) is " << result << endl;
bool result; result = (3 == 5) && (3 < 5); // false
result = (a == b); // false cout << "(3 == 5) && (3 < 5) is " << result << endl;
cout << "3 == 5 is " << result << endl; result = (3 == 5) && (3 > 5); // false
result = (a != b); // true cout << "(3 == 5) && (3 > 5) is " << result << endl;
cout << "3 != 5 is " << result << endl; result = (3 != 5) || (3 < 5); // true
result = a > b; // false cout << "(3 != 5) || (3 < 5) is " << result << endl;
cout << "3 > 5 is " << result << endl; result = (3 != 5) || (3 > 5); // true
result = a < b; // true cout << "(3 != 5) || (3 > 5) is " << result << endl;
cout << "3 < 5 is " << result << endl; result = (3 == 5) || (3 > 5); // false

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.

if (number >= 0) { #include <iostream>


cout << "You entered a positive integer: " << number << endl; using namespace std;
} int main () {
else { int number;
cout << "You entered a negative integer: " << number << endl; cout << "Enter an integer: ";
} cin >> number;
cout << "This line is always printed."; if (number > 0) {
return 0; cout << "You entered a positive integer: " << number << endl;
} }
else if (number < 0) {
C++ if...else if...else statement cout << "You entered a negative integer: " << number << endl;
The syntax of the if...else if...else statement is: }
if (condition1) { else {
// code block 1 cout << "You entered 0." << endl;
} }
else if (condition2) { cout << "This line is always printed.";
// code block 2 return 0;
} }
else { C++ Nested if...else
// code block 3 syntax:
} // outer if statement
Example 3: C++ if...else if...else if (condition1) {
// Program to check whether an integer is positive, negative or zero // statements

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.

} Example 1: Display Numbers from 1 to 5


Example 2: Find the sum of first n Natural Numbers // C++ Program to print numbers from 1 to 5
// C++ program to find the sum of first n natural numbers #include <iostream>
// positive integers such as 1,2, 3...n is known as natural numbers using namespace std;
#include <iostream> int main () {
using namespace std; int i = 1;
int main () { // while loop from 1 to 5
int num, sum; while (i <= 5) {
sum = 0; cout << i << " ";
cout << "Enter a positive integer: "; ++i;
cin >> num; }
for (int i = 1; i <= num; ++i) { return 0;
sum += i; }
} Example 2: Sum of Positive Numbers Only
cout << "Sum = " << sum << endl; // program to find the sum of positive numbers
return 0; // if the user enters a negative number, the loop ends
} // the negative number entered is not added to the sum
#include <iostream>
C++ while Loop using namespace std;
The syntax of the while loop is: int main () {
while (condition) { int number;
// body of the loop int sum = 0;
} // take input from the user

6
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.

cout << "Enter a number: "; int main () {


cin >> number; int i = 1;
while (number >= 0) { // do...while loop from 1 to 5
// add all positive numbers do {
sum += number; cout << i << " ";
// take input again if the number is positive ++i;
cout << "Enter a number: "; }
cin >> number; while (i <= 5);
} return 0;
// display the sum }
cout << "\nThe sum is " << sum << endl; Example 4: Sum of Positive Numbers Only
return 0; // program to find the sum of positive numbers
} // If the user enters a negative number, the loop ends
C++ do...while Loop // the negative number entered is not added to the sum
Its syntax is: #include <iostream>
do { using namespace std;
// body of loop; int main () {
} int number = 0;
while (condition); int sum = 0;
Example 3: Display Numbers from 1 to 5 do {
// C++ Program to print numbers from 1 to 5 sum += number;
#include <iostream> // take input from the user
using namespace std; cout << "Enter a number: ";

7
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.

cin >> number; Example2:


} #include <iostream>
while (number >= 0); using namespace std;
// display the sum int main () {
cout << "\nThe sum is " << sum << endl; int number;
return 0; int sum = 0;
} // nested for loops
C++ break Statement // first loop
The syntax of the break statement is: for (int i = 1; i <= 3; i++) {
break; // second loop
// program to print the value of i for (int j = 1; j <= 3; j++) {
#include <iostream> if (i == 2) {
using namespace std; break;
int main () { }
for (int i = 5; i <= 10; i++) { cout << "i = " << i << ", j = " << j << endl;
// break condition }
if (i == 9) { }
break; return 0;
} }
cout << i << endl;
} C++ continue Statement
return 0; The syntax of the continue statement is:
} continue;

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.

// This program calculates the average of numbers entered by the jump:


user.
average = sum / (i - 1);
// If the user enters a negative number, it ignores the number and
cout << "\nAverage = " << average;
// calculates the average number entered before it.
return 0;
# include <iostream>
}
using namespace std;
#include <iostream>
int main () using namespace std;
{ int main()
float num, average, sum = 0.0; {
int i,j,r;
int i, n; cout << "\n\n Display the pattern like a diamond:\n";
cout << "----------------------------------------\n";
cout << "Maximum number of inputs: ";
cout << " Input number of rows (half of the diamond): ";
cin >> n; cin >> r;
for(i=0;i<=r;i++)
for (i = 1; i <= n; ++i) {
{ for(j=1;j<=r-i;j++)
cout<<" ";
cout << "Enter n" << i << ": "; for(j=1;j<=2*i-1;j++)
cout<<"*";
cin >> num; cout<<endl;
if(num < 0.0) }
for(i=r-1;i>=1;i--)
{ {
for(j=1;j<=r-i;j++)
// Control of the program move to jump: cout<<" ";
goto jump; for(j=1;j<=2*i-1;j++)
cout<<"*";
} cout<<endl;;
}
sum += num;
}
}

11
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.

Display the pattern like a diamond: for(i=1; i<=(rowNum-1); i++)


---------------------------------------- {
Input number of rows (half of the diamond): 5 for(j=1; j<=space; j++)
cout<<" ";
* space++;
*** for(j=1; j<=(2*(rowNum-i)-1); j++)
***** {
******* cout<<num;
********* num++;
******* }
***** cout<<endl;
*** num = 1;
* }
cout<<endl;
#include<iostream> return 0;
using namespace std; }
int main()
{ Here is its sample run with user input, 5:
int i, j, rowNum, space, num=1;
cout<<"Enter the Number of Rows: ";
cin>>rowNum;
space = rowNum-1;
for(i=1; i<=rowNum; i++)
{
for(j=1; j<=space; j++)
cout<<" ";
space--;
for(j=1; j<=(2*i-1); j++)
{
cout<<num;
num++;
}
cout<<endl;
num = 1;
}
space = 1;

12
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.

Print Diamond Pattern of Alphabet Characters ch++;


}
This is the last program that creates diamond pattern of alphabet cout<<endl;
characters. ch = 'A';
}
#include<iostream> cout<<endl;
using namespace std; return 0;
int main() }
{
int i, j, rowNum, space; #include<iostream>
char ch='A';
cout<<"Enter the Number of Rows: "; using namespace std;
cin>>rowNum; int main()
space = rowNum-1; {
for(i=1; i<=rowNum; i++) int num, i=1;
{ cout<<"Enter a Number: ";
for(j=1; j<=space; j++) cin>>num;
cout<<" "; cout<<"\nFactors of "<<num<<" are:\n";
space--; while(i<=num)
for(j=1; j<=(2*i-1); j++) {
{ if(num%i==0)
cout<<ch; cout<<i<<" ";
ch++; i++;
} }
cout<<endl; cout<<endl;
ch = 'A'; return 0;
} }
space = 1;
for(i=1; i<=(rowNum-1); i++) #include<iostream>
{ using namespace std;
for(j=1; j<=space; j++) int main()
cout<<" "; {
space++; char ch;
for(j=1; j<=(2*(rowNum-i)-1); j++) cout<<"Enter a Character: ";
{ cin>>ch;
cout<<ch; if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))

13
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.

cout<<endl<<ch<<" is an Alphabet"; cout<<"\nRoots are "<<realPart<<"+"<<imaginaryPart<<"i


else and "<<realPart<<"-"<<imaginaryPart<<"i";
cout<<endl<<ch<<" isn't an Alphabet"; }
cout<<endl;
return 0; return 0;
} }

#include<iostream> Example: Write C++ Program to Check Given Character is


#include<math.h> //to calculate square root Uppercase, Lowercase, Digit or Special Character

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.

return 0; cin >> n;


}
for(i = 1; i <= n; i++)
C++ Program to Find ASCII value of a character {
#include<iostream.h>
#include<conio.h> for(j = 1; j <= i; j++)
void main() {
{
clrscr(); cout << "* ";
char ch,c;
int cha; }
cout<<“Enter a character:”; //Ending line after each row
cin>>ch;
cha=ch; cout << "\n";
cout<<“nASCII value of “<<ch<<” is “<<cha;
}
c=ch+1; cha=c;
cout<<“nAdding one to the character:”<<“nASCII value of “<<c<<” return 0;
is “<<cha;
getch(); }
}

Example 1 – Program in C++ to print half star pyramid pattern


In the following C++ program, the user can enter a number of rows
to print the half star pyramid pattern as he wishes, then the result will
be displayed on the screen:
#include <iostream>
using namespace std;
int main()
{ Example 2- Program in C++ to print inverted half star pyramid
pattern
int i, j, n;
cout << "Enter number of rows: ";

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.

cout << " "; #include<iostream>


} using namespace std;
//for loop to display star equal to row number int main()
for(j = 1; j <= (2 * i - 1); j++) {
{ int n, s, i, j;
cout << "*"; cout << "Enter number of rows: ";
} cin >> n;
// ending line after each row for(i = n; i >= 1; i--)
cout << "\n"; {
} //for loop to put space
} for(s = i; s < n; s++)
cout << " ";
//for loop for displaying star
for(j = 1; j <= (2 * i - 1); j++)
cout << "* ";
// ending line after each row
cout << "\n";
}

Example 4- Program in C++ to enter a number of rows to print the return 0;


star pyramid pattern }
In the following program, the user can enter a number of rows to
print the star pyramid pattern as he wishes, then the result will be
displayed on the screen:

17
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.

//for loop for displaying star


for(j = 1; j <= i; j++)
cout << "* ";
// ending line after each row
cout << "\n";
}
return 0;
Example 5– Program in C++ to print inverted star pyramid pattern }
In the following program, the user can enter a number of rows to
print the inverted star pyramid pattern as he wishes, then the result
will be displayed on the screen:
#include<iostream>
using namespace std;
int main()
{
int n, s, i, j; Example 6 – Program in C++ to enter the number of rows to print the
star pyramid pattern
cout << "Enter number of rows: ";
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
displayed on the screen:
for(i = n; i >= 1; 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;

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.

cout << "* "; cin >> n;


// ending line after each row for(i = 1; i <= n; i++)
cout << "\n"; {
} for(j = 1; j <= i; j++)
return 0; {
} cout << "*";
}
cout<<"\n";
}
for(i = n; i >= 1; i--)
{
for(j = 1; j <= i; j++)
{
cout << "*" ;
}
// ending line after each row
Example 8
cout<<"\n";
#include<iostream>
}
using namespace std;
return 0;
int main()
}
{
int n, i , j;
cout << "Enter number of rows: ";

20
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.

for(j = 1; j <= i; j++)


{
cout << "*";
}
cout << "\n";
}
for(i = n; i >= 1; i--)
{
for(j = i; j <= n; j++)
Example 9
{
#include<iostream>
cout << " ";
using namespace std;
}
int main()
for(j = 1; j < i; j++)
{
{
int n, i, j;
cout<<"*";
cout << "Enter number of rows: ";
}
cin >> n;
// ending line after each row
for(i = 1; i <= n; i++)
cout<<"\n";
{
}
for(j = i; j < n; j++)
return 0;
{
}
cout << " ";
}

21
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.

cout << " ";


//for loop to print star
for(j = 1; j <= (2 * r - 1); j++)
{
if(i == r || j == 1 || j == 2*i - 1)
cout << "*";
else
cout << " ";
}
Example 10-Program to print hollow star pyramid
//ending line after each row
In the following program, the user can enter the number of rows to
print the hollow star pyramid pattern as he wishes, then the result cout << "\n";
will be displayed on the screen:
}
#include<iostream>
return 0;
using namespace std;
}
int main()
Example 11-Program to print inverted hollow star pyramid pattern
{
In the following program, the user can enter a number of rows to
int r, i, j, s; print the inverted hollow star pyramid pattern as he wishes, then the
result will be displayed on the screen:
cout << "Enter number of rows: ";
#include<iostream>
cin >> r;
using namespace std;
for(i = 1; i <= r; i++)
int main()
{
{
//for loop to put space in pyramid
int r, i, j, s;
for (s = i; s < r; s++)

22
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.

cout << "Enter number of rows: ";


cin >> r;
for(i = r; i >= 1; i--)
{
//for loop to put space in pyramid
for (s = i; s < r; s++)
Example 12- Program to print hollow star pyramid in a diamond
cout << " "; pattern
//for loop to print star in pyramid In the following program, the user can enter the number of rows to
print the hollow star pyramid in a diamond pattern as he wishes, then
for(j = 1; j <= 2 * i - 1; j++)
the result will be displayed on the screen:
{
#include<iostream>
if(i == r || j == 1 || j == 2*i - 1)
using namespace std;
cout << "*";
int main()
else
{
cout << " ";
cout << "Enter size of Daimond: ";
}
int n, i, j, m = 1, k;
cout << "\n";
cin >> n;
return 0;
for(i = 0; i <= n; i++)
}
{
for(j = n; j > i; j--)
{
cout << " ";
}

23
Ambo University, Hachalu Hundessa Campus, 2023/2024…Compiled by Eyosiyas A.

cout << "*"; }


if (i > 0) m -= 2;
{ if(i != n-1)
for(k = 1; k <= m; k++) {
{ cout << "*";
cout << " "; }
} //ending line after each row
m += 2; cout << endl;
cout << "*"; }
} return 0;
cout << endl; }
}
m -= 4;
for(i = 0; i <= n-1; i++)
{
for(j = 0; j <= i; j++)
{
cout << " ";
}
cout << "*";
for(k = 1; k <= m; k++)
{
cout << " ";

24

You might also like