Write a program that takes a number (N) as input and if it is a prime number, calculate the summation for the following series.
Problem: Write a program that takes a number (N) as input and if it is a prime number, calculate the summation for the following series.
#include<iostream>
using namespace std;
int main()
{
int n;
int sum=0;
cout<<" "<<"Enter any positive number:"<<" ";
cin>>n;
for(int i=2;i<3;i++)
{
if(n==0 || n==1)
{
cout<<"Error!try another number."<<endl;
cout<<"Prime number is start from 2."<<endl;
}
else if(n%2!=0)
{
cout<<" "<<n<<" "<<"is a prime number."<<endl;
}
else{
cout<<" "<<n<<" "<<"is not a prime number."<<endl;
break;
}
for(int i=2;i<=n;i++)
{
cout<<" "<<"The random number till"<<" "<<n<<" "<<"is:"<<" "<<i<<endl;
sum+=i;
}
cout<<endl;
cout<<" "<<"The sum of this random number:"<<" "<<sum<<endl;
}
}
Output :
No comments