Exempel för att skriva ut halvpyramid, pyramid, inverterad pyramid, Pascals triangel och Floyds triangel i C ++ programmering med hjälp av kontrolluttalanden.
För att förstå detta exempel bör du ha kunskap om följande C ++ programmeringsämnen:
- C ++ if, if… else och Nested if … else
- C ++ för Loop
- C ++ medan och gör … medan Loop
- C ++ bryta och fortsätt uttalande
Lista över källkoden |
---|
Skriv ut triangeln med *, siffror och tecken |
Skriv ut inverterad triangel med * och siffra |
Kod för att skriva ut pyramid |
Kod för att skriva ut omvänd pyramid |
Kod för att skriva ut Pascals trånga |
Kod för att skriva ut Floyds triangel |
Program för att skriva ut trianglar med *, siffror och tecken
Exempel 1: Program för att skriva ut halv pyramid med *
* * * * * * * * * * * * * *
Källkod
#include using namespace std; int main() ( int rows; cout <> rows; for(int i = 1; i <= rows; ++i) ( for(int j = 1; j <= i; ++j) ( cout << "* "; ) cout << ""; ) return 0; )
Exempel 2: Program för att skriva ut halvpyramid a med siffror
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Källkod
#include using namespace std; int main() ( int rows; cout <> rows; for(int i = 1; i <= rows; ++i) ( for(int j = 1; j <= i; ++j) ( cout << j << " "; ) cout << ""; ) return 0; )
Exempel 3: Program för att skriva ut halvpyramiden med alfabet
ABBCCCDDDDEEEEE
Källkod
#include using namespace std; int main() ( char input, alphabet = 'A'; cout <> input; for(int i = 1; i <= (input-'A'+1); ++i) ( for(int j = 1; j <= i; ++j) ( cout << alphabet << " "; ) ++alphabet; cout << endl; ) return 0; )
Program för att skriva ut inverterad halvpyramid med * och siffror
Exempel 4: Inverterad halvpyramid med *
* * * * * * * * * * * * * *
Källkod
#include using namespace std; int main() ( int rows; cout <> rows; for(int i = rows; i>= 1; --i) ( for(int j = 1; j <= i; ++j) ( cout << "* "; ) cout << endl; ) return 0; )
Exempel 5: Inverterad halvpyramid med siffror
1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Källkod
#include using namespace std; int main() ( int rows; cout <> rows; for(int i = rows; i>= 1; --i) ( for(int j = 1; j <= i; ++j) ( cout << j << " "; ) cout << endl; ) return 0; )
Program för att visa pyramid och inverterad pyramid med * och siffror
Exempel 6: Program för att skriva ut hela pyramiden med *
* * * * * * * * * * * * * * * * * * * * * *
Källkod
#include using namespace std; int main() ( int space, rows; cout <> 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; ) return 0; )
Exempel 7: Program för att skriva ut pyramid med siffror
1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5
Källkod
#include using namespace std; int main() ( int rows, count = 0, count1 = 0, k = 0; cout <> rows; for(int i = 1; i <= rows; ++i) ( for(int space = 1; space <= rows-i; ++space) ( cout << " "; ++count; ) 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; ) return 0; )
Exempel 8: Inverterad full pyramid med *
* * * * * * * * * * * * * * * * * * * * * *
Källkod
#include using namespace std; int main() ( int rows; cout <> 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; ) return 0; )
Exempel 9: Skriv ut Pascals triangel
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
Källkod
#include using namespace std; int main() ( int rows, coef = 1; cout <> rows; for(int i = 0; i < rows; i++) ( for(int space = 1; space <= rows-i; space++) cout <<" "; for(int j = 0; j <= i; j++) ( if (j == 0 || i == 0) coef = 1; else coef = coef*(i-j+1)/j; cout << coef << " "; ) cout << endl; ) return 0; )
Exempel 10: Skriv ut Floyds triangel.
1 2 3 4 5 6 7 8 9 10
Källkod
#include using namespace std; int main() ( int rows, number = 1; cout <> rows; for(int i = 1; i <= rows; i++) ( for(int j = 1; j <= i; ++j) ( cout << number << " "; ++number; ) cout << endl; ) return 0; )