You are on page 1of 21

1.

Write a program in C++ to display the n terms of even natural number and their
sum.
SOL::

#include <iostream>
using namespace std;
int main()
{
int i, n, sum = 0;
cout << " Input number of terms: ";
cin >> n;
cout << "\n The even numbers are: ";
for (i = 1; i <= n; i++)
{
cout << 2 * i << " ";
sum += 2 * i ;
}
cout << "\n The Sum of even Natural Numbers upto " << n << " terms: " << sum <<
endl;
system("pause");
}
____________________________________________________________________________________
_____________

2. Write a program in C++ to display the n terms of harmonic series and their sum.
SOL:

#include <iostream>
using namespace std;
int main()
{
int i, n;
float s = 0.0;
cout << "\n\n Display n terms of harmonic series and their sum:\n";
cout << " Input number of terms: ";
cin >> n;
for (i = 1; i <= n; i++)
{
if (i < n)
{
cout << "1/" << i << " + ";
s += 1 / (float)i;
}
if (i == n)
{
cout << "1/" << i;
s += 1 / (float)i;
}
}
cout << "\n The sum of the series upto " << n << " terms: " << s << endl;
system("pause")

}
____________________________________________________________________________________
_____

3. Write a program in C++ to display the sum of the series [ 9 + 99 + 999 + 9999
...].
Sol

#include <iostream>
using namespace std;

int main()
{
long int n, i, t = 9;
int sum = 0;
cout << " Input number of terms: ";
cin >> n;

for (i = 1; i <= n; i++)


{
sum += t;
cout << t << " ";
t = t * 10 + 9;
}
cout << "\n The sum of the sarise = " << sum << endl;
system("pause")
}

____________________________________________________________________________________
_____

4. Write a program in C++ to display the sum of the series [ 1+x+x^2/2!+x^3/3!+....]


SOL::

#include <iostream>
using namespace std;
int main()
{
float x, sum, no_row;
int i, n;
cout << " Input the value of x: ";
cin >> x;
cout << " Input number of terms: ";
cin >> n;
sum = 1;
no_row = 1;
for (i = 1; i < n; i++)
{
no_row = no_row * x / (float)i;
sum = sum + no_row;
}
cout << " The sum is : " << sum << endl;
system("pause")
}

____________________________________________________________________________________
_________

5.Write a program in C++ to find the sum of the series [ x - x^3 + x^5 + ......].
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float x, sum, ctr;
int i, n, m, mm, nn = 0;
cout << "\n\n Display the sum of the series [ x - x^3 + x^5 + ......]\n";
cout << "------------------------------------------------------------\n";
cout << " Input the value of x: ";
cin >> x;
cout << " Input number of terms: ";
cin >> n;
sum = x;
m = -1;
cout << "The values of series:" << endl;
cout << sum << endl;
for (i = 1; i < n; i++) {
ctr = (2 * i + 1);
mm = pow(x, ctr);
nn = mm * m;
cout << nn << endl;
sum = sum + nn;
m = m * (-1);
}
cout << "\n The sum of the series upto " << n << " term is: " << sum << endl;
system("pause")
}
____________________________________________________________________________________
____________

6.Write a program in C++ to find the sum of the series 1 +11 + 111 + 1111 + .. n
terms.
SOL:

#include <iostream>
using namespace std;
int main()
{
int n, i, sum = 0;
int t = 1;
cout << " Input number of terms: ";
cin >> n;
for (i = 1; i <= n; i++)
{
cout << t << " ";
if (i < n)
{
cout << "+ ";
}
sum = sum + t;
t = (t * 10) + 1;
}
cout << "\n The sum of the series is: " << sum << endl;
system("pause");
}
____________________________________________________________________________________
_______

7.Write a program in C++ to display the first n terms of Fibonacci series


Sol:

#include <iostream>
using namespace std;

int main()
{
int prv = 0, pre = 1, trm, i, n;
cout << " Input number of terms to display: ";
cin >> n;
cout << " Here is the Fibonacci series upto to " << n << " terms: "<<endl;
cout << prv << " " << pre;
for (i = 3; i <= n; i++)
{
trm = prv + pre;
cout << " " << trm;
prv = pre;
pre = trm;
}
cout << endl;
system("pause")
}

