60 likes | 154 Views
Example 21. #include<iostream.h> int main() { char Letter = 0; cout <<"Enter a lower case letter: "; cin >> Letter; switch (Letter * (Letter>='a' && Letter <= 'z')) { case 'a': case 'e': case 'i': case 'o': case 'u': cout <<endl <<"You have entered a vowel.";
E N D
Example 21 #include<iostream.h> int main() { char Letter = 0; cout <<"Enter a lower case letter: "; cin >> Letter; switch (Letter * (Letter>='a' && Letter <= 'z')) { case 'a': case 'e': case 'i': case 'o': case 'u': cout <<endl <<"You have entered a vowel."; break; case 0: cout <<endl << Letter <<" it is not a lower case letter."; break; default: cout <<endl <<"You entered a consonant." ; } cout <<endl; return 0; }
Example 22 #include<iostream.h> int main() { char indicator ='n'; long value =0, factorial = 0; do { cout <<"Enter an integer value: "; cin >> value; factorial = 1; for (int i=2; i<=value; i++) factorial *=i; cout <<"Factorial " <<value <<" is " <<factorial; cout <<endl <<"Do you want to enter another value (y or n)?"; cin >>indicator; } while ((indicator == 'y')|| (indicator == 'y')); return 0; }
Example 23 #include<iostream.h> #include<iomanip.h> int main() { int SIZE =9; int i=0, j=0; cout <<SIZE <<" by " <<SIZE <<"multiplication table"; cout <<endl; cout <<endl <<" |"; for (i=1; i<=SIZE; i++) cout <<setw(3) <<i <<" "; cout <<endl; for (i=0; i<=SIZE; i++) cout <<"_____"; for (i=0; i<=SIZE; i++) { cout <<endl <<setw(3) <<i <<" |"; for (j=1; j<=SIZE; j++) cout <<setw(3) <<i*j <<" "; } cout <<endl; return 0; }
Arrays • An array is simply a number of memory locations, each of which can store an item of data of the same data type and which are all referenced through the same variable name. Type name[size] Type name[row][column]
Example 24 #include <iostream.h> #include <iomanip.h> int main() { int array1[6]={1, 2, 3 ,4, 5, 6}; int array2[6]={1, 2, 3}; int array3[6]; for (int i=0; i<6; i++) cout <<setw(12) <<array1[i]; cout <<endl; for (i=0; i<6; i++) cout <<setw(12) <<array2[i]; cout <<endl; for (i=0; i<6; i++) cout <<setw(12) <<array3[i]; cout <<endl; return 0; }
Example 25 #include <iostream.h> #include <iomanip.h> int main() { int array1[2][6]={ {1, 2, 3 ,4, 5, 6}, {6, 5, 4, 3, 2, 1} }; int array2[2][6]={ {1, 2, 3}, {4, 3, 2, 1} }; int array3[2][6]={0}; for (int i=0; i<2; i++) for (int j=0; j<6; j++) cout <<setw(12) <<array1[i][j]; cout << endl <<endl; for (i=0; i<2; i++) for (int j=0; j<6; j++) cout <<setw(12) <<array2[i][j]; cout <<endl <<endl; for (i=0; i<2; i++) for (int j=0; j<6; j++) cout <<setw(12) <<array3[i][j]; cout <<endl; return 0; }