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<=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 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:
6) Program to find duplicate String elements using HashSet
import java.util.*;
class FindDuplicate{
public static void main(String args[])
{
String [] strArray={"lucky","abc","lucky","reddy","pqr"};
HashSet<String> hs=new HashSet<String>();
for(String arryElement:strArray)
{
if(!hs.add(arryElement))
{
System.out.println("duplicate element is"+arryElement);
}
}
}
}
Output:
duplicate element is lucky
7)Java program to find out the given number is prime or not
class primeNumber
{
public static void main(String args[]){
int n,i,a=0;
n=Integer.parseInt(args[0]);
for(i=1;i<=n;i++)
if(n%i==0)
a++;
if(a==2)
{
System.out.println("the given number is prime");
}
else
System.out.println("the given number is not prime");
}
}
Output:
java primeNumber 5
the given number is prime
java primeNumber 6
the given number is not prime
8) Program for swaping the two numbers
class swap
{
public static void main(String args[])
{
int a,b,temp;
a=20;
b=10;
temp=a;
a=b;
b=temp;
System.out.println("A="+a);
System.out.println("B="+b);
}
}
output:
A=10
B=20
9) program to find out whether the given number is palindrome or not
import java.util.*;
class PalindromeDemo{
public static void main (String args[])
{
int reverse=0,rem;
int number=Integer.parseInt(args[0]);
int n=number;//here we are using n variable to check last time to check
while(number>0)
{
rem=number%10;
reverse=reverse*10+rem;
number=number/10;
}
if(reverse==n)
{
System.out.println("The given number is palindrome");
}
else
{
System.out.println("The given number is not palindrome");
}
}
}
Output:
javac PalindromeDemo.java
java Palindrome 121
the given number is palindrome
java Palindrome 123
The given number is not palindrome
10)program to find the whether the given number is Armstrong or not
import java.io.*;
class Armstrong{
public static void main(String[] args)
{
int num=Integer.parseInt(args[0]);
int n=num;
int check=0,remainder;
while(num>0){
remainder=num%10;
check=check+(int)Math.pow(remainder,3);
num=num/10;
}
if(check==n)
System.out.println("given number is armstrong");
else
System.out.println("given number is not armstrong");
}
}
Output:
javac Armstrong.java
java Armstrong 7
given number is not armstrong
java Armstrong 153
given number is armstrong
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 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:
6) Program to find duplicate String elements using HashSet
import java.util.*;
class FindDuplicate{
public static void main(String args[])
{
String [] strArray={"lucky","abc","lucky","reddy","pqr"};
HashSet<String> hs=new HashSet<String>();
for(String arryElement:strArray)
{
if(!hs.add(arryElement))
{
System.out.println("duplicate element is"+arryElement);
}
}
}
}
Output:
duplicate element is lucky
7)Java program to find out the given number is prime or not
class primeNumber
{
public static void main(String args[]){
int n,i,a=0;
n=Integer.parseInt(args[0]);
for(i=1;i<=n;i++)
if(n%i==0)
a++;
if(a==2)
{
System.out.println("the given number is prime");
}
else
System.out.println("the given number is not prime");
}
}
Output:
java primeNumber 5
the given number is prime
java primeNumber 6
the given number is not prime
8) Program for swaping the two numbers
class swap
{
public static void main(String args[])
{
int a,b,temp;
a=20;
b=10;
temp=a;
a=b;
b=temp;
System.out.println("A="+a);
System.out.println("B="+b);
}
}
output:
A=10
B=20
9) program to find out whether the given number is palindrome or not
import java.util.*;
class PalindromeDemo{
public static void main (String args[])
{
int reverse=0,rem;
int number=Integer.parseInt(args[0]);
int n=number;//here we are using n variable to check last time to check
while(number>0)
{
rem=number%10;
reverse=reverse*10+rem;
number=number/10;
}
if(reverse==n)
{
System.out.println("The given number is palindrome");
}
else
{
System.out.println("The given number is not palindrome");
}
}
}
Output:
javac PalindromeDemo.java
java Palindrome 121
the given number is palindrome
java Palindrome 123
The given number is not palindrome
10)program to find the whether the given number is Armstrong or not
import java.io.*;
class Armstrong{
public static void main(String[] args)
{
int num=Integer.parseInt(args[0]);
int n=num;
int check=0,remainder;
while(num>0){
remainder=num%10;
check=check+(int)Math.pow(remainder,3);
num=num/10;
}
if(check==n)
System.out.println("given number is armstrong");
else
System.out.println("given number is not armstrong");
}
}
Output:
javac Armstrong.java
java Armstrong 7
given number is not armstrong
java Armstrong 153
given number is armstrong
No comments:
Post a Comment