____________________________________________________________________________________
______________

8.Write a program in C++ to find the number and sum of all integer between 100 and
200 which are divisible by 9.
SOL:

#include <iostream>
using namespace std;

int main()
{
int i, sum = 0;
cout << " Numbers between 100 and 200, divisible by 9: " << endl;
for (i = 101; i < 200; i++)
{
if (i % 9 == 0)
{
cout << " " << i;
sum += i;
}
}
cout << "\n The sum : " << sum << endl;
system("pause")
}
____________________________________________________________________________________
____________

9. Write a program in C++ to find LCM of any two numbers using HCF.
SOL:

#include <iostream>
using namespace std;

int main()
{
int i, n1, n2, j, hcf = 1, lcm;
cout << " Input 1st number for LCM: ";
cin >> n1;
cout << " Input 2nd number for LCM: ";
cin >> n2;
j = (n1 < n2) ? n1 : n2;
for (i = 1; i <= j; i++) {

if (n1 % i == 0 && n2 % i == 0) {
hcf = i;
}
}
lcm = (n1 * n2) / hcf;
cout << " The LCM of " << n1 << " and " << n2 << " is: " << lcm << endl;
system("pause");
}
____________________________________________________________________________________
______________
10.Write a program in C++ to display the number in reverse order.
SOL:

#include <iostream>
using namespace std;

int main()
{
int num, r, sum = 0, t;
cout << " Input a number: ";
cin >> num;
for (t = num; num != 0; num = num / 10)
{
r = num % 10;
sum = sum * 10 + r;
}
cout << " The number in reverse order is : " << sum << endl;
system("pause");
}
____________________________________________________________________________________
____________

31. Write a program in C++ to find out the sum of an A.P. series.
SOL:
#include <iostream>
using namespace std;

int main()
{
int n1, df, n2, i, ln;
int s1 = 0;
cout << " Input the starting number of the A.P. series: ";
cin >> n1;
cout << " Input the number of items for the A.P. series: ";
cin >> n2;
cout << " Input the common difference of A.P. series: ";
cin >> df;
s1 = (n2 * (2 * n1 + (n2 - 1) * df)) / 2;
ln = n1 + (n2 - 1) * df;
cout << " The Sum of the A.P. series are : " << endl;
for (i = n1; i <= ln; i = i + df)
{
if (i != ln)
cout << i << " + ";
else
cout << i << " = " << s1 << endl;

}
system("pause");
}

____________________________________________________________________________________
____________
12. Write a program in C++ to find the Sum of GP series.
SOL:

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float g1,cr,i,n,j;
int ntrm,gpn;
float sum=0;
cout << " Input the starting number of the G.P. series: ";
cin >> g1;
cout << " Input the number of items for the G.P. series: ";
cin >> ntrm;
cout << " Input the common ratio of G.P. series: ";
cin >> cr;
cout<<"\nThe numbers for the G.P. series:\n ";
cout<<g1<<" ";
sum=g1;
for(j=1;j<ntrm;j++)
{
gpn=g1*pow(cr,j);
sum=sum+gpn;
cout<<gpn<<" ";
}
cout<<"\n The Sum of the G.P. series: "<<sum<<endl;
system("pause");
}
____________________________________________________________________________________
_________

13.Write a program in C++ to Check Whether a Number can be Express as Sum of Two
Prime Numbers.
SOL::

#include <iostream>
using namespace std;

int main()
{
int n, i, flg1 = 1, flg2 = 1, flg3 = 0, j;
float sum = 0;
cout << " Input a positive integer: ";
cin >> n;
for (i = 2; i <= n / 2; i++)
{
flg1 = 1;
flg2 = 1;
for (j = 2; j < i; j++)
{
if (i % j == 0)
{
flg1 = 0;
j = i;
}
}
for (j = 2; j < n - i; j++)
{
if ((n - i) % j == 0)
{
flg2 = 0;
j = n - i;
}
}
if (flg1 == 1 && flg2 == 1)
{
cout << n << " = " << i << " + " << n - i << endl;
flg3 = 1;
}
}
if (flg3 == 0)
{
cout << n << " can not be expressed as sum of two prime numbers." << endl;
}
system("pause"):
}

