Code for following output * * * * * * * * * using nested loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Code for following output * * * * * * * * * using nested loops

30th Sep 2016, 9:42 AM
Soutik
Soutik - avatar
6 Answers
+ 7
int n = 3; for(int i = 1; i < n * 2; i++){ int stars = i > n ? n * 2 - i : i; for(int s = 0; s <= n - stars; s++){ cout<< " "; } for(int s = 0; s < stars; s++){ cout<< "* "; } cout<<endl; }
30th Sep 2016, 10:39 AM
Max Adams
Max Adams - avatar
+ 1
I've created this int n; start: cout<<"\nEnter the number of lines (odd numbers): "; cin>>n; int m=n; if(n%2==0) {cout<<"Invalid choice.Try again."; goto start;} for(int i=1;i<=m/2+1;i++) { for(int k=1;k<=n/2;k++) cout<<" "; for(int j=1;j<=i;j++) cout<<"* "; cout<<endl; n-=2; } //Next half n=(m/2); int o=n; for(int a=1;a<=o;a++) { for(int b=1;b<=a;b++) cout<<" "; for(int c=1;c<=n;c++) cout<<"* "; cout<<endl; n--; }
1st Oct 2016, 10:24 AM
Soutik
Soutik - avatar
0
#Max Adam Actually I want to input number of lines.. then if I input 7 there comes 13 lines..
1st Oct 2016, 10:19 AM
Soutik
Soutik - avatar
0
can you help me in learning c++ I am beginer in this field plz help me I am thanful to you for that.
18th Dec 2017, 4:47 PM
Khadijaishfaq
0
can u help me atif iqbal
18th Dec 2017, 4:48 PM
Khadijaishfaq
- 4
#include <iostream> using namespace std; int main() { for(int a=1; a<=5;a=a+5){ for(int i=1;i<=5 ;i++){ if(i==1){ cout<<" "<<" "<<"*"<<endl; } if(i==2){ cout<<" "<<"*"<<" "<<"*"<<endl; } if(i==3){ cout<<"*"<<" "<<"*"<<" "<<"*"<<endl; } if(i==4){ cout<<" "<<"*"<<" "<<"*"<<endl; } if(i==5){ cout<<" "<<" "<<"*"; } }//inner for loop }//outer for loop return 0; } //Works well in Code-blocks
30th Sep 2016, 12:37 PM
Atif Iqbal
Atif Iqbal - avatar