0% found this document useful (0 votes)
14 views15 pages

Top Java Programs for Beginners

Java

Uploaded by

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

Top Java Programs for Beginners

Java

Uploaded by

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

Program 1:

Even odd number

package CoveredJavaPrograms;

public class EvenOdd1 {

public static void main(String[] args) {

int a=37;
if(a%2==0) {
[Link]("Number is Even");
}
else {
[Link]("Number is odd");
}
}

}
Program 2:
EvenOdd number taking value from user

package CoveredJavaPrograms;

import [Link];

public class EvenOddUser1 {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter the Number::");
int a = [Link]();
//int a=60;
if(a%2==0) {
[Link]("Number is Even");
}
else {
[Link]("Number is odd");
}
}

}
Program 3:
PrimeNumber
package CoveredJavaPrograms;

import [Link];

public class PrimeNumber1 {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter the number::");
int n= [Link]();

int temp=0;
for(int i=2;i<=n-1;i++) { //2-3
if(n%i==0) {
temp= temp+1;
}
}
if(temp==0) {
[Link]("Number is Prime");
}
else {
[Link]("Number is not Prime");
}
}

Program 4:
FibonnaciSeries
package CoveredJavaPrograms;

import [Link];

public class FibonnaciSeries2 {

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
[Link]("Enter the number::");
int no = [Link]();

int a=0,b=1,c;

for(int i=0;i<=no-1;i++) { //i=0,1,2,3,4,5


c=a+b;//8,
[Link](c);//1,2,3,5,8

a=b;//5
b=c;//8
}
}

Program 5:
FibonnaciSeries taking input from user
package CoveredJavaPrograms;

import [Link];

public class FibonnaciSeriesUser2 {

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
[Link]("Enter the number::");
int no = [Link]();
int a=0,b=1,c;

for(int i=1;i<=no;i++) {
c=a+b;
[Link](c);
a=b;
b=c;
}
}

}
Program 6:
Factorial Number
package CoveredJavaPrograms;

import [Link];

public class FactorialNumber2 {

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
[Link]("Enter the number::");
int no = [Link]();//4
int fact=1;
for(int i=1;i<=no;i++) {//i=1,2,3,4,5
fact = fact*i;//24
}
[Link](fact);
}

}
Program 7:
ReverseNo
package CoveredJavaPrograms;

import [Link];

public class ReverseNo {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter the number::");
int no=[Link]();//1221

int temp = no;//1221


int rev=0,rem;
while(temp!=0) {
rem=temp%10; //1
rev = rev*10+rem;//1221
temp=temp/10;//.0
}
[Link]("Reverse is: " +rev);
}

}
Program 8:
PalindromeNumber2
package CoveredJavaPrograms;

import [Link];

public class PalindromeNumber2 {

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
[Link]("Enter the number::");
int no=[Link]();//1221

int temp = no;//1221


int rev=0,rem;
while(temp!=0) {
rem=temp%10; //1
rev = rev*10+rem;//1221
temp=temp/10;//.0
}
if(no==rev) {
[Link](no+" is Palindrome number");
}
else {
[Link](no+" is not palindrome number");
}
}

}
Program 9:
Reverse String
package CoveredJavaPrograms;

import [Link];

public class ReverseString {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter the String::");
String name=[Link]();//1221

//String name = "ABCDE"; //rev = EDCBA


int leng = [Link]();//5
[Link](leng);
String rev="";//ED
for(int i=leng-1;i>=0;i--) {//i=4,3,2,1,0
rev=rev+[Link](i);//EDCBA
}
[Link]("Reverse of string is::"+rev);
}

}
Program 10:
PalindromeString

package CoveredJavaPrograms;

import [Link];

public class PalindromeString {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter the string you want to check:");
String str = [Link]();
String org_str=str; //maam
String rev="";
int len=[Link]();
for(int i = len - 1; i >= 0; i--)
{
rev = rev + [Link](i);//maam
}
if(org_str.equals(rev))
{
[Link]("The string is palindrome.");
}
else
{
[Link]("The string is not palindrome.");
}
}

Program 11:
ArmstrongNumber

package CoveredJavaPrograms;

import [Link];