____________________________________________________________________________________
_________________

14.Write a program in C++ to find the length of a string without using the library
function.

SOL::

#include <iostream>
#include <string>
using namespace std;

int main()
{
char str1[50];
int i, l = 0;
cout << " Input a string: ";
cin >> str1;
for (i = 0; str1[i] != '\0'; i++) {
l++;
}
cout << "The string contains " << l << " number of characters." << endl;
cout << "So, the length of the string " << str1 << " is:" << l << endl;
system("pause");
}
____________________________________________________________________________________
________

15.Write a program in C++ to display the pattern like right angle triangle using an
asterisk.
SOL::

#include <iostream>
using namespace std;

int main()
{
int i,j,rows;
cout << " Input number of rows: ";
cin >> rows;
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}
system("pause");
}
____________________________________________________________________________________
___________

16.Write a program in C++ to display the pattern like right angle triangle with
number.
SOL::
#include <iostream>
using namespace std;

int main()
{
int i,j,rows;
cout << " Input number of rows: ";
cin >> rows;
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
cout<<j;
cout<<endl;
}
system("pause");
}
____________________________________________________________________________________
_____________________

17.Write a program in C++ to make such a pattern like right angle triangle using
number which will repeat the number for that row.

SOL::

#include <iostream>
using namespace std;

int main()
{
int i,j,rows;
cout << " Input number of rows: ";
cin >> rows;
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
cout<<i;
cout<<endl;
}
system("pause");
}
____________________________________________________________________________________
_________________

18.Write a program in C++ to make such a pattern like right angle triangle with
number increased by 1.

SOL::

#include <iostream>
#include <string>
using namespace std;

int main()
{
int i,j,rows,k=1;
cout << " Input number of rows: ";
cin >> rows;
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
cout<<k++<<" ";
cout<<endl;
}
system("pause");
}
____________________________________________________________________________________
________

19.Write a program in C++ to make such a pattern like a pyramid with numbers
increased by 1.

SOL::

#include <iostream>
#include <string>
using namespace std;

int main()
{
int i,j,spc,rows,k,t=1;
cout << " Input number of rows: ";
cin >> rows;
spc=rows+4-1;
for(i=1;i<=rows;i++)
{
for(k=spc;k>=1;k--)
{
cout<<" ";
}
for(j=1;j<=i;j++)
cout<<t++<<" ";
cout<<endl;
spc--;
}
system("pause");
}
____________________________________________________________________________________
__________

20.Write a program in C++ to make such a pattern like a pyramid with an asterisk.

SOL::

#include <iostream>
#include <string>
using namespace std;

int main()
{
int i,j,spc,rows,k;
cout << "\n\n Display such a pattern like a pyramid with an asterisk:\n";
cout << "------------------------------------------------------------\n";
cout << " Input number of rows: ";
cin >> rows;
spc=rows+4-1;
for(i=1;i<=rows;i++)
{
for(k=spc;k>=1;k--)
{
cout<<" ";
}
for(j=1;j<=i;j++)
cout<<"*"<<" ";
cout<<endl;
spc--;
}
system("pause");
}

____________________________________________________________________________________
_________________

21.Write a program in C++ to make such a pattern like a pyramid using number and a
number will repeat for a row.
SOL::

#include <iostream>
#include <string>
using namespace std;

int main()
{
int i,j,spc,rows,k;
cout << " Input number of rows: ";
cin >> rows;
spc=rows+4-1;
for(i=1;i<=rows;i++)
{
for(k=spc;k>=1;k--)
{
cout<<" ";
}
for(j=1;j<=i;j++)
cout<<i<<" ";
cout<<endl;
spc--;
}
system("pause");
}

____________________________________________________________________________________
______________
22.Write a program in C++ to display the pattern like a pyramid using asterisk and
each row contain an odd number of asterisks.

SOL::
#include <iostream>
using namespace std;

