Factorial Using Loop in C++
#include
#include
using namespace std;
int main()
{
int counter, n, fact = 1;
cout<<"Enter the Number :";
cin>>n;
for (int counter = 1; counter <= n; counter++)
{
fact = fact * counter;
}
cout<
getch();
return 0;
}
Output
Enter the Number :6
6 Factorial Value Is
720
Factorial Using Recursion in C++
#include
#include
using namespace std;
long factorial(int);
int main()
{
int counter, n;
cout<<"Enter the Number :";
cin>>n;
cout<
getch();
return 0;
}
long factorial(int n)
{
if (n == 0)
return
1;
else
return(n
* factorial(n-1));
}
Output
Enter the Number :6
6 Factorial Value Is
720
Find Prime Number (Method1) in C++
#include
#include
#include
using namespace std;
int main()
{
int n;
cout<<"Enter the Number :";
cin>>n;
cout<<"List Of Prime
Numbers Below "<
for (int i=2; i
for (int j=2; j*j<=i; j++)
{
if (i % j == 0)
break;
else if (j+1 > sqrt(i))
{
cout << i << endl;
}
}
getch();
return 0;
}
Output
Enter the Number :50
List of Prime Numbers Below 50
5
7
11
13
17
19
23
29
31
37
41
43
47
Find Prime Number ( Method2 ) in C++
#include
#include
#include
using namespace std;
int main()
{
int n;
cout<<"Enter the Number :";
cin>>n;
cout<<"List Of Prime Numbers Below "<
for (int i=2; i
{
bool prime=true;
for (int j=2; j*j<=i; j++)
{
if (i % j == 0)
{
prime=false;
break;
}
}
if(prime) cout << i << endl;
}
getch();
return 0;
}
Output
Enter the Number :50
List Of Prime Numbers
Below 50
5
7
11
13
17
19
23
29
31
37
41
43
47
No comments:
Post a Comment