[Go to site: main page, start]

0% found this document useful (0 votes)
2 views11 pages

Java Programs

Its about projects building in java programing language.

Uploaded by

Faizan Dot
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)
2 views11 pages

Java Programs

Its about projects building in java programing language.

Uploaded by

Faizan Dot
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

1.

Java Program to check Even or Odd number


import [Link];

class CheckEvenOdd
{
public static void main(String args[])
{
int num;
[Link]("Enter an Integer number:");
Scanner input = new Scanner([Link]);
num = [Link]();
if ( num % 2 == 0 )
[Link]("Entered number is even");
else
[Link]("Entered number is odd");
}
}

Output 1:

Enter an Integer number:


78
Entered number is even
Output 2:

Enter an Integer number:


77
Entered number is odd
2. Program to check Vowel or Consonant using Switch Case
import [Link];
class JavaExample
{
public static void main(String[ ] arg)
{
boolean isVowel=false;;
Scanner scanner=new Scanner([Link]);
[Link]("Enter a character : ");
char ch=[Link]().charAt(0);
[Link]();
switch(ch)
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
case 'A' :
case 'E' :
case 'I' :
case 'O' :
case 'U' : isVowel = true;
}
if(isVowel == true) {
[Link](ch+" is a Vowel");
}
else {
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
[Link](ch+" is a Consonant");
else
[Link]("Input is not an alphabet");
}
}
}
Output 1:
Enter a character :
A
A is a Vowel
Output 2:
Enter a character :
P
P is a Consonant
Output 3:
Enter a character :
9
Input is not an alphabet
3. Program to find the average of numbers using array
public class JavaExample {

public static void main(String[] args) {


double[] arr = {19, 12.89, 16.5, 200, 13.7};
double total = 0;

for(int i=0; i<[Link]; i++){


total = total + arr[i];
}
double average = total / [Link];
[Link]("The average is: %.3f", average);
}
}
Output:
The average is: 52.418
4. Program to reverse the array
import [Link];
public class Example
{
public static void main(String args[])
{
int counter, i=0, j=0, temp;
int number[] = new int[100];
Scanner scanner = new Scanner([Link]);
[Link]("How many elements you want to enter: ");
counter = [Link]();
for(i=0; i<counter; i++)
{
[Link]("Enter Array Element"+(i+1)+": ");
number[i] = [Link]();
}
j = i - 1;
i = 0;
[Link]();
while(i<j)
{
temp = number[i];
number[i] = number[j];
number[j] = temp;
i++;
j--;
}

[Link]("Reversed array: ");


for(i=0; i<counter; i++)
{
[Link](number[i]+ " ");
}
}
}
Output:

How many elements you want to enter: 5


Enter Array Element1: 11
Enter Array Element2: 22
Enter Array Element3: 33
Enter Array Element4: 44
Enter Array Element5: 55
Reversed array: 55 44 33 22 11
5. Program to Sort an Array in Ascending Order
import [Link];
public class JavaExample
{
public static void main(String[] args)
{
int count, temp;

//User inputs the array size


Scanner scan = new Scanner([Link]);
[Link]("Enter number of elements you want in the array: ");
count = [Link]();

int num[] = new int[count];


[Link]("Enter array elements:");
for (int i = 0; i < count; i++)
{
num[i] = [Link]();
}
[Link]();
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++) {
if (num[i] > num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
[Link]("Array Elements in Ascending Order: ");
for (int i = 0; i < count - 1; i++)
{
[Link](num[i] + ", ");
}
[Link](num[count - 1]);
}
}

Output:
Enter number of elements you want in the array: 5
Enter array elements:
90
-20
8
11
3
Array Elements in Ascending Order: -20, -3, 8, 11, 90
6. Java program to print Floyd’s triangle

import [Link];
class FloydTriangleExample
{
public static void main(String args[])
{
int rows, number = 1, counter, j;
//To get the user's input
Scanner input = new Scanner([Link]);
[Link]("Enter the number of rows for floyd's triangle:");
//Copying user input into an integer variable named rows
rows = [Link]();
[Link]("Floyd's triangle");
[Link]("****************");
for ( counter = 1 ; counter <= rows ; counter++ )
{
for ( j = 1 ; j <= counter ; j++ )
{
[Link](number+" ");
//Incrementing the number value
number++;
}
//For new line
[Link]();
}
}
}
Output:

Enter the number of rows for floyd's triangle:


6
Floyd's triangle
****************
1
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
7. Program to print fibonacci series using for loop

public class JavaExample {

public static void main(String[] args) {

int count = 7, num1 = 0, num2 = 1;


[Link]("Fibonacci Series of "+count+" numbers:");

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


{
[Link](num1+" ");
int sumOfPrevTwo = num1 + num2;
num1 = num2;
num2 = sumOfPrevTwo;
}
}
}
Output:

Fibonacci Series of 7 numbers:0 1 1 2 3 5 8

8. Write a Java program to display the half pyramid star pattern.


*
**
***
****
*****

public class Pyramid {


public static void main(String[] args) {
// outer loop for row
for (int i=1; i <= 5; i++) {
// inner loop for column
for(int j=1; j <= i; j++) {
// print star
[Link]("* ");
}
// new line
[Link]();
}
}
}
9. ATM program Java

import [Link];

//create ATMExample class to implement the ATM functionality


public class ATMExample
{
//main method starts
public static void main(String args[] )
{
//declare and initialize balance, withdraw, and deposit
int balance = 100000, withdraw, deposit;

//create scanner class object to get choice of user


Scanner sc = new Scanner([Link]);

while(true)
{
[Link]("Automated Teller Machine");
[Link]("Choose 1 for Withdraw");
[Link]("Choose 2 for Deposit");
[Link]("Choose 3 for Check Balance");
[Link]("Choose 4 for EXIT");
[Link]("Choose the operation you want to perform:");

//get choice from user


int choice = [Link]();
switch(choice)
{
case 1:
[Link]("Enter money to be withdrawn:");

//get the withdrawl money from user


withdraw = [Link]();

//check whether the balance is greater than or equal to the withdrawal amount
if(balance >= withdraw)
{
//remove the withdrawl amount from the total balance
balance = balance - withdraw;
[Link]("Please collect your money");
}
else
{
//show custom error message
[Link]("Insufficient Balance");
}
[Link]("");
break;

case 2:

[Link]("Enter money to be deposited:");

//get deposite amount from te user


deposit = [Link]();

//add the deposit amount to the total balanace


balance = balance + deposit;
[Link]("Your Money has been successfully depsited");
[Link]("");
break;

case 3:
//displaying the total balance of the user
[Link]("Balance : "+balance);
[Link]("");
break;

case 4:
//exit from the menu
[Link](0);
}
}
}
}

Output:
10. Sort Array in Ascending Order
import [Link];
public class SortArrayExample1
{
public static void main(String[] args)
{
//defining an array of integer type
int [] array = new int [] {90, 23, 5, 109, 12, 22, 67, 34};
//invoking sort() method of the Arrays class
[Link](array);
[Link]("Elements of array sorted in ascending order: ");
//prints array using the for loop
for (int i = 0; i < [Link]; i++)
{
[Link](array[i]);
}
}
}

Output:

Array elements in ascending order:


5
12
22
23
34
67
90
109

You might also like