write a program using nested loop to produce the following output: & & & & & & & & & & & & & & & & write a program using nested loop to produce the following output: & & & & & & & & & & & & & & & & @Computer Science
public class Printand { public static void main (String args[]){ System.out.println("&&&&&&&"); System.out.println(" &&&&& "); System.out.println(" &&& "); System.out.println(" & ");
umm .... nested loop?
for(int i =4 ; i>=0 ; i--) { if(i==4) System.out.println("&&&&&&&"); else if(i==3) System.out.println(" &&&&& "); else if(i==2) System.out.println(" &&& "); else if(i==1){ System.out.println(" & ");
that's not nested loop, you should add one within other. here is your C++ code: #include <iostream> using namespace std; int main(void){ int row=7,col; for(col=4;col>=1;col--){ for(int null=4;null>col;null--) cout<<" "; for(row=row-2*(4-col);row>=1;row--){ cout<<"A"; } row=7; cout<<"\n"; } return(0); }
If you want to find out the logic, this one is better for you: #include <iostream> using namespace std; int main(void){ int row,col,temp=7,space,a=0; for(col=4;col>=1;col--){ for(space=0;space<a;space++){ cout<<" "; } for(row=temp;row>=1;row--){ cout<<"&"; } a=a+1; temp=temp-2; cout<<"\n"; } return(0); }
thanks a lot....:)
Join our real-time social learning platform and learn together with your friends!