public class ArmstrongNumber {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter the number");
int number = [Link]();//153
int t1=number;//t1=153
int length = 0;
while(t1!=0) {//1!=0
t1 = t1/10;//.
length = length+1;//3 // for find out number of digits

}
int t2 = number;//153
int arm = 0;
int rem ;
while(t2!=0) {//15!=0
int mul =1;
rem = t2%10;//1
for(int i=1;i<=length;i++) {
mul = mul*rem;//1
}
arm = arm+mul;//153
t2 = t2/10;//.
}
if(arm==number) {
[Link](number +" is Armstrong number");
}
else {
[Link](number +" is not Armstrong number");
}
}

}
Program 12:
ArrayAscending
package CoveredJavaPrograms;

import [Link];

public class ArrayAscending {

public static void main(String[] args) {


//Create object of scanner class

Scanner sc = new Scanner([Link]);


[Link]("Enter the size of Array:");
int n= [Link]();

//Declare and initialize Array and other Required variables..

int a[] = new int [n];//n=3


int i,j,temp=0;

//Now get all array elements from user

[Link]("Enter " + n +" number of array elements: ");


for(i=0;i<n;i++) {
a[i]=[Link]();
}

//Print all array elements

[Link]("All array elements are: ");


for(i=0;i<n;i++) {
[Link](" "+a[i]);
}

//Now arrange array elements in ascending order


for(i=0;i<n;i++) {//i=2
for(j=i+1;j<n;j++) {//j=2
if(a[i]>a[j]) {//6>1
temp=a[i];//6
a[i]=a[j];//1
a[j]=temp;//6
}
}
}

//Print array in ascending order...


[Link]("\nArray Elements in Ascending Order: ");
for(i=0;i<n;i++) {
[Link](" "+a[i]);
}
}

Program 13:
ArrayDesending
package CoveredJavaPrograms;

import [Link];

public class ArrayDesending {

public static void main(String[] args) {


//Create object of scanner class

Scanner sc = new Scanner([Link]);


[Link]("Enter the size of Array:");
int n= [Link]();

//Declare and initialize Array and other Required variables..

int a[] = new int [n];//n=3


int i,j,temp=0;

//Now get all array elements from user

[Link]("Enter " + n +" number of array elements: ");


for(i=0;i<n;i++) {
a[i]=[Link]();
}

//Print all array elements

[Link]("All array elements are: ");


for(i=0;i<n;i++) {
[Link](" "+a[i]);
}

//Now arrange array elements in ascending order

for(i=0;i<n;i++) {//i=2
for(j=i+1;j<n;j++) {//j=2
if(a[i]<a[j]) {//6>1
temp=a[i];//6
a[i]=a[j];//1
a[j]=temp;//6
}
}
}

//Print array in ascending order...


[Link]("\nArray Elements in Desending Order: ");
for(i=0;i<n;i++) {
[Link](" "+a[i]);
}
}

}
Program 14:
ArrayMinElement
package CoveredJavaPrograms;

import [Link];

public class ArrayMinElement {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter the Length of Array::");
int n = [Link]();

[Link]("Enter the "+n+" number of Array Element::");


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

}
int min=a[0];//5
for(int i=1;i<[Link];i++) {
if(a[i]<min) {//7>5
min=a[i];//7
}
}
[Link]("Minimum Element is::"+min);
}
}

Program 15:
ArrayMaxElement
package CoveredJavaPrograms;

import [Link];

public class ArrayMaxElement {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter the Length of Array::");
int n = [Link]();

[Link]("Enter the "+n+" number of Array Element::");

int a[] = new int[n];


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

}
int max=a[0];//5
for(int i=1;i<[Link];i++) {
if(a[i]>max) {//7>5
max=a[i];//7
}
}
[Link]("Maximum Element is::"+max);
}
}

Program 16:
Star1

package CoveredJavaPrograms;

public class Star1 {

public static void main(String[] args) {

//i for rows and j for columns


//row denotes the number of rows you want to print
int i, j, row=6;
//outer loop for rows
for(i=0; i<row; i++) //i=1
{
//inner loop for columns
for(j=0; j<=i; j++)
{
//prints stars
[Link]("* ");
}
//throws the cursor in a new line after printing each line
[Link]();
}

}
}
Program 17:
Star2

package CoveredJavaPrograms;

