Di Posting Oleh : Simple Learning
Kategori : C++ shapes hollow square cplusplus square shape in c++ square shape in c++ using for loop
Brief Explanation of Code:
Sample Outputs:(click on images to view large)
With Different Input
- Program takes input form user one is height and other is width of square and print respective size of hollow square on console
- There is one main for loop in a program which runs equal to height of square. Other 3 loops runs for width in side main loop
- There is one if , else if and else statement in the program each contain a for
C++ Code
Scroll Down to See Image View
Scroll Down to See Image View
- #include<iostream>
- using namespace std;
- main()
- {
- int squareHeight, squareWidth;
- cout<< "Enter Height: ";
- cin>> squareHeight;
- cout<< "Enter Widht: ";
- cin>> squareWidth;
- for(int width=1; width<=squareHeight; width++)
- {
- if(width <= 1)
- for(int width=1; width<=squareWidth; width++)
- {
- cout<< "*";
- }
- else if(width<squareHeight)
- {
- cout<< endl;
- for(int width2=1; width2<=squareWidth; width2++)
- {
- if(width2==1 || width2==squareWidth)
- cout<< "*";
- else
- cout<< " ";
- } //end of for variable width2
- }// end of else if
- else
- {
- cout<< endl;
- for(int width3=1; width3<=squareWidth; width3++)
- {
- cout<<"*";
- }//end of for having variable width3
- }// end of else
- }// end of first for loop
- }// end of main function
square shape in c++ using for loop c++ code |
Sample Outputs:(click on images to view large)
sample output square shape in c++ using for loop c++ code |
With Different Input
For more shape Programs Visit Here
C++ Shape Programs
C++ Shape Programs
0 Response to "C++ program to print a hollow square shape using for loops, if else and asterisks"
Post a Comment