int main()
{
int i,j,n;
cout << " Input number of rows: ";
cin >> n;
for(i=0;i<n;i++)
{
for(j=1;j<=n-i;j++)
cout<<" ";
for(j=1;j<=2*i-1;j++)
cout<<"*";
cout<<endl;
}
system("pause");
}

____________________________________________________________________________________
____________

23.Write a program in C++ to print the Floyd's Triangle.

SOL::
#include <iostream>
using namespace std;

int main()
{
int i,j,n,p,q;
cout << " Input number of rows: ";
cin >> n;
for(i=1;i<=n;i++)
{
if(i%2==0)
{
p=1;q=0;
}
else
{
p=0;q=1;
}
for(j=1;j<=i;j++)
if(j%2==0)
cout<<p;
else
cout<<q;
cout<<endl;
}
system("pause");
}
____________________________________________________________________________________
__________

24.Write a program in C++ to display the pattern like a diamond.

SOL::

#include <iostream>
using namespace std;

int main()
{
int i,j,r;
cout << " Input number of rows (half of the diamond): ";
cin >> r;
for(i=0;i<=r;i++)
{
for(j=1;j<=r-i;j++)
cout<<" ";
for(j=1;j<=2*i-1;j++)
cout<<"*";
cout<<endl;
}
for(i=r-1;i>=1;i--)
{
for(j=1;j<=r-i;j++)
cout<<" ";
for(j=1;j<=2*i-1;j++)
cout<<"*";
cout<<endl;;
}
system("pause");
}
____________________________________________________________________________________
_______________

25.Write a program in C++ to display Pascal's triangle like pyramid.

SOL::
#include <iostream>
using namespace std;

int main()
{
int row,c=1,blk,i,j;
cout << " Input number of rows: ";
cin >> row;
for(i=0;i<row;i++)
{
for(blk=1;blk<=row-i;blk++)
cout<<" ";
for(j=0;j<=i;j++)
{
if (j==0||i==0)
c=1;
else
c=c*(i-j+1)/j;
cout<<c<<" ";
}
cout<<endl;
}
system("pause");
}
____________________________________________________________________________________
___________

26.Write a program in C++ to display Pascal's triangle like right angle triangle.

SOL::

#include <iostream>
using namespace std;

int main()
{
int row,c=1,blk,i,j;
cout << " Input number of rows: ";
cin >> row;
for(i=0;i<row;i++)
{
for(j=0;j<=i;j++)
{
if (j==0||i==0)
c=1;
else
c=c*(i-j+1)/j;
cout<<c<<" ";
}
cout<<endl;
}
system("pause")
}
____________________________________________________________________________________
________
27.Write a program in C++ to display such a pattern for n number of rows using
number. Each row will contain odd numbers of number. The first and last number of
each row will be 1 and middle column will be the row number.

SOL::
#include <iostream>
using namespace std;

int main()
{
int i,j,n;
cout << " Input number of rows: ";
cin >> n;
for(i=0;i<=n;i++)
{
/* print blank spaces */
for(j=1;j<=n-i;j++)
cout<<" ";
/* Display number in ascending order upto middle*/
for(j=1;j<=i;j++)
cout<<j;

/* Display number in reverse order after middle */


for(j=i-1;j>=1;j--)
cout<<j;
cout<<endl;
}
system("pause");
}
____________________________________________________________________________________
_________

28.Write a program in C++ to display the pattern like pyramid using the alphabet.
SOL::
#include <iostream>
using namespace std;

int main()
{
int i, j;
char alph = 'A';
int n, blk;
int ctr = 1;
cout << " Input the number of Letters (less than 26) in the Pyramid: ";
cin >> n;
for (i = 1; i <= n; i++) {
for (blk = 1; blk <= n - i; blk++)
cout << " ";
for (j = 0; j <= (ctr / 2); j++)
{
cout << alph++ << " ";
}
alph = alph - 2;
for (j = 0; j < (ctr / 2); j++)
{
cout << alph-- << " ";
}
ctr = ctr + 2;
alph = 'A';
cout << endl;
}
system("pause");
}
____________________________________________________________________________________
_____________

29.Write a program in C++ to print a pyramid of digits as shown below for n number
of lines.

