Assignment

1. Display an hourglass pattern
2. #include <iostream>
3. #include <string>
4. #include <sstream>
5. #include ...
Using hash ###

#include
#include
#include
#include

<iostream>
<string>
<sstream>
<iomanip>

using namespace std;
const c...
2. Display a diamond pattern
#include <iostream>
using namespace std;
void main()
{
int i, j, k;
int n = 0;
cout << "Progr...
3. Display the Nepal flag
#include <iostream>
using namespace std;
void main()
{
int i, j, k;
int n = 0;
cout << "Program ...
}
Upcoming SlideShare
Loading in …5
×

C programs

360 views
278 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
360
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C programs

  1. 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. 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. 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. 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. 5. }

×