Aata n practice
class prime
{
static void prime_N(int N)
{
int x, y, flg;
System.out.println( "All the Prime numbers within 1 and " + N + " are:");
for (x = 1; x <= N; x++)
{
if (x == 1 || x == 0)
continue;
flg = 1;
for (y = 2; y <= x / 2; ++y)
{
if (x % y == 0)
{
flg = 0;
break;
}
}
if (flg == 1)
System.out.print(x + " ");
}
}
public static void main(String[] args)
{
int N = 45;
prime_N(N);
}
}
Simple program to print factorial of given number using recursion
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number :");
int num = sc.nextInt(); //Input the number
if(num>=0)
{
int factorial=findFactorial(num);
System.out.println("The factorial of the entered the number is :"+factorial);
}
else
{
System.out.println("Factorial not possible.");
System.out.println("Please enter valid input.");
}
}
//Recursive Function to Find the Factorial of a Number
public static int findFactorial(int num)
{
if(num==0)
return 1;
else if(num==1)
return 1;
else
return num*findFactorial(num-1);
}
}
Comments
Post a Comment