Home Forums C Programming exercise

Viewing 3 reply threads
  • Author
    Posts
    • #1873
      rovita
      Participant

      dear everybody……..!!!!!!!!
      i have a problem in my programming study

      how to make a program which the out put is:
      enter n=5


      1 2 3 4 5 4 3 2 1
      2 3 4 5 4 3 2
           3 4 5 4 3
             4 5 4
                 5

    • #3125
      starfish
      Participant

      hi frd,

      just see if u can find this appropriate….

      #include 
      using namespace std;

      main(){

      int a[10];
      int n,i,j=0,k;

      cout<<"Enter the value for n: ";
      cin>>n;
      k=n;

      for (i=0 ;i < 10; i++)
      a=i + 1;

      while(k>0){
              for(i=(0+j);i                 cout<         for(i=n-2;i>=(0+j);i--)
                      cout<         cout<         j++;
              for(i=0;i                 cout<<" ";
              k--;
      }

      return 0;
      }

      Output :

      Enter the value for n: 5
      1 2 3 4 5 4 3 2 1
      2 3 4 5 4 3 2
          3 4 5 4 3
            4 5 4
              5

      plz let me know if you have any further queries

    • #3126
      hemalathak
      Participant

      the code doesnt output as expected
       
      when i try this code its output is
       

      10 10 10 10 10 10 10 10 10
           10 10 10 10 10 10 10
                10 10 10 10 10
                     10 10 10
                          10

       

    • #3127
      S.Thananchayan
      Participant

      This one was interesting.
      I ended up with this:

      #include 
      using namespace std;
      #include
      int main()
      {
       cout << "You are only allowed to enter 5" << endl  ;
       std::vector::iterator iter;
       std::vector
      intVector;
       int z;
       cout << "enter a valuen";
       cin >>z;
       if (z <5)
            return main();
            
       if (z >5)
        return main();
       else
        for (int i=0; i    intVector.push_back(z - i);
       reverse(intVector.begin(), intVector.end());
       for (int i=1; i   intVector.push_back(z - i);
       intVector.pop_back();
       reverse(intVector.begin(), intVector.end());
       for (iter = intVector.begin(); iter != intVector.end(); ++iter)
        cout << *iter;
       cout << endl;
       reverse(intVector.begin(), intVector.end());
       for (iter = intVector.begin() +1; iter != intVector.end()-1; ++iter)
        cout << *iter;
       cout << endl;
       for (iter = intVector.begin() +2; iter != intVector.end()-2; ++iter)
        cout << *iter;
       cout << endl;
       for (iter = intVector.begin() +3; iter != intVector.end()-3; ++iter)
        cout << *iter;
       cout << endl;
       for (iter = intVector.begin() +4; iter != intVector.end()-4; ++iter)
        cout << *iter;
       cout << endl;
       return (0);
      }
Viewing 3 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.