C programs
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

C programs

on

  • 255 views

 

Statistics

Views

Total Views
255
Views on SlideShare
255
Embed Views
0

Actions

Likes
0
Downloads
6
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

C programs Document Transcript

  • 1. Assignment 1. Display an hourglass pattern 2. #include <iostream> 3. #include <string> 4. #include <sstream> 5. #include <iomanip> 6. 7. using namespace std; 8. 9. const char star = '*'; 10. const char space = ' '; 11. 12. int main(int argc, char *argv[]) { 13. int n, m, i = 0, increment = 1; 14. string in; 15. stringstream ss; 16. 17. do { 18. cout << "Enter height (an odd, positive integer, > 2): "; 19. getline(cin,in); 20. ss.clear(); ss.str(in); 21. } while (!(ss >> n) || (n < 3) || (!(n & 1))); 22. 23. m = n / 2; 24. 25. do { 26. cout << setfill(space) << setw(i + 1) << space; 27. cout << setfill(star) << setw(n - (2*i)) << star << endl; 28. i += increment; 29. if (i == m) increment = -1; 30. } while (i >= 0); 31. 32. system("pause"); 33. }
  • 2. Using hash ### #include #include #include #include <iostream> <string> <sstream> <iomanip> using namespace std; const char hash = '#'; const char space = ' '; int main(int argc, char *argv[]) { int n, m, i = 0, increment = 1; string in; stringstream ss; do { cout << "Enter height (an odd, positive integer, > 2): "; getline(cin,in); ss.clear(); ss.str(in); } while (!(ss >> n) || (n < 3) || (!(n & 1))); m = n / 2; do { cout << setfill(space) << setw(i + 1) << space; cout << setfill(hash) << setw(n - (2*i)) << hash << endl; i += increment; if (i == m) increment = -1; } while (i >= 0); system("pause"); }
  • 3. 2. Display a diamond pattern #include <iostream> using namespace std; void main() { int i, j, k; int n = 0; cout << "Program for displaying pattern of *.n"; cout << "Enter the maximum number of *: "; cin >> n; cout << "nHere is the Diamond of Starsn"; for (i = 1; i <= n; i++) { for (j = 0; j < (n - i); j++) cout << " "; for (j = 1; j <= i; j++) cout << "*"; for (k = 1; k < i; k++) cout << "*"; cout << "n"; } for (i = n - 1; i >= 1; i--) { for (j = 0; j < (n - i); j++) cout << " "; for (j = 1; j <= i; j++) cout << "*"; for (k = 1; k < i; k++) cout << "*"; cout << "n"; } cout << "n"; system("pause"); }
  • 4. 3. Display the Nepal flag #include <iostream> using namespace std; void main() { int i, j, k; int n = 0; cout << "Program for displaying pattern of *.n"; cout << "Enter the maximum number of *: "; cin >> n; cout << "nHere is the Diamond of Starsn"; for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) cout << "*"; cout << "n"; } for (i = n - 1; i >= 1; i--) { for (j = 1; j <= i; j++) cout << "*"; cout << "n"; } for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) cout << "*"; cout << "n"; } for (i = n - 1; i >= 1; i--) { for (j = 1; j <= i; j++) cout << "*"; cout << "n"; } cout << "n"; system("pause");
  • 5. }