0% found this document useful (0 votes)
1 views1 page

Logic and Loop

The document contains two Java programs: the first checks if an integer is divisible by 2 and 5, providing appropriate messages based on the input. The second program demonstrates two methods for calculating the factorial of a number, one using a loop and the other using recursion, with examples for calculating the factorial of 5 and 4. Both programs include user input and output statements to display results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views1 page

Logic and Loop

The document contains two Java programs: the first checks if an integer is divisible by 2 and 5, providing appropriate messages based on the input. The second program demonstrates two methods for calculating the factorial of a number, one using a loop and the other using recursion, with examples for calculating the factorial of 5 and 4. Both programs include user input and output statements to display results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

1)

divisible by
public class divisibleBy2and5 {

public static void main(String[] args) {


// TODO Auto-generated method stub
System.out.println("Enter an integer number:");
Scanner input = new Scanner(System.in);
int x;
x = input.nextInt();
if (x % 2==0){
System.out.println("The integer number you entered is divisible by
2");
}
else{
System.out.println("The integer number you entered is not divisible by
2");
if(x % 5==0){
System.out.println("The integer number you entered is divisible by
5");
}
else{
System.out.println("The interger number you entered is not
divisible by 5");
}
}

2)Factorial number

class FactorialExample{
public static void main(String args[]){
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
Factorial of 5 is: 120

class FactorialExample2{
static int factorial(int n){
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
public static void main(String args[]){
int i,fact=1;
int number=4;//It is the number to calculate factorial
fact = factorial(number);
System.out.println("Factorial of "+number+" is: "+fact);
}
} Factorial of 4 is: 24

You might also like