Computer Science, asked by aditya11351, 1 year ago

write a program in Java to input a number and print whether it is an even or odd, if it is an even print it's factors otherwise print it's factorial​

Answers

Answered by rakeshchennupati143
5

Program:

import java.util.Scanner;

public class EvenOdd {

     public static void main(String[] args) {

           Scanner reader = new Scanner(System.in);

           System.out.print("Enter a number: ");

           int num = reader.nextInt();

           if(num % 2 == 0){

                 System.out.println(num + " is even and its factors are : ");

                 for ( int i = 0 ; i < num ; i++){

                       if ( num % i == 0){

                             System.out.print(i+" ");

                       }

                 }

           }else{

                 System.out.println(num + " is odd and it factorial is : ");

                 int fact = 1;

                 for ( int i = 0 ; i < num ; i++ ){

                       fact = fact * num;

                       num--;

                 }

                 System.out.print(fact);

           }

     }

}

Output-1:

Enter a number to check even or odd : 20

20 is even its factors are 1,2,4,5,10

Output-2:

Enter a number to check even or odd : 9

20 is even its factors are 362880

---Hope my program will help you ( mark brainliest if you like my answer :) )

Answered by AestheticDude
14

Solution :

The given problem is solved using the language - Java .

class Even_odd

{

void display(int n)  //  Formal parameter

{

if (n%2==0)   // Modulus

System.out.printIn("Even Number" +n);

else

System.out.printIn("Odd Number" +n);

}

}

Now to check whether the no. is even, divide the number by 2 and check the remainder obtained in it . If it is 0 , the no. is even else not.

A output of it is given in the attachment you can check for it .

Attachments:
Similar questions