In this page,we will learn some basic java programs for freshers. As a Beginner very useful these Programs.
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<=n;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<n)
{
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 Nubmer?
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:
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<=n;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<n)
{
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 Nubmer?
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:
No comments:
Post a Comment