lecture 14 Programming fundamental
lecture 14 Programming fundamental
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
#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>
int main()
{ int rows;
1 2 3
1 2 3 4
1 2 3 4 5
PROGRAMS TO PRINT INVERTED HALF PYRAMID
USING *
#include <iostream>
#include<conio.h>
int main()
{ int rows;
* * *
* *
*
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>
int main()
+space)
{ cout << " "; ++count; }
PROGRAM CONTINUED
while(k != 2*i-1)
{ if (count <= rows-1)
"; }
++k;
}
count1 = count = k = 0;
getch();
}
RESULTS
1
232
34543
4567654
567898765
INVERTED FULL PYRAMID USING *
#include <iostream>
#include<conio.h>
int main()
{ int rows;
for(int j = i; j <= 2*i-1; ++j)
cout << "* ";
}
getch();
}
RESULTS
*********
*******
*****
***
*