You are on page 1of 10

1.

Write a C++ program that will print the following


pattern
*******

******

*****

****

***

**

#include <cstdlib>

#include <iostream>

#include<iomanip>

#include<cmath>

using namespace std;

int main(int argc, char *argv[])

int i=7;
int j=7;

for(i=1;i<=7;i++){

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

cout<<"\n";

cout<<"\n";

system("PAUSE");

return EXIT_SUCCESS;

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

#include <iostream>
using namespace std;

int main()
{
int n, i, sum = 0;
int t = 1;
cout << "\n\n Display the sum of the series 1 +11 +
111 + 1111 + .. n terms:\n";
cout << "--------------------------------------------------------------
-----\n";
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;
}
3. Write a language program in C++ which accepts the user's first and last
name and print them in reverse order with a space between them
Input First Name: Alexandra
Input Last Name: Abramov
Name in reverse is: Abramov Alexandra
# include <iostream>
# include <string>
using namespace std;
int main()

{
char fname[30], lname [30];
cout << "\n\n Print the name in reverse where last name comes
first:\n";
cout << "-----------------------------------------------------------\n";
cout << " Input First Name: ";
cin >> fname;
cout << " Input Last Name: ";
cin >> lname;

cout << " Name in reverse is: "<< lname << " "<< fname <<endl;
cout << endl;
return 0;
}

4. Write a program in C++ to find the sum of first 10 natural numbers.


#include <iostream>

using namespace std;

int main()

int i,sum=0;

cout << "\n\n Find the first 10 natural numbers:\n";

cout << "---------------------------------------\n";


cout << " The natural numbers are: \n";

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

cout << i << " ";

sum=sum+i;

cout << "\n The sum of first 10 natural numbers: "<<sum << endl;

5. Write a program in C++ to find the factorial of a number.


Sample output:
Input a number to find the factorial: 5
The factorial of the given number is: 120

#include <iostream>
using namespace std;
int main()
{
int num1,factorial=1;
cout << "\n\n Find the factorial of a number:\n";
cout << "------------------------------------\n";
cout << " Input a number to find the factorial: ";
cin>> num1;
for(int a=1;a<=num1;a++)
{
factorial=factorial*a;
}
cout<<" The factorial of the given number is: "<<factorial<<endl;
return 0;
}

6. Write a program in C++ to calculate the series (1) + (1+2) + (1+2+3) +


(1+2+3+4) + ... + (1+2+3+4+...+n).
Sample Output:
Input the value for nth term: 5
1=1
1+2 = 3
1+2+3 = 6
1+2+3+4 = 10
1+2+3+4+5 = 15
The sum of the above series is: 35

#include <iostream>

using namespace std;

int main()

int i, j, n, sum = 0, tsum;

cout << "\n\n Find the sum of the series (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... +
(1+2+3+4+...+n):\n";

cout << "------------------------------------------------------------------------------------------\n";

cout << " Input the value for nth term: ";

cin >> n;

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

tsum = 0;

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

sum += j;

tsum += j;

cout << j;

if (j < i)

cout << "+";

}
cout << " = " << tsum << endl;

cout << " The sum of the above series is: " << sum << endl;

7. Write a program in C++ to asked user to input positive integers to


process count, maximum, minimum, and average or terminate the
process with -1.
Sample Output:
Your input is for termination. Here is the result below:
Number of positive integers is: 4
The maximum value is: 9
The minimum value is: 3
The average is 6.00
#include <iostream>
#include <climits>
#include <iomanip>
using namespace std;

int main()
{
int posnum, ctr, sum, max = 0;
int min = INT_MAX;
int terval = -1;
cout << "\n\n Input a positive integers to calcute some processes or -1 to terminate:\n";
cout << "----------------------------------------------------------------------------\n";
cout << " Input positive integer or " << terval << " to terminate: ";
while (cin >> posnum && posnum != terval)
{
if (posnum > 0)
{
++ctr;
sum += posnum;
if (max < posnum)
max = posnum;
if (min > posnum)
min = posnum;
}
else
{
cout << "error: input must be positive! if negative, the value will only be -1! try
again..." << endl;
}
cout << " Input positive integer or " << terval << " to terminate: ";
}
cout << "\n Your input is for termination. Here is the result below: " << endl;
cout << " Number of positive integers is: " << ctr << endl;
if (ctr > 0)
{
cout << " The maximum value is: " << max << endl;
cout << " The minimum value is: " << min << endl;
cout << fixed << setprecision(2);
cout << " The average is " << (double)sum / ctr << endl;
}
}

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

Sample Output:
Input number of rows: 5
1
22
333
4444
55555

#include <iostream>
using namespace std;

int main()
{
int i,j,rows;
cout << "\n\n Display the pattern using number repeating for a row:\n";
cout << "----------------------------------------------------------\n";
cout << " Input number of rows: ";
cin >> rows;
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
cout<<i;
cout<<endl;
}
}
9. Write a program in C++ to make such a pattern like a pyramid with an
asterisk.
Sample Output:
Input number of rows: 5
*
* *
* * *
* * * *
* * * * *
#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--;
}
}
10. Write a program in C++ to calculate product of digits of any number.
Sample Output:
Input a number: 3456
The product of digits of 3456 is: 360

#include <iostream>
using namespace std;
int main()
{
int num1, num2, r, pro=1,i;
cout << "\n\n Find the product of digits of a given number:\n";
cout << "--------------------------------------------------\n";
cout << " Input a number: ";
cin >> num1;
num2 = num1;
for(i=num1;i>0;i=i/10)
{
r = i % 10;
pro = pro*r;
}
cout << " The product of digits of " << num2 << " is: " << pro
<< endl;
}

You might also like