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

lecture 14 Programming fundamental

The document provides multiple C++ programs demonstrating various types of calculators and pyramid patterns using loops and switch statements. It includes examples for simple arithmetic operations, half pyramids with asterisks and numbers, inverted pyramids, and full pyramids. Each program is accompanied by input prompts and expected output results.

Uploaded by

Shahab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

lecture 14 Programming fundamental

The document provides multiple C++ programs demonstrating various types of calculators and pyramid patterns using loops and switch statements. It includes examples for simple arithmetic operations, half pyramids with asterisks and numbers, inverted pyramids, and full pyramids. Each program is accompanied by input prompts and expected output results.

Uploaded by

Shahab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

EXAMPLE: SIMPLE CALCULATOR USING SWITCH

STATEMENT

 # include <iostream>
 #include <conio.h>
 using namespace std;
 int main() { char op;
 float num1, num2;
 cout << "Enter operator either + or - or * or /: ";
 cin >> op;
 cout << "Enter two operands: ";
 cin >> num1 >> num2;
 switch(op)
 { case '+':
 cout << num1+num2;
 break;
PROGRAM CONTINUED
 case '-':
 cout << num1-num2;
 break;
 case '*':
 cout << num1*num2;
 break;
 case '/':
 cout << num1/num2;
 break;
 default: // If the operator is other than +, -, * or /, error
message is shown
 cout << "Error! operator is not correct"; break;
 } getch(); }
TEST RESULTS
Enter operator either + or - or * or divide : -
 Enter two operands: 3.4

 8.4

 3.4 - 8.4 = -5.0


PROGRAM TO PRINT HALF PYRAMID
USING *

 #include <iostream>
 #include<conio.h>
 using namespace std;
 int main() {
 int rows;
 cout << "Enter number of rows: ";
 cin >> rows;
 for(int i = 1; i <= rows; ++i)
 { for(int j = 1; j <= i; ++j)
 { cout << "* "; }
 cout << "\n"; }
 getch(); }
RESULTS
*
* *

 * * *

 * * * *

 * * * * *
PROGRAM TO PRINT HALF PYRAMID A USING
NUMBERS

 #include <iostream>
 #include<conio.h>

 using namespace std;

 int main()

 { int rows;

 cout << "Enter number of rows: ";

 cin >> rows;

 for(int i = 1; i <= rows; ++i)

 { for(int j = 1; j <= i; ++j)

 { cout << j << " "; }

 cout << "\n"; } getch(); }


RESULTS
1
1 2

1 2 3

 1 2 3 4

 1 2 3 4 5
PROGRAMS TO PRINT INVERTED HALF PYRAMID
USING *

 #include <iostream>
 #include<conio.h>

 using namespace std;

 int main()

 { int rows;

 cout << "Enter number of rows: ";

 cin >> rows;

 for(int i = rows; i >= 1; --i)

 { for(int j = 1; j <= i; ++j)

 { cout << "* "; }

 cout << endl; } getch();}


RESULTS
*****
 * * * *

 * * *

 * *

 *
INVERTED HALF PYRAMID USING
NUMBERS

 #include <iostream>
 #include<conio.h>
 using namespace std;
 int main()
 { int rows;
 cout << "Enter number of rows: ";
 cin >> rows;
 for(int i = rows; i >= 1; --i)
 { for(int j = 1; j <= i; ++j)
 { cout << j << " "; }
 cout << endl; }
 getch(); }
RESULTS
12345
1 2 3 4

 1 2 3

 1 2

 1
PROGRAM TO PRINT FULL PYRAMID USING
*

 #include <iostream>
 #include<conio.h>
 using namespace std;
 int main()
 { int space, rows;
 cout <<"Enter number of rows: ";
 cin >> rows;
 for(int i = 1, k = 0; i <= rows; ++i, k = 0)
 { for(space = 1; space <= rows-i; ++space)
 { cout <<“ "; } while(k != 2*i-1)
 { cout << "* "; ++k; }
 cout << endl; } getch(); }
RESULTS
 *
 ***
 *****
 *******
 *********
PROGRAM TO PRINT PYRAMID USING
NUMBERS

 #include <iostream>
 #include<conio.h>

 using namespace std;

 int main()

 { int rows, count = 0, count1 = 0, k = 0;

 cout << "Enter number of rows: ";

 cin >> rows;

 for(int i = 1; i <= rows; ++i)

 { for(int space = 1; space <= rows-i; +

+space)
 { cout << " "; ++count; }
PROGRAM CONTINUED
 while(k != 2*i-1)
 { if (count <= rows-1)

 { cout << i+k << " "; ++count; }

 else { ++count1; cout << i+k-2*count1 << "

"; }
 ++k;

 }

 count1 = count = k = 0;

 cout << endl; }

 getch();

 }
RESULTS
 1
 232
 34543
 4567654
 567898765
INVERTED FULL PYRAMID USING *

 #include <iostream>
 #include<conio.h>

 using namespace std;

 int main()

 { int rows;

 cout << "Enter number of rows: ";

 cin >> rows;

 for(int i = rows; i >= 1; --i)

 { for(int space = 0; space < rows-i; ++space)

 cout << " ";


for(int j = i; j <= 2*i-1; ++j)
 cout << "* ";

 for(int j = 0; j < i-1; ++j)

 cout << "* ";

 cout << endl;

}

 getch();

}
RESULTS
 *********
 *******
 *****
 ***
 *

You might also like