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
too big.. check this out
ReplyDelete#include
#include
int main()
{
int h,n,i,j;
printf("Input the nos. of rows and columns greater than 4:\n");
scanf("%d%d",&n,&h);
for(i=1;i<=h;i++)
{
for(j=1;j<=n;j++)
{
if(j>1 && j1 && i<h)
printf(" ");
else
printf("*");
}
printf("\n");
}
getch();
return 0;
}
i m not able to understand what is written inside if statement. plz write it correctly
DeleteNo doubt its easy to get and well explained ......:)
ReplyDeleteadd more c++ projects and programs with the explanation and logic :)
You Can Find More Here
DeleteC++ Simple Examples
Beautifully explained my dear mahira
DeleteNice programs.I have learned a lot big up
ReplyDeleteGood To Know.
DeleteYou Can Find More Programs Here.
C++ Examples Code
#include
ReplyDeleteint main()
{
int h,w,r,c;
printf("Give height : ");
scanf("%d",&h);
printf("Give width : ") ;
scanf("%d",&w) ;
for(r=0;r<h;r++)
{
for(c=0;c<w;c++)
{
if(r==0||r==(h-1)||c==0||c==(w-1))
{
printf("* ");
}
else
printf(" ");
}
printf("\n") ;
}
return 0;
}
This is the right code... The upper one given doesn't work
DeleteBut if we want to write in this asteriks square what is the process plzzz any one can tell
ReplyDeleteIf we want to write in this asteriks square then what is the process
ReplyDelete