Professional Documents
Culture Documents
Sample output:
The natural numbers are:
1 2 3 4 5 6 7 8 9 10
ANSWER:
#include<iostream>
using namespace std;
int main()
{
int i, num;
cout << endl << "The natural numbers are:" << num << endl;
for(i = 1; i <= num; i++)
{
cout << i << " ";
}
return 0;
}
2. Write a program in C++ to find the sum of first 10 natural numbers. Sample
Output:
Find the first 10 natural numbers:
---------------------------------------
The natural numbers are:
1 2 3 4 5 6 7 8 9 10
The sum of first 10 natural numbers: 55
#include <iostream>
int main()
int i,sum=0;
sum=sum+i;
cout << "\n The sum of first 10 natural numbers: "<<sum << endl;
3. Write a program in C++ to display n terms of natural number and their sum
Sample Output:
Input a number of terms: 7
The natural numbers upto 7th terms are:
1234567
The sum of the natural numbers is: 28
#include <iostream>
int main()
int n,i,sum=0;
cout << "\n\n Display n terms of natural number and their sum:\n";
cin>> n;
cout << " The natural numbers upto "<<n<<"th terms are: \n";
sum=sum+i;
cout << "\n The sum of the natural numbers is: "<<sum << endl;
4. Write a program in C++ to find the perfect numbers between 1 and 500. The
perfect numbers between 1 to 500 are:
6
28
496
#include <iostream>
int main()
{
cout << "\n\n Find the perfect numbers between 1 and 500:\n";
int i = 1, u = 1, sum = 0;
cout << "\n The perfect numbers between 1 to 500 are: \n";
if (u < i)
if (i % u == 0)
sum = sum + u;
u++;
if (sum == i) {
i++;
u = 1;
sum = 0;
#include <iostream>
using namespace std;
int main()
{
int num1, ctr = 0;
cout << "\n\n Check whether a number is prime or not:\n";
cout << "--------------------------------------------\n";
cout << " Input a number to check prime or not: ";
cin>> num1;
for (int a = 1; a <= num1; a++)
{
if (num1 % a == 0)
{
ctr++;
}
}
if (ctr == 2)
{
cout << " The entered number is a prime number. \n";
}
else {
cout << " The number you entered is not a prime number. \n";
}
}
6. Write a program in C++ to find prime number within a range. Input number for
starting range: 1
Input number for ending range: 100
The prime numbers between 1 and 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
The total number of prime numbers between 1 to 100 is: 25
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int num1,num2;
int fnd=0,ctr=0;
cout << "\n\n Find prime number within a range:\n";
cout << "--------------------------------------\n";
cout << " Input number for starting range: ";
cin>> num1;
cout << " Input number for ending range: ";
cin>> num2;
}
ctr=0;
}
cout<<"\n\n The total number of prime numbers between
"<<num1<<" to "<<num2<<" is: "<<fnd<<endl;
return 0;
}
8. Write a program in C++ to find the last prime number occur before the entered
number. Sample Output:
Input a number to find the last prime number occurs before the number: 50
47 is the last prime number before 50
#include <iostream>
using namespace std;
int main()
{
int num1, ctr = 0;
cout << "\n\n Find the last prime number occurs before the entered
number:\n";
cout << "-----------------------------------------------------------------\n";
cout << " Input a number to find the last prime number occurs before
the number: ";
cin >> num1;
for (int n = num1 - 1; n >= 1; n--)
{
for (int m = 2; m < n; m++)
{
if (n % m == 0)
ctr++;
}
if (ctr == 0)
{
if (n == 1)
{
cout << "no prime number less than 2";
break;
}
cout << n << " is the last prime number before " << num1 <<
endl;
break;
}
ctr = 0;
}
return 0;
}
9. Write a program in C++ to find the Greatest Common Divisor (GCD) of two
numbers. Sample Output:
Input the first number: 25
Input the second number: 15
The Greatest Common Divisor is: 5
#include <iostream>
using namespace std;
int main()
{
int num1, num2, gcd;
cout << "\n\n Find the Greatest Common Divisor of two numbers:\n";
cout << "-----------------------------------------------------\n";
cout << " Input the first number: ";
cin >> num1;
cout << " Input the second number: ";
cin >> num2;
#include <iostream>
using namespace std;
int main()
{
int num1, num2, r, sum=0;
cout << "\n\n Find the sum of digits of a given number:\n";
cout << "----------------------------------------------\n";
cout << " Input a number: ";
cin >> num1;
num2 = num1;
while (num1 > 0)
{
r = num1 % 10;
num1 = num1 / 10;
sum = sum + r;
}
cout << " The sum of digits of " << num2 << " is: " << sum << endl;
}
11. Write a program in C++ to find the sum of the series 1 + 1/2^2 + 1/3^3 + ..+
1/n^n. Sample Output:
Input the value for nth term: 5
1/1^1 = 1
1/2^2 = 0.25
1/3^3 = 0.037037
1/4^4 = 0.00390625
1/5^5 = 0.00032
The sum of the above series is: 1.29126
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double sum = 0, a;
int n, i;
cout << "\n\n Find the sum of the series 1 + 1/2^2 + 1/3^3 +.....+
1/n^n:\n";
cout << "----------------------------------------------------------------\n";
cout << " Input the value for nth term: ";
cin >> n;
for (i = 1; i <= n; ++i)
{
a = 1 / pow(i, i);
cout << "1/" << i << "^" << i << " = " << a << endl;
sum += a;
}
cout << " The sum of the above series is: " << sum << endl;
}
12. Write a program in C++ to calculate the sum of the series (1*1) + (2*2) +
(3*3) + (4*4) + (5*5) + ... + (n*n).
Sample Output:
Input the value for nth term: 5
1*1 = 1
2*2 = 4
3*3 = 9
4*4 = 16
5*5 = 25
The sum of the above series is: 55
#include <iostream>
using namespace std;
int main()
{
int i, n, sum = 0;
cout << "\n\n Find the sum of the series (1*1) + (2*2) + (3*3) + (4*4)
+ (5*5) + ... + (n*n):\n";
cout <<
"------------------------------------------------------------------------------------\n";
cout << " Input the value for nth term: ";
cin >> n;
#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;
}
14. Write a program in C++ to find the sum of series 1 - X^2/2! + X^4/4!-.... upto
nth term. Sample Output:
Input the value of X: 3
Input the value for nth term: 4
term 1 value is: 1
term 2 value is: -4.5
term 3 value is: 3.375
term 4 value is: -1.0125
The sum of the above series is: -1.1375
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float x, sum, term, fct, y, j, m;
int i, n;
y = 2;
cout << "\n\n Find the sum of the series 1 - X^2/2! + X^4/4!-....:\n";
cout << "---------------------------------------------------------\n";
cout << " Input the value of X: ";
cin >> x;
cout << " Input the value for nth term: ";
cin >> n;
sum = 1;
term = 1;
cout << " term 1 value is: " << term << endl;
for (i = 1; i < n; i++)
{
fct = 1;
for (j = 1; j <= y; j++)
{
fct = fct * j;
}
term = term * (-1);
m = pow(x, y) / fct;
m = m * term;
cout << " term " << i + 1 << " value is: " << m << endl;
sum = sum + m;
y += 2;
}
cout << " The sum of the above series is: " << sum << endl;
}
15. 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