public class Star2 {

public static void main(String[] args) {

for(int i=1;i<=4;i++) //i=3


{
for(int j=3;j>=i;j--)//i=2
{
[Link](" ");
}
for(int k=1;k<=i;k++) {//i=2
[Link]("*");
}
[Link](" ");
}
}

}
Program 18:
Star3

package CoveredJavaPrograms;

public class Star3 {

public static void main(String[] args) {

for(int i=1;i<4;i++) //i=2


{
for(int j=3;j>=i;j--)//i=2
{
[Link]("* ");
}

[Link]("");
}
}

Common questions

Powered by AI

The Scanner class facilitates user interaction by reading various input types like integers and strings directly from the console. Its use in Source 1 programs enables dynamic execution whereby users can determine specific values on the fly, improving program flexibility and testing ease. However, its reliance on standard input (System.in) might pose challenges for non-console implementations or require careful resource management, including closure to avoid memory leaks .

Both Fibonacci Series programs in Source 1, Program 4 and Program 5, generate Fibonacci numbers using iterative methods. Program 4 begins printing from the first two numbers supplied in the code (a=0 and b=1) while running the loop from 0 to "no-1." Program 5, on the other hand, iterates starting from 1 to "no" to include the initial values. While both methods have linear time complexity O(n), their difference lies in their printing format and the range of the loop. Program 5's loop range from 1 ensures that the defined input number leads to the generation of that exact number of Fibonacci numbers .

Pattern programs like Star1, Star2, and Star3 in Source 3 demonstrate the use of nested loops to produce systematic visual patterns, aiding in understanding loop constructs and control flow mechanisms. Star1 gradually increases stars per row (a left-aligned triangle), while Star2 includes spaces leading to a right-aligned triangular pattern, and Star3 creates an inverted triangle by reducing stars in each subsequent row. These serve educational purposes, offering practice in control structures, logic operations, and iterative sequences .

The string reversal program (Program 9) involves input collection via the Scanner class, followed by calculation of the string's length used to iterate from end to start. It concatenates characters from the original string in reverse order into a new "rev" string, effectively using a decrement loop. This straightforward character reassembly captures the reversal efficiently by leveraging simple loop control and character indexing .

The maximum and minimum element identification programs (Programs 14 and 15) use arrays to efficiently traverse and compare values by storing integers in sequential order. By iterating with simple logic, they engage fully with array indexing capabilities to process all elements and track boundary values. This effective utilization minimizes computational complexity in simple O(n) operations and illustrates arrays' utility in organized data handling within linear time frames .

The palindrome number check (Program 8) involves numeric manipulation where the original number is reversed by extracting each digit and rebuilding the number in reversed order. This approach considers integers and arithmetic operations. The string palindrome check (Program 10) performs similarly but within the string data type, reversing the string by concatenating each character in reverse order. Although both employ similar logic to solve the reversal and comparison problem, the integer-based approach is numerical, using modulo and division, while the string approach uses concatenation and character iteration .

Both the ascending (Program 12) and descending (Program 13) array sorting programs use a basic bubble sort algorithm with nested loops. The outer loop iterates over the array, while the inner loop compares adjacent elements, swapping them as needed. The time complexity of both algorithms is O(n²) due to the double iteration over the array elements, making them inefficient for large data sets by contrast with more efficient algorithms like quicksort or mergesort, which have O(n log n) time complexity .

The prime number program (Program 3) checks divisibility of the given number by all integers starting from 2 up to the number minus one to ascertain its "primeness". It uses a loop to check if any division results in zero, setting a flag if it does. The even or odd checking programs (Program 1 and 2) implement a straightforward if-else condition based on the modulus of 2. While the prime check is computationally more complex due to the looping construct, the parity check relies on a simple arithmetic operation and branching .

The Armstrong number program sets the multiplication value to 1 before calculating each digit's power, instead of initializing it to zero. This can lead to incorrect calculations for Armstrong numbers that include zero within them, as leading zeros should not impact the exponentiation but in the enabled logic they will, since any number raised to zero is one affecting sums unexpectedly .

The factorial calculation program (Program 6) exemplifies iterative programming by using a for loop to compute the product of a sequence of integers from 1 to "no." This signifies a direct application where iteration sequentially accumulates results through multiplication. Such iterative designs are straightforward for tasks requiring accumulation of results over defined ranges, benefiting from predictability and simplicity .

You might also like