# Java Basic Programs

In this page,we will learn some basic java programs for freshers. Earlier, i have shared frequently asked java programs in interview, today we will learn some basic java programs for freshers.

1) Java program to find whether the given number  is Even or Odd?

Ans:

import java.util.Scanner;
class EvenOrOdd
{
public static void main(String[] args)
{
int a;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a value:");
a=sc.nextInt();
if(a%2==0)
{
System.out.println("Given number is Even");
}
else
{
System.out.println("Given number is Odd");
}
}
Output:

2) Java Program to find Factorial of the Given number?

Ans:

import java.util.Scanner;
class FactorialDemo
{
public static void main(String[] args)
{
int n,fact=1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter n value");
n=sc.nextInt();
for(int i=1;i
{
fact=fact*i;
}
System.out.println("factorial of "+n"is"fact);
}
}
Output:

3) Java program to check the given number is perfect or not?

Ans:

import java.util.Scanner;
class PerfectTest
{
public static void main(String[] args)
{
int n;
int i=1;
int sum=0;
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
n=sc.nextInt();
while(i
{
if(n%i==0)
{
sum=sum+i;
}
i++;
}
if(sum==n)

{
System.out.println(i+"is a perfect number");
}
else

{
System.out.println(i+"is not a perfect number");
}
}

Output:

4) Java Program to Count the Digit in a Numer?

ANS:

import java.util.Scanner;
class Number
{
public static void main(String[] args)
{
int num;

int count=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
num=sc.nextInt();
while(num!=0)
{
num=num/10;

count++;
}
System.out.println("total digits are:"+count);

}
}

Output:

5) Java Program to reverse a number?

Ans:
import java.util.Scanner;
class Reverse
{
public static void main(String[] args)
{
int a,b;

int c=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter any number:");
a=sc.nextInt();
while(a>0)
{
b=a%10;
a=a/10;
c=(c*10)+b;
}
System.out.println("reverse number is:"+c)
}
}

Output:

This post first appeared on Learnprogramingbyluckysir, please read the originial post: here

# Share the post

Java Basic Programs

×