Fundmental Java Programs

In this post we will learn frequently asked Basic Java Programs for freshers. As a fresher all these programs should be aware before attend the interview. Let's Look at the following examples.


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.print("Enter a value:");
a=sc.nextInt();
if(a%2==0)
{
System.out.print("Given number is Even");
}
else
{
System.out.print("Given number is Odd");
}
}

Output:




javac EvenOrOdd.java
java EvenOrOdd
Enter a value: 6
Given number is Even

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.print("Enter n value");
n=sc.nextInt();
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.print("factorial of "+n"is"fact);
}
}

Output: 

javac FactorialDemo.java
java FactorialDemo
Enter n value 5
Factorial of 5 is 120

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.print("enter a number");
n=sc.nextInt();
while(i<n)
{
if(n%i==0){
sum=sum+i;
}
i++;
}
if(sum==n)
{
System.out.print(i+"is a perfect number");
}
else{
System.out.print(i+"is not a perfect number");
}
} 

Output:
javac PerfectTest.java
java PerfectTest
enter a number
1
1 is not a perfect number
enter a number
6
6 is a perfect number

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

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.print("Enter a number");
num=sc.nextInt();
while(num!=0)
{
num=num/10;
count++;
}
System.out.print("total digits are:"+count);
}
}
Output:
javac Number.java
java Number
enter a number
567
total digits are: 3


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.print("Enter any number:");
a=sc.nextInt();
while(a>0)
{
b=a%10;
a=a/10;
c=(c*10)+b;
}
System.out.print("reverse number is:"+c)
}
}

Output:


javac Reverse.java
java Reverse
Enter any number: 234
reverse number is: 432

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.print("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.print("the given number is prime");
}
else
System.out.print("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.print("A="+a);
System.out.print("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.print("The given number is palindrome");
}
else{
System.out.print("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.print("given number is armstrong");
else
System.out.print("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

Basics of CPP Programs

In this post you will learn fundamental C++ programs. These programs for freshers or beginners. 1 .write a cpp program to display hello w...