SOL::
#include <iostream>
using namespace std;

int main()
{
int i, j, spc, n;
cout << " Input the number of rows: ";
cin >> n;
for (i = 1; i <= n; i++)
{
spc = n - i;
while (spc-- > 0)
cout << " ";
for (j = i; j < 2 * i - 1; j++)
cout << j;
for (j = 2 * i - 1; j > i - 1; j--)
cout << j;
cout << endl;
}
system("pause");
}
____________________________________________________________________________________
______

30.Write a program in C++ to print a pattern like highest numbers of columns appear
in first row.

SOL::
#include <iostream>
using namespace std;

int main()
{
int i, j, n;
cout << " Input the number of rows: ";
cin >> n;
for (i = 1; i <= n;)
{
cout << i;
for (j = i + 1; j <= n;)
{
cout << j;
j = j + 1;
}
cout << endl;
i = i + 1;
}
system("pause");
}
____________________________________________________________________________________
_____________

31.Write a program in C++ to display the pattern using digits with right justified
and the highest columns appears in first row.

SOL::

#include <iostream>
using namespace std;

int main()
{
int i, j, rows;
cout << " Input number of rows: ";
cin >> rows;
for (i = rows; i >= 1; i--)
{
for (j = 1; j <= rows - i; j++)
cout << " ";
for (j = 1; j <= i; j++)
cout << j;
cout << endl;
}
system("pause");
}
____________________________________________________________________________________
____________

32.Write a program in C++ to display the pattern using digits with left justified
and the highest columns appears in first row in descending order.

SOL::

#include <iostream>
using namespace std;

int main()
{
int i, j, rows, d;
cout << " Input number of rows: ";
cin >> rows;
d = 0;
for (i = 1; i <= rows; i++)
{
for (j = rows - d; j >= 1; j--)
{
cout << j << " ";
}
d++;
cout << endl;
}
system("pause");
}
____________________________________________________________________________________
__________

33.Write a program in C++ to display the pattern like right angle triangle with
right justified using digits.

#include <iostream>
using namespace std;

int main()
{
int i, j, rows, d;
cout << " Input number of rows: ";
cin >> rows;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows - i; j++)
{
cout << " ";
}
d = i;
for (j = 1; j <= i; j++)
{
cout << d;
d--;
}
cout << endl;
}
system("pause");
}

____________________________________________________________________________________
___________

34.Write a program in C++ to display the pattern power of 2, triangle.

SOL::
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
int i, j, spc, n, mm, d = 1, k;
cout << " Input the number of rows: ";
cin >> n;
//----------- space for first line ----------------------
for (i = 1; i <= n * 2 + 2 + 5; i++) //extra 5 spaces is the margin from left
cout << " ";
cout << pow(2, 0) << endl;
for (i = 1; i < n; i++)
{
//----------- printing spaces from 2nd line to end -------
for (k = 1; k <= n * 2 - d + 5; k++)
{
cout << " ";
}
//----------- print upto middle ----------------
for (j = 0; j < i; j++)
{
mm = pow(2, j);
cout << mm << " "; //print 2 spaces
}
//------------- print after middle to end -------
for (j = i; j >= 0; j--)
{
mm = pow(2, j);
cout << mm << " "; //print 2 spaces
}
d = d + 3;
cout << endl;
}
system("pause");
}
____________________________________________________________________________________
___________
35.Write a program in C++ to display such a pattern for n number of rows using
number. Each row will contain odd numbers of number. The first and last number of
each row will be 1 and middle column will be the row number. n numbers of columns
will appear in 1st row.

SOL::

#include <iostream>
using namespace std;

int main()
{
int i,j,n;
cout << " Input number of rows: ";
cin >> n;
for(i=n;i>=1;i--)
{
/* print blank spaces */
for(j=1;j<=n+5-i;j++)
cout<<" ";
/* Display number in ascending order upto middle*/
for(j=1;j<=i;j++)
cout<<j;

/* Display number in reverse order after middle */


for(j=i-1;j>=1;j--)
cout<<j;
cout<<endl;
}
system("pause");
}

You might also like