Given an integer n, print the following shapes using asterisks and spaces. For
example, for n = 5 the following shapes are printed
Input:
5
Output:
*
* *
* * *
* * * *
* * * * *
Given an integer n, print the following shapes using asterisks and spaces. For
example, for n = 5 the following shapes are printed
Input:
5
Output:
*
* *
* * *
* * * *
* * * * *
Jump to PostPosting thrice doesn't get you extra credit.
First show us some attempt to solve the problem, then we'll help you with your difficulties.
Posting thrice doesn't get you extra credit.
First show us some attempt to solve the problem, then we'll help you with your difficulties.
#include<iostream>
using namespace std;
int main()
{
for (int i=5; i>=1; i--) // A loop for spaces
{
for (int Space=1; Space <= i ; Space++)
{
cout<<" ";
}// Printing Spaces
for (int Star=5; Star>=i; Star--) // loop for stars
{
cout<<"* "; // Notice that one extra space, that creates the equi. triangle shape.. But don't just copy the code. See how it works. use algorithms to crack the problems. Cheers
}
cout<<endl;
}
}
Hope that helps. But really dude, try to break the algorithms. Try to break the solution in steps.
Even in this example, I first created those spaces. (I couldn't see them, so swapped with dots and when got the perfect result, changed back to spaces)
Then I created those stars. All i needed was one space after them. This is the way you learn. For more, join my page:
www.facebook.com/YourFirstStepTowardsProgramming
ya pritam.das was correct,if u need i would provide you the same program in c language.?!
Here you go.
#include <iostream>
using namespace std;
string printA(int n){
string s = "";
for(int i = 0; i<n; i++)
s += " ";
return s;
}
string printB(int n){
string s = "* ";
for(int i = 0; i<n; i++)
s += "* ";
return s;
}
int main(){
int n;
cout << "Enter value n: "; cin >> n;
cout << endl;
if (n<0 || n > 10)
return 1;
for(int i = 0; i<n; i++){
cout << printA(n-i) << printB(i) << endl;
}
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.