Ad

Sunday, July 01, 2007

Simple Problems in C++ Part IV

Take a break and have a look at these problems!

Problem 1: Write a Program in C++ to print the following pattern.

Example:

**********
 ********
  ******
   ****
    **

Solution:


  // Example Program in C++
  // to print a pattern
  // that looks like a reverse
  // pyramid of '*'
  #include<iostream.h>

  void main(void)
  {
   int i,j,k;

   for(i=0;i<=10;i++)
   {
    for(j=5;j<i+5;j++)
      cout<<" ";

    for(j=5;j>i;j--)
      cout<<"*";

    for(j=5;j>i;j--)
      cout<<"*";

    cout<<endl;
   }
  }

Problem 2: Write a program in C++ to find the row sum and column sum of a two dimensional array. Example:


 1   2   3  = 6
 4   5   6  = 15
 7   8   9  = 24
     
 12  15 18

Solution:

  // Example Program in c++ to find the
  // row sum and column sum of a 2
  // dimensional array
  #include<iostream.h>

  void main(void)
  {
   int arr[3][3]={1,2,3,4,5,6,7,8,9};
   int row_sum[3], col_sum[3];
   int i,j;

   for(i=0;i<3;i++)
   {
    row_sum[i]=0;
    for(j=0;j<3;j++)
    row_sum[i]+=arr[i][j];
   }

   for(i=0;i<3;i++)
   {
    col_sum[i]=0;
    for(j=0;j<3;j++)
    col_sum[i]+=arr[j][i];
   }

   for(i=0;i<3;i++)
   {
    for(j=0;j<3;j++)
      cout<<" "<<arr[i][j]<<" ";

    cout<<"= "<<row_sum[i];
    cout<<endl;
   }

   cout<<"  \n\n";
   for(i=0;i<3;i++)
     cout<<col_sum[i]<<" ";

   cout<<"\n\n";
  }

Problem 3: Write a Program in C++ to find the fibonacci series (in fibonacci series every term is the sum of its preceeding two terms).

Example:

Enter No. of elements(>3):7
 
0 1 1 2 3 5 8

Solution:

  // Example program in C++ to
  // print the fibonacci series
  #include<iostream.h>

  void main(void)
  {
   int first=0,second=1,third;
   int element,i;

   cout<<"Enter No. of elements(>3):";
   cin>>element;
   cout<<"\n\n";

   cout<<first<<" "<<second;
   for(i=2;i<element;i++)
   {
    third=first+second;
    cout<<" "<<third;

    first=second;
    second=third;
   }

   cout<<"\n\n";
  }

Good-Bye!

Related Articles:

6 comments:

  1. Thank you Very much it's so helpfull I've a task to Print Fibonacci in C++ .
    I hope u could make more example like this.
    i'm sorry my English was bad Coz I'm Indonesian see ya
    email
    aris_4riyadi@yahoo.com

    ReplyDelete
  2. @wizardian

    It's my pleasure.

    Enjoy!

    ReplyDelete
  3. Anonymous3:31 AM

    That was mad hard. I couldn't even do one of them.

    ReplyDelete
  4. Hi Anonymous,

    Then you should perhaps first try the first part:

    http://learning-computer-programming.blogspot.com/2007/05/simple-problem-in-c.html

    and then move on to the other ones.

    ReplyDelete
  5. Anonymous2:37 PM

    can you help with my project in c++...500 lines..i have no idea what program i will do..

    ReplyDelete
  6. Anonymous3:10 AM

    Helpful! :)
    Thanks
    XOXO :)

    ReplyDelete

You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.