Home › Forums › C Programming › exercise
- This topic has 3 replies, 4 voices, and was last updated 17 years, 2 months ago by S.Thananchayan.
Viewing 3 reply threads
- AuthorPosts
- December 5, 2004 at 11:39 pm #1873rovitaParticipant
dear everybody……..!!!!!!!!
i have a problem in my programming studyhow to make a program which the out put is:
enter n=51234567<br />1 2 3 4 5 4 3 2 1<br />2 3 4 5 4 3 2<br />3 4 5 4 3<br />4 5 4<br />5<br /> - December 24, 2004 at 2:09 pm #3125starfishParticipant
hi frd,
just see if u can find this appropriate….
1234567891011121314151617181920212223242526272829303132333435363738#include <iostream><br />using namespace std;<br /><br />main(){<br /><br />int a[10];<br />int n,i,j=0,k;<br /><br />cout<<"Enter the value for n: ";<br />cin>>n;<br />k=n;<br /><br />for (i=0 ;i < 10; i++)<br />a=i + 1;<br /><br />while(k>0){<br />for(i=(0+j);i<n;i++)<br />cout<<a<<" ";<br />for(i=n-2;i>=(0+j);i--)<br />cout<<a<<" ";<br />cout<<endl;<br />j++;<br />for(i=0;i<j;i++)<br />cout<<" ";<br />k--;<br />}<br /><br />return 0;<br />}<br /><br />Output :<br /><br />Enter the value for n: 5<br />1 2 3 4 5 4 3 2 1<br />2 3 4 5 4 3 2<br />3 4 5 4 3<br />4 5 4<br />5 </iostream>plz let me know if you have any further queries
- May 17, 2007 at 9:53 pm #3126hemalathakParticipant
the code doesnt output as expected
when i try this code its output is
1234510 10 10 10 10 10 10 10 10<br />10 10 10 10 10 10 10<br />10 10 10 10 10<br />10 10 10<br />10 - October 10, 2007 at 7:35 am #3127S.ThananchayanParticipant
This one was interesting.
I ended up with this:123456789101112131415161718192021222324252627282930313233343536373839404142#include <iostream><br />using namespace std;<br />#include <vector><br />int main()<br />{<br />cout << "You are only allowed to enter 5" << endl ;<br />std::vector<int>::iterator iter;<br />std::vector</int><int> intVector;<br />int z;<br />cout << "enter a valuen";<br />cin >>z;<br />if (z <5)<br />return main();<br /><br />if (z >5)<br />return main();<br />else<br />for (int i=0; i<z; ++i)<br />intVector.push_back(z - i);<br />reverse(intVector.begin(), intVector.end());<br />for (int i=1; i<z+1; ++i)<br />intVector.push_back(z - i);<br />intVector.pop_back();<br />reverse(intVector.begin(), intVector.end());<br />for (iter = intVector.begin(); iter != intVector.end(); ++iter)<br />cout << *iter;<br />cout << endl;<br />reverse(intVector.begin(), intVector.end());<br />for (iter = intVector.begin() +1; iter != intVector.end()-1; ++iter)<br />cout << *iter;<br />cout << endl;<br />for (iter = intVector.begin() +2; iter != intVector.end()-2; ++iter)<br />cout << *iter;<br />cout << endl;<br />for (iter = intVector.begin() +3; iter != intVector.end()-3; ++iter)<br />cout << *iter;<br />cout << endl;<br />for (iter = intVector.begin() +4; iter != intVector.end()-4; ++iter)<br />cout << *iter;<br />cout << endl;<br />return (0);<br />}</int></vector></iostream>
- AuthorPosts
Viewing 3 reply threads
- The forum ‘C Programming’ is closed to new topics and replies.