Common Programs Prime Number up to N number / First Ten Prime numbers / Factorial of a given number

Language

Submitted by:-

Akhilesh Sharma India / Punjab / Nawanshahr.

(+91)(0)9463008090

 

 

1. //Program to find the factorial of given number;

#include<iostream.h>

#include<conio.h>

void main()

{

long num,fact=1;

clrscr();

cout<<"\n Enter the number to find the factorial: ";

cin>>num;

for(;num>0;)

{

fact=fact*num;

num--;

}

cout<<"Factorial is :"<<fact;

getch();

}

/**********************

2. //Prime numbers up to N number

#include<iostream.h>

#include<conio.h>

#include<ctype.h>

void main()

{

 int num,i,j;

 clrscr();

 cout<<"Please Enter the Number upto which prime numbers are to be generated :";

 cin>>num;

 //Validation

 while(num<=0)

 {

 cout<<"Please Enter a Number greater than or equal to 1";

 cin>>num;

 }

 cout<<"1 ";

 for(i=2;i<=num;i++)

 {

  for(j=2;j<=num;j++)

  {

   if(i%j==0)

   break;

  }//end of Inner for Loop

  if(i==j)

  cout<<j<<" ";

 }//End of Outer for Loop

 getch();

}

//************

3. // To find the first 10 prime numbers

#include<iostream.h>

#include<conio.h>

void prime(int i, int c, int n, int j) /* This function can be used to find prime numbers to any

extent ( first 10 here) by simply changing the while statement */

{

 while (c <=10)

 {

  n=0;

  i +=1;

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

  {

   if ( i%j == 0)

   {

    n +=1;   

   }

  }

  if (n <=2 && n != 0)

  {

   cout<<i<<"\n";

   c +=1;

  }

 }

}

void main()

{

 int i=0,c=1,n,j;

 prime(i,c,n,j);

 cout<<"\n\nPress ENTER to close this Window";

 getch();

}

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment