0% found this document useful (0 votes)
18 views22 pages

Java Programming Examples and Exercises

The document provides an overview of Java programming, including its features, syntax, and the use of a Java compiler and JVM. It includes multiple Java programs demonstrating various concepts such as calculating percentages, finding areas, sorting arrays, handling exceptions, and checking for prime numbers. Each program is accompanied by its output, showcasing practical applications of Java programming.

Uploaded by

vanshikaa.1279
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views22 pages

Java Programming Examples and Exercises

The document provides an overview of Java programming, including its features, syntax, and the use of a Java compiler and JVM. It includes multiple Java programs demonstrating various concepts such as calculating percentages, finding areas, sorting arrays, handling exceptions, and checking for prime numbers. Each program is accompanied by its output, showcasing practical applications of Java programming.

Uploaded by

vanshikaa.1279
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA PROGRAMS

Java is a high-level, class-based, object-oriented programming language that is designed to have as few
implementation dependencies as possible. It is a general-purpose programming language intended to let
programmers write once, run anywhere, meaning that compiled Java code can run on all platforms that support
Java without the need to recompile. Java applications are typically compiled to bytecode that can run on any Java
virtual machine (JVM) regardless of the underlying computer architecture. The syntax of Java is similar to C and
C++, but has fewer low-level facilities than either of them.

A compiler is needed to translate high level program code into machine language code that will
be understood by the computer. After a program is compiled, the machine code can be
executed on the computer, say Windows, for which it was compiled.
A Java compiler instead of translating Java code to machine language code, translates it into
Java Bytecode (a highly optimized set of instructions). When the bytecode (also called a Java
class file) is to be run on a computer, a Java interpreter, called the Java Virtual Machine (JVM),
translates the bytecode into machine code and then executes it.
There are a wide variety of Java Integrated Development Environments (IDEs) available in the
market that come equipped with a text editor and a Java compiler thus simplifying writing,
compiling and executing Java programs. Most of them are freely downloadable from the
Internet. We will be using the open source and free Java Net Beans IDE for writing Java
programs.
A method is a group of statements written to perform a specific task. The method body is
enclosed within a pair of curly braces and contains the statements that the method will
execute. Main is a special method that every Java application must have. When you run a
program, the statements in the main method are the first to be executed.
PROGRAM: Create a program to find total marks and
percentage of a student

public class TotalMarksPercentage {


public static void main(String[] args) {
int total_marks = 400;
double marks_obtained = 346;
double percentage = 0.0;

percentage = (marks_obtained/total_marks)*100;
[Link]("Student1's Percentage = "+percentage);
}
}

OUTPUT

Student1’s Percentage = 86.5


PROGRAM: Create a program to to find the area of
rectangle

public class Area {


public static void main(String[] args) {
int width = 500;
int height = 489;
int area = width*height;
[Link] (“Area of rectangle="+ area);
}
}

OUTPUT

Area of rectangle=244500
PROGRAM: Create a program to find the largest element
in an array

public class Largest {


public static void main(String[] args) {
double[] numArray = { 23.4, -34.5, 50.0, 33.5, 55.5, 43.7, 5.7, -66.5
};
double largest = numArray[0];
for (double num: numArray) {
if(largest < num)
largest = num;
}
[Link]("Largest element = %.2f", largest);
}
}

OUTPUT

Largest element = 55.50


PROGRAM: Write a program to sort the marks using
arrays
import [Link];
public class ArraySort {
public static void main(String(] args) {
String[] names = {"Sleepy", "Doc", "Happy", "Grumpy", "Bashful", "Dopey",
"Sneezy"};
System. [Link]("Names array before sorting: ");
for (int i = 0; i < [Link]; i++)
[Link] (names[i] + " , ");
[Link]();
[Link] (names);
[Link] ("Names Array after sorting: ");
for (int i = 0; i < names. length; i++)
[Link](names[i] +" , ");
[Link]();
}}

OUTPUT
Names array before sorting:
Sleepy , Doc , Happy , Grumpy , Bashful , Dopey , Sneezy ,
Names Array after sorting:
Bashful , Doc , Dopey , Grumpy , Happy , Sleepy , Sneezy ,
PROGRAM: Write a programme to search the position of
a prime number

import [Link].*;
public class Position {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
[Link]("Input a prime number: ");
int n = in .nextInt();
[Link]("Position of the said Prime number: " +
kth_Prime(n));
}
public static int kth_Prime(int n) {
int[] prime_num = new int[10000];
int num = 3;
int i = 0, index = 0;
prime_num[0] = 2;
while (num <= n) {
for (i = 0; i <= index; i++) {
if (num % prime_num[i] == 0) {
break;
}}
if (i > index) {
prime_num[++index] = num; }
num++; }
return index + 1;
}
}

OUTPUT

Input a prime number: 13


Position of the said Prime number: 6
PROGRAM: Write a program in Java to print the square of
every alternate number of an array

class AltSquare {
public static void main( String[] args ) {
int[] arr = {11, 12, 13, 14, 15};
int n = [Link];
for(int i=0; i<n; i = i+2)
[Link](arr[i]*arr[i]);
}
}

OUTPUT

121
169
225
PROGRAM: Write a program in java to enable user to
handle divide by zero exception

import [Link].*;
public class ZeroHandling {
public static void main(String[] args) {
int a = 5;
int b = 0;
try {
[Link](a / b); }
catch (ArithmeticException e) {
[Link]("Divided by zero operation cannot possible");
}
}
}

OUTPUT

Divided by zero operation cannot possible


PROGRAM: Write a program to check whether input
program is prime or not

public class Main {


public static void main(String[] args) {
int num = 29;
boolean flag = false;
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
flag = true;
break;
}
}
if (!flag)
[Link](num + " is a prime number.");
else
[Link](num + " is not a prime number.");
}
}

OUTPUT

29 is a prime number.
PROGRAM: Write a Java Program to find Reverse Number
using While Loop
import [Link].*;
import [Link];
public class Reverse{
public static void main(String[] args) {
int num, rem;
int rev = 0, sum = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter the number: 25 ");
num = [Link]();
num = 25;
do {
rem = num % 10;
rev = rev * 10 + rem;
sum = sum + rem;
num = num / 10;
}
while (num > 0);
[Link]("Reverse of given number: " + rev);
}}

OUTPUT
Enter the number: 25
25
Reverse of given number: 52
PROGRAM: Create a program to find square of a number
& create a programme in Java by importing libraries

import [Link].*;
public class Square {
public static void main(String args[]) {
Scanner sc=new Scanner([Link]);
int no;
[Link]("Enter a number which in integer format: ");
no=[Link]();
[Link]("Square of "+ no + " is: "+(no*no));
}
}

OUTPUT

Enter a number in integer format: 10


Square of 10 is: 100
PROGRAM: Check whether an alphabet is vowel or
consonant using switch statement

public class VowelConsonant {


public static void main(String[] args) {
char ch = 'z';
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
[Link](ch + " is vowel");
break;
default: [Link](ch + " is consonant");
}
}

OUTPUT

z is consonant
PROGRAM: Create a program to compute Quotient and
Remainder

public class QuotientRemainder {


public static void main(String[] args) {

int dividend = 25, divisor = 4;


int quotient = dividend / divisor;
int remainder = dividend % divisor;
[Link]("Quotient = " + quotient);
[Link]("Remainder = " + remainder);
}
}

OUTPUT

Quotient = 6
Remainder = 1
PROGRAM: Create a program to check if a Number is
Positive or Negative

public class PositiveNegative {


public static void main(String[] args) {
double number = 12.3;
if (number < 0.0)
[Link](number + " is a negative number.");
else if ( number > 0.0)
[Link](number + " is a positive number.");
else
[Link](number + " is 0.");
}
}

OUTPUT

12.3 is a positive number.


PROGRAM: Create a program to find Factorial of a
number

public class Factorial {


public static void main(String[] args) {
int num = 10;
long factorial = 1;
for(int i = 1; i <= num; ++i)
factorial *= i;
[Link]("Factorial of %d = %d", num, factorial);
}}

OUTPUT

Factorial of 10 = 3628800
PROGRAM: Create a program to Java Program to display
Alphabets (A to Z)

class Main {
public static void main(String[] args) {
char c;
for(c = 'A'; c <= 'Z'; ++c)
[Link](c + " ");
}}

OUTPUT
ABCDEFGHIJKLMNOPQRSTUVWXYZ
PROGRAM: Create a program to generate Multiplication
Table

public class MultiplicationTable {


public static void main(String[] args) {
int num = 9, i = 1;
while(i <= 10)
{
[Link]("%d * %d = %d \n", num, i, num * i);
i++;
}
}}

OUTPUT
9*1=9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
PROGRAM: Create a program to count Number of Digits
in an Integer

public class Main {


public static void main(String[] args) {
int count = 0, num = 0003452;
while (num != 0) {
num /= 10;
++count;
}
[Link]("Number of digits: " + count);
}
}

OUTPUT

Number of digits: 4
PROGRAM: Create a program to find the Sum of Natural
Numbers using for loop

public class SumNatural {


public static void main(String[] args) {
int num = 100, sum = 0;
for(int i = 1; i <= num; ++i)
{ sum += i; }
[Link]("Sum = " + sum);
}
}

OUTPUT

Sum = 5050
PROGRAM: Create a Java Program to Check a Leap Year

public class LeapYear {


public static void main(String[] args) {
int year = 1900;
boolean leap = false;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
leap = true;
else
leap = false; }
else
leap = true; }
else
leap = false;
if (leap)
[Link](year + " is a leap year.");
else
[Link](year + " is not a leap year.");
}}

OUTPUT
1900 is not a leap year
PROGRAM: Get the length of the string and Create a
program to print its character by character in reverse
order.

public class StrLenght {


public static void main(String[] args) {
String str1= "Hello World!";
[Link]("The length of the string: "+ [Link]());
[Link]("Actual String: "+str1);
[Link]("String reverse: ");
for(int i = [Link]()-1; i>=0; i--){
[Link]([Link](i));
}
}
}

OUTPUT

The length of the string: 12


Actual String: Hello World!
String reverse: !dlroW olleH

You might also like