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:


Check out this stream