// Write a program to print prime numbers
import java.util.Scanner;
class PrimeNumbers
{
public static void main(String args[])
{
int n, status = 1, num = 3;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number
of prime numbers you want");
n = in.nextInt();
if (n >= 1)
{
System.out.println("First "+n+"
prime numbers are :-");
System.out.println(2);
}
for ( int count = 2 ; count <=n ; )
{
for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
{
if ( num%j == 0 )
{
status = 0;
break;
}
}
if ( status != 0 )
{
System.out.println(num);
count++;
}
status = 1;
num++;
}
}
}
OUTPUT:-
Enter the number of prime numbers you want
5
First 5 Prime Numbers are:-
2
3
5
7
11
// Write a program to print Factorial Number
import java.util.Scanner;
class Factorial
{
public static void main(String args[])
{
int n, c, fact = 1;
System.out.println("Enter an integer
to calculate it's factorial");
Scanner in = new Scanner(System.in);
n = in.nextInt();
if ( n < 0 )
System.out.println("Number should be
non-negative.");
else
{
for ( c = 1 ; c <= n ; c++ )
fact = fact*c;
System.out.println("Factorial of
"+n+" is = "+fact);
}
}
}
OUTPUT:-
Enter an integer to calculate it's factorial
4
Factorial of 4 is = 24.
import java.util.Scanner;
class PrimeNumbers
{
public static void main(String args[])
{
int n, status = 1, num = 3;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number
of prime numbers you want");
n = in.nextInt();
if (n >= 1)
{
System.out.println("First "+n+"
prime numbers are :-");
System.out.println(2);
}
for ( int count = 2 ; count <=n ; )
{
for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
{
if ( num%j == 0 )
{
status = 0;
break;
}
}
if ( status != 0 )
{
System.out.println(num);
count++;
}
status = 1;
num++;
}
}
}
OUTPUT:-
Enter the number of prime numbers you want
5
First 5 Prime Numbers are:-
2
3
5
7
11
// Write a program to print Factorial Number
import java.util.Scanner;
class Factorial
{
public static void main(String args[])
{
int n, c, fact = 1;
System.out.println("Enter an integer
to calculate it's factorial");
Scanner in = new Scanner(System.in);
n = in.nextInt();
if ( n < 0 )
System.out.println("Number should be
non-negative.");
else
{
for ( c = 1 ; c <= n ; c++ )
fact = fact*c;
System.out.println("Factorial of
"+n+" is = "+fact);
}
}
}
OUTPUT:-
Enter an integer to calculate it's factorial
4
Factorial of 4 is = 24.
No comments:
Post a Comment