JAVA Sample Programs

JAVA Sample Programs


1) Write a java program to find out :
    i) A number entered by user is even or odd
    ii) Prime or Not
    iii) Armstrong or Not

-----------------------------------------------------------------

 i) A number entered by user is even or odd

import java.lang.*;

import java.util.Scanner;


class Assign

{

public static void main(String[] args) 

{


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

Scanner x = new Scanner(System.in);

int n = x.nextInt();


if(n%2==0)

System.out.println("Even Number");

else

System.out.println("Odd Number");




}

}


Output:

Enter a number: 6

Even Number


ii) Prime or Not:-

import java.lang.*;

import java.util.Scanner;


class Assign

{

public static void main(String[] args) 

{

int c=0;


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

Scanner x = new Scanner(System.in);

int n = x.nextInt();


for(int i=1;i<=n;i++)

{

if(n%i==0)

c++;

}


if(c==2)

System.out.println("Prime Number");

else

System.out.println("Not a Prime Number");




}

}


Output:

Enter a number: 7

Prime Number


iii) Armstrong or Not:-

import java.lang.*;

import java.util.Scanner;


class Assign

{

public static void main(String[] args) 

{


int digits = 0;

double sum = 0;


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

Scanner x = new Scanner(System.in);

int n = x.nextInt();


int m = n;


while(m>0)

{

m = m / 10;

digits++;

}


int p = n;


while(p>0)

{

int l = p % 10;

sum = sum + Math.pow(l,digits);

p = p / 10;

}


if(sum == n)

System.out.println("Armstrong Number");

else

System.out.println("It is not an Armstrong Number");

}

}

Output: 

Enter a number: 153

Armstrong Number

-----------------------------------------------------------------------------------------------

2) Write a java program to calculate sum of digits of a number entered by user

import java.lang.*;

import java.util.Scanner;


class Assign

{

public static void main(String[] args) 

{


int sum = 0;


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

Scanner x = new Scanner(System.in);

int n = x.nextInt();


while(n>0)

{

int l = n % 10;

sum = sum + l;

n = n / 10;

}


System.out.println("Sum of digits of given number is " + sum);

}

}



Output:

Enter a number: 123

Sum of digits of given number is 6


3) Write a java program to check string is palindrome or not also calculate length of the string and find out number of times each character is repeated in that string

import java.lang.*;

import java.util.Scanner;


class Assign

{

public static void main(String[] args) 

{


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

Scanner x = new Scanner(System.in);

String s = x.nextLine();


String strev = " ";

int l = 0;


char string[] = s.toCharArray();


for(int i = 0; i < string.length; i++)

{

char ch = string[i];

strev = ch + strev;

l++;

}


if(strev == s)

System.out.println("String is Palindrome");

else

System.out.println("String is Not Palindrome");


System.out.println("Length of string is "+ l);


for(int i = 0; i<string.length; i++)

{

int c = 0;


for(int j = i+1; j<string.length; j++)

{

if(string[i] == string[j] && string[i] != ' ')

{

c++;

string[j] = '0';

}

}



if(string[i] != '0')

System.out.println(string[i] + " is repeated " + c + " times");



}


}

}



Output:

Enter a string: rvgrvvlblbbh

String is Not Palindrome

Length of string is 12

r is repeated 1 times

v is repeated 2 times

g is repeated 0 times

l is repeated 1 times

b is repeated 2 times

h is repeated 0 times


4) Write a java program to state name of OSI layer and its purpose

import java.lang.*;

import java.util.Scanner;


class Assign

{

public static void main(String[] args) 

{


System.out.print("Enter the OSI model layer number: ");

Scanner x  = new Scanner(System.in);

int n = x.nextInt();


switch(n)

{

case 1:

System.out.println("\nPhysical Layer (Layer 1) :\nThe lowest layer of the OSI reference model is the physical layer. It is responsible for the actual physical connection between the devices.\n");

System.out.println("The functions of the physical layer are as follows: \nBit synchronization: \nBit rate control:\nTransmission mode:\n");


break;

case 2:

System.out.println("\n2. Data Link Layer (DLL) (Layer 2) :\nThe data link layer is responsible for the node-to-node delivery of the message.");

System.out.println("\nThe functions of the Data Link layer are : \nFraming: \nPhysical addressing:\nError control: \nFlow Control: \nAccess control: ");

break;

case 3:

System.out.println("\n3. Netork Layer (Layer 3) :\nThe network layer works for the transmission of data from one host to the other located in different networks.\n");

System.out.println("\nThe functions of the Network layer are :  \nRouting: \nLogical Addressing:");

break;


case 4:

System.out.println("\n4. Transport Layer (Layer 4) :\nThe transport layer provides services to the application layer and takes services from the network layer.");

System.out.println("\nThe functions of the transport layer are as follows:  \nSegmentation and Reassembly:\nService Point Addressing:");

break;


case 5:

System.out.println("\n5. Session Layer (Layer 5) :\nThis layer is responsible for the establishment of connection, maintenance of sessions, authentication, and also ensures security. ");

System.out.println("\nThe functions of the session layer are : \nSession establishment, maintenance, and termination:\nSynchronization: \nDialog Controller:");

break;

case 6:

System.out.println("\n6. Presentation Layer (Layer 6):\nThe presentation layer is also called the Translation layer. The data from the application layer is extracted here and manipulated as per the required format to transmit over the network.");

System.out.println("\nThe functions of the presentation layer are : \nTranslation:\nCompression:");

break;

case 7:

System.out.println("\n7. Application Layer (Layer 7) :\nAt the very top of the OSI Reference Model stack of layers, we find the Application layer which is implemented by the network applications.");

System.out.println("\nThe functions of the Application layer are : \nNetwork Virtual Terminal\nFTAM-File transfer access and management\nMail Services\nDirectory Services");

break;

default:

System.out.println("Invalid Input...!");


}

}

}



Output: 

7. Application Layer (Layer 7) :

At the very top of the OSI Reference Model stack of layers, we find the Application layer which is implemented by the network applications.


The functions of the Application layer are :

Network Virtual Terminal

FTAM-File transfer access and management

Mail Services

Directory Services




Comments