[Go to site: main page, start]

0% found this document useful (0 votes)
34 views82 pages

Java Programming Basics and Examples

The document contains several Java programs that take user input and perform basic conditional checks and operations. The programs include checking if a number is even or odd, validating a month number, identifying if a number is three digits, finding the smallest of three numbers, finding the middle of three numbers, printing the name of a month from its number, identifying the sport played on a given day of the week, checking if a year is a leap year, and determining grade based on percentage or road tax based on bike price.

Uploaded by

Wrushabh Shirsat
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)
34 views82 pages

Java Programming Basics and Examples

The document contains several Java programs that take user input and perform basic conditional checks and operations. The programs include checking if a number is even or odd, validating a month number, identifying if a number is three digits, finding the smallest of three numbers, finding the middle of three numbers, printing the name of a month from its number, identifying the sport played on a given day of the week, checking if a year is a leap year, and determining grade based on percentage or road tax based on bike price.

Uploaded by

Wrushabh Shirsat
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

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@@@@
***********************PROGRAMMING JAVA ONLINE
BATCH*************************
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@

//WAJP to check given number is even or odd without control use of


statement
import [Link];
class M2
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);

[Link]("Enter the number);


int n=[Link]();
switch(n%2)
{
case 0: [Link]("even");
break;
case 1: [Link]("odd");
break;
}
}
}

.......................................................

// WAJP to check user enter month number is valid or not


import [Link];
class Validmonth
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int a=[Link]();
if(a>0&&a<=12)
{
[Link](a+" it is valid month number");
}
else
{
[Link](a+" it is invalid month number");
}
}
}
............................................................

// WAJP to read integer value from user and print it is 3 digit or not
import [Link];
class Digitvalue
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int n=[Link]();

if(n>99&&n<1000)
{
[Link](n+" it is three digit number");
}

else

{
[Link](n+" It is invalid number ");
}
}
}

....................................................................

// WAJP to read the three integer value from user and print smallest one
import [Link];
class SmallestOne
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int a=[Link]();
int b=[Link]();
int c=[Link]();
if (a<b &&a<c)
{
[Link](a+" is smallest");
}
else if(b<c)
{
[Link](b+" is smallest");
}
else
{
[Link](c+" is smallest");
}
}
}

....................................................................
// WAJP to read three distinct integer from the user and print the
middle value.
import [Link];
class MiddleOne
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int a=[Link]();
int b=[Link]();
int c=[Link]();
if(a>b &&b>c)
{
[Link](b+" is middle number");
}
else if(b>a && a>c)
{
[Link](a+" is middle number");
}
else
{
[Link](c+" is middle number");
}
}
}
....................................................................

//WAJP to read month number from the user and print corresponding month
name
import [Link];
class MonthName
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number: ");
int n=[Link]();

switch(n)
{
case 1 : [Link]("January");
break;
case 2 : [Link]("February");
break;
case 3 : [Link]("March");
break;
case 4 : [Link]("April");
break;
case 5 : [Link]("May");
break;
case 6 : [Link]("June");
break;
case 7 : [Link]("July");
break;
case 8 : [Link]("August");
break;
case 9 : [Link]("September");
break;
case 10 : [Link]("October");
break;
case 11 : [Link]("November");
break;
case 12 : [Link]("December");
default : [Link]("Invalid Month number");
}
}
}
.........................................................................
........
/* In a school,they have 3 sports, they are cricket,Basket ball and
Volley ball.
They arranged a time table as follows.
Day Sport
Monday Cricket
Tuesday Basketball
Wednesday Cricket
Trusday Volley Ball
Friday Basket Ball
Saturday Volley Ball
Design a program such that it asks the user to enter the name of
the week(in lower case) and
program should say which sport the students should play.*/

import [Link];
class WeeklySports
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the day : ");
String a=[Link]();
switch (a)
{
case "monday" : [Link]("Cricket ");
break;
case "tuesday" : [Link]("Basket ball ");
break;
case "wednesday" : [Link]("Cricket ");
break;
case "thrusday" : [Link]("Volley ball ");
break;
case "friday" : [Link]("Basket ball ");
break;
case "saturday" : [Link]("Volley ball ");
break;
}
}
}
.........................................................................
.....

//WAJP to print coooesponding month days in month

import [Link];
class MonthName
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number: ");
int n=[Link]();

switch(n){
case 1 :

case 3 :

case 5 :

case 7 :

case 8 :

case 10 :
case 12 : [Link]("31 days");
break;
case 4 :
case 6 :
case 9 :
case 11 : [Link]("30 days");
break;
case 2 : [Link]("28 or 29 days");
default : [Link]("Invalid Month number");
}
}
}
.....................................................................
//WAP to check year is leap year or not
mport [Link];
class Leapyear
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number: ");
int x=[Link]();

if(x%4==0 && x%100!==0 ||x%400==0)


{
[Link]("leap year");
}
else
{
[Link](" not leap year");
}
}
}

.........................................................................
...

//WAJP to check whether given number is even or odd

class M2
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number");
int n=[Link]()

String sc[]={"even","odd"};

[Link](n+"is a"+st[n%2]);

...................................11/01/2022............................
................
///write a program to accept percentage from the user and display the
grade
according to the following criteria:
Marks Grade
>90 A
>80 and <=90 B
>=60 and <=80 C
below 60 D
code:
import [Link];
class PercentageGrade
{
public static void main(String[] args)
{
Scanner sc= new Scanner([Link]);
int p = [Link]();
if(p>90)
{
[Link]("Percentage : "+p+ "\n"+"Grade
: A");
}
else if(p>80 && p<=90)
{
[Link]("Percentage : "+p+ "\n"+"Grade
: B");
}
else if(p>=60 && p<=80)
{
[Link]("Percentage : "+p+ "\n"+"Grade
: C");
}
else if(p<60)
{
[Link]("Percentage : "+p+ "\n"+"Grade
: D");
}
}
}

.........................................................................
...........
///Write a program to accept the cost price of a bike and display the
road tax to be paid according to the following
Cost price (in Rs.) Tax
>100000 15%
>50000 and <=100000 10%
<=50000 5% ////////

import [Link];
class RoadTaxPaid
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the Bike price : ");
int n=[Link]();
String b=(n>100000)?"Tax :
15%":(n>50000&&n<=100000)?"Tax : 10%":(n<=50000)?"Tax : 5%":"Invalid
Amount";
[Link]("Your Road "+b);
}
}
.........................................................................
............
///Write a program to accept a number from 1 to 12 display the name of
the month and days in that month
like 1 for January and number of days 31 and so on/////

import [Link];
class MonthsWithDays
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Select Your No.");
int a = [Link]();
String b = (a==1)?"Jan 31 days":(a==2)?"Feb 28
Days":(a==3)?"March 31 Days":(a==4)?"April 30 Days"
:(a==5)?"May 31 Days":(a==6)?"June 30
Days":(a==7)?"March 31 Days":(a==7)?"July 31 Days"
:(a==8)?"August 31 Days":(a==9)?"September 30
Days":(a==10)?"October 31 Days":(a==11)?"November 30 Days"
:(a==12)?"December 31 Days":"Invalid Month Selected";
[Link]("Your Output is :"+b);
}
}
.........................................................................
........
/// accept number from user
if it is divisible by 3 print fizz
if it is divisible by 5 print buzz
if it is divisible by 3 and 5 print fizz and buzz /////

import [Link];
class FizzBuzz
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int n=[Link]();

if(n%3==0 && n%5==0)


{
[Link]("Fizz and Buzz");
}
else if(n%5==0)
{
[Link]("Buzz");
}
else if(n%3==0)
{
[Link]("Fizz");
}
else
{
[Link]("Not divisible by 3 or 5");
}

}
}

......................................................................
///// WAJP to print the city and famous place from the given cities
import [Link];
class City
{
public static void main(String [] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter your City Name :");
String u1 = [Link]();
String c1 = "Delhi";
String c2 = "Agra";
String c3 = "Jaypur";
if ([Link] (c1))
{
[Link]("Red Foart");
}
else if ([Link] (c2))
{
[Link]("Taj Mahal");
}
else if ([Link] (c3))
{
[Link]("Jal Mahal");
}
else
{
[Link]("Invalid Input.");
}
}
}
..........................................12/01/2022.....................
..............
////WAP to print number from n to 1/////
import [Link];
class MainRunner{
public static void main(String[] args)
{
Scanner sc= new Scanner([Link]);
[Link]("Enter the number");
int n=[Link]();
while(n>0)
{
[Link](n);
n--;
}
[Link]("thank you");
}
}
...................................................................

////WAP to print number from 1 to n/////

import [Link];
class MainRunner1
{
public static void main(String[] args)
{
Scanner sc= new Scanner([Link]);
[Link]("Enter the number");
int n=[Link]();
int i=1;
while(i<=n)
{
[Link](n);
i++;
}
[Link]("thank you");
}
}
...................................................

////WAP to calculate sum of first natuaral numbers/////


import [Link];
class MainRunner2
{
public static void main(String[] args)
{
Scanner sc= new Scanner([Link]);
[Link]("Enter the number");
int n=[Link](); //5
int sum=0;

while(n>0)
{
sum+=n;
n--;
}
[Link](sum); //15 answer
[Link]("thank you");
}
}
.......................................................................

////WAP to calculate factorial of first n natuaral numbers/////

import [Link];
class MainRunner3
{
public static void main(String[] args)
{
Scanner sc= new Scanner([Link]);
[Link]("Enter the n number");
int n=[Link](); //5
int fact=1;

while(n>0)
{
fact*=n;
n--;
}
[Link](fact); //5*4*3*2*1=120 answer
[Link]("thank you");
}
}
.......................................................................

////WAP to print all the even numbers from 1 to n/////

import [Link];
class MainRunner4
{
public static void main(String[] args)
{
Scanner sc= new Scanner([Link]);
[Link]("Enter the n number");
int n=[Link](); //10
int i=1;

while(i<=n)
{
if(i%2==0)
[Link](i); //2 4 6 8 10
i++;
}
[Link]("thank you");
}
}
.................................................................
////WAP to print all the even numbers from 1 to n/////

import [Link];
class MainRunner
{
public static void main(String[] args)
{
Scanner sc= new Scanner([Link]);
[Link]("Enter the n number");
int n=[Link](); //10
int i=2;

while(i<=n)
{
if(i%2==0)
[Link](i); //2 4 6 8 10
i+=2;
}
[Link]("thank you");
}
}
.................................assignment..............................
.....

///WAJP to print all the odd numbers within n///

import [Link];
class OddNumbers
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int n=[Link]();
int i=1;

while(i<=n)
{
if(i%2!=0)
[Link](i+" ");
i++;
}
}
}
........................................................................

///WAJP to calculate the average of first N natural number///

import [Link];
class Average
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number: ");
int n=[Link](); //10
int i=0;
float sum=0;
while(i<=n)
{
sum+=i;
i++;
}

[Link]("the average of first N natural number :


"+sum/n);
}
}
.........................................................................
.........

///WAJP to calculate the sum of even numbers with in N///

import [Link];
class SumEven
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number: ");
int n=[Link](); //10
int i=0;
int sum=0;
while(i<=n)
{
if (i%2==0) //2+4+6+8+10
sum+=i;
i++;
}
[Link](sum);
}
}
.........................................................................
....

///WAJP to count the how many digits present in the number//

import [Link];
class DigitPresent
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number: ");
int n=[Link]();
String a=(1<=n && n<=9)?"1 digit number":(10<=n && n<=99)?"2
digit number":(100<=n && n<=999)?"3 digit number"
:(1000<=n && n<=9999)?"4 digit number":(10000<=n &&
n<=99999)?"5 digit number"
:(100000<=n && n<=999999)?"6 digit number":"more than 3 digit
number";

[Link]("digits present in the number are: "+a);


}
}
......OR......

// WAJP to c0unt how many digit present in the the number///

import [Link];
class DigitPresent
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number: ");
int n=[Link](); //10
int count=0;
do
{
count++;
n=n/10;
}while(n!=0);

[Link]("the no. of digit present in the number :


"+count);
}
}
.........................................................................
.
///WAJP to calculate N to the power of p/////

import [Link];
class CalcPawer
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Select Your No.");

int n = [Link]();
int p = [Link]();
int i=1;
int power=1; //(or p>0 and p--; also use)

while(i<=p)
{
power*=n;
i++;
}
[Link](n+ " to the power of "+p+"is" +power);
}
}
....................................13/01/2022...........................
..........

///WAJP to calculate the average of first N natural number///

import [Link];
class Average1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number: ");
int n=[Link](); //10
double sum=0;
for(int i=0;i<=n;i++){
sum+=i;
}
[Link]("the average of first N natural number :
"+sum/n);
}
}
.........................................................................
..........

///WAJP to calculate the sum digit present in the number?///

import [Link];
class SumDigit
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number: ");
int n=[Link]();
int sum=0;
do{
int d=n%10;
sum+=d;
n=n/10;
}while(n!=0);
[Link]("the sum of digit present in the number :
"+sum);
}
}
.......................................................

///WAJP to check the number is Armstrong number///


import [Link];
class ArmstrongNo
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number: ");
int n=[Link]();
int sum=0;
int temp=n;
do{
int d=n%10;
sum+=d*d*d;
n=n/10;
}while(n!=0);

{
if(sum==temp)
[Link]("the number is Armstrong");
else
[Link]("not a Armstrong number");
}
}
}
............................Assignment...................................
//WAJP to reverse the number

import [Link];
class ReverseNum
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int rev=0;
do{
int d=n%10;
rev=rev*10+d;
n=n/10;
}while(n!=0);

[Link](rev);
}
}
.........................................................................
..

//WAJP to check number is palindrome or not//

import [Link];
class PalindromeNo
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int rev=0;
int temp=n;
do{
int d=n%10;
rev=rev*10+d;
n=n/10;
}while(n!=0);

{
if(rev==temp)
[Link]("Number is Palindrome number");
else
[Link]("Number is not a Palindrome number");
}
}
}
.........................................................................
.......

//WAJP to calculate the product of digits

import [Link];
class ProductOfDigit
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int res=1;

do{
int d=n%10;
res*=d;
n=n/10;
}while(n!=0);

[Link](res);
}
}
.........................................................................
......
//WAJP to count how many prime digits present in the number
import [Link];
class PrimeDigits
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int count=0;
do{
int d=n%10;
n/=10;
if(d==2 || d==3 || d==5 || d==7)
count++;

}while(n!=0);

[Link](count);
}
}

.........................................................................
.......

//WAJP to calculate the sum of even digits and odd digits in the number?
import [Link];
class SumEvenOdd
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int n=[Link]();
int sum1=0;
int sum2=0;
do{
int d=n%10;
n=n/10;
if(n%2==0)
sum1+=d;
else
sum2+=d;
}while(n!=0);
[Link](sum1);
[Link](sum2);
}
}
.....................................14/01/2022..........................
......

//// WAJP to check number is strong number or not........(145=1!+4!+5!)


import [Link];
class StrongNum
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int n=[Link]();
int sum=0;temp=n;
do{
int d=n%10;
int fact=1;
while(d>1)
{
fact*=d;
d--;
}
sum=sum+fact;
n=n/10;
}while(n!=0);

if(sum==temp)

[Link]("strong number");
else
[Link]("not strong number");
}
}
.........................................................................
....................
////WAJP to check number is perfect number or not...(18=1,2,4,7,14(add
all)sum of its divisers=18)
import [Link];
class PerfectNum
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int n=[Link]();
int sum=0;
for(int i=0;i<=n/2;i++){
if(n%i==0)
sum=sum+i;
}

if(sum==temp)
[Link](n+"is a Perfect number");
else
[Link]("not a Perfect number ");
}
}

.........................................................................
//WAJP to check the number is prime or not

import [Link];
class PrimeNum
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
boolean flag=false;
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=true;
break;
}
}
if(flag==true)
[Link]("it is not a prime number");
else
[Link]("it is a prime number");

}
}
......................................assignment.........................
............

//W///WAJP to print how many divisors of the number n

import [Link];
class Divisors
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int n=[Link]();
int i=0;
int count=0;
for (i=1;i<=n;i++ )
{
if (n%i==0)
{
count++;
}
}
[Link](count);

}
}
.........................................................................
..

//WAJP to print Multiplication Table for given no.

import [Link];
class MultiplicationTable
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter your no :");
int no = [Link]();

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


{
[Link](no*i);
}
}
}
.........................................................................
..

// WAJP to print the average of digits

import [Link];
class AverageDigits
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
int sum=0;
int count=0;
do{
int d=n%10;
sum+=d;
n=n/10;
count++;
}while(n!=0);

[Link]("The average of digits is : "+sum/count);


}
}
.........................................................................
........
WAJP tp check wether the no is HAPPY NO or NOT?
import [Link];
public class HappyNo
{
public static int isHappy(int number)
{
int sum = 0;
int rem = 0;

while (number > 0);


{
rem = number%10;
sum=sum+(rem*rem);
number = number/10;
}
return sum;
}

public static void main(String [] args)


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

while(res != 1 && res != 4){


res=isHappy(res);
}
if (res==1)
{
[Link]("Happy number : ");
}
else
{
[Link]("not happy number : ");
}
}
}

.................OR..................

import [Link];
class HappyNo
{
public static void main(String [] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter your no :");
int x = [Link]();
boolean rs=isHappy(x);
if (rs==true)
[Link]("Happy number : ");
else
[Link]("not happy number : ");
}
static boolean isHappy(int n)
{
while(n>9){

int sum=0;
do{
int d=n%10;
sum=sum+d*d;
n=n/10;
}while(n!=0);
n=sum;
}
if(n==1 || n==7)
return true;
else
return false;
}
}
}

.........................................................................
........
//WAJP to print the number is Xylem or pholem

import [Link];
class XylemPlolemNum
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number");
int n=[Link]();
int sum1=0;
int sum2=0;
int temp=n;
while(temp!=0){
if(temp==n || temp<10)
sum1=sum1+temp%10;
else
sum2=sum2+temp%10;
temp=temp/10;
}
if (sum1==sum2)
{
[Link]("it is Xylem number");
}
else
{
[Link]("It is pholem number!");
}
}
}

...................................15/01/2021............................
.............
***********methodssss******
.............
methos is group of statements write it ones and execute many times
[<modifier>] return type= methodName(parameter)

class Example
{
static void m1()......//calling method
{
.................
................
}
static void m2()......//calling method
{
,,,,,,,,,,,,,
,,,,,,,,,,,,
}
public static void main(String[] args)
{

m1()
m2()
m1()
}
}
..............................................................
// WAJP to calculate area and circumference of circle

import [Link];
class Circle
{
static void circleInfo(double r)......//calling method
{
double ar=3.143*r*r;
double cir=2*3.143*r;

[Link]("area is: "+ar);

[Link]("radius is : " +r);

[Link]("circumference is : "+cir);

public static void main(String[] args)


{

[Link]("main method started");


circleInfo(5.7);
[Link]("---------------------------");

circleInfo(7.8);

[Link]("---------------------------");
Scanner sc=new Scanner([Link]);
[Link]("enter the radius");
double rd=[Link](); //8.5
circleInfo(rd);
}
}
...................................................................
//WAJP to define a method to return biggest among three integer number?

class Biggest
{
static int getBiggest(intx,int y,int z) ////write int seperately
{
// write a code here
if(x>y && x>z)
return x;
else if(y>z)
return y;
else
return z;
}

public static void main(String[] args)


{
int big=getBiggest(34,56,18);
[Link]("Biggest is :"+big);
}
}
............................................................
//using Scanner class...............

import [Link];
class Biggest
{
static int getBiggest(int x,int y,int z) ////write int seperately
{
// write a code here
if(x>y && x>z)
return x;
else if(y>z)
return y;
else
return z;
}

public static void main(String[] args)


{
Scanner sc=new Scanner([Link]);
[Link]("enter the 3 integer value");
int a=[Link]();
int b=[Link]();
int c=[Link]();

int big=getBiggest(a,b,c);
[Link]("Biggest is :"+big);
}
}
............................Assignment...................................
//define a method return smallest among three number

import [Link];
class ReturnSmallest
{
static int smallest(int x, int y, int z)
{
int res=(x<y && x<z)?x:(y<z)?y:z;
return res;
}

public static void main(String[] args)


{
Scanner sc=new Scanner([Link]);
[Link]("Enter three numbers : ");
int a=[Link]();
int b=[Link]();
int c=[Link]();
int num=smallest(a,b,c);
[Link](num);
}
}
........................................................................
//define a method to print area and parameter of a rectangle
import [Link];
class RectangleArea
{
static void rectangle(double a, double b) //calling
method
{
double ar=4*a*a*b*b;
double rect=2*a*b;
[Link]("area is: "+ar);
[Link](" small size is : " +a);
[Link](" small size is : " +b);
[Link]("parameter is : "+rect);
}
public static void main(String[] args)
{

[Link]("main method started");


rectangle(5.7,4.2);
[Link]("---------------------------");
rectangle(7.8,5.7);
[Link]("---------------------------");
Scanner sc=new Scanner([Link]);
[Link]("enter the sides : ");
double s1=[Link]();
double s2=[Link]();
rectangle(s1,s2);
}
}
........................................................................

//define a method to print area and parameter of a sqare

import [Link];
class SqareArea
{
static void sqare(double a) //calling method
{
double ar=4*a*a;
double parimeter=4*a;
[Link](" side is : " +a);
[Link]("area is: "+ar);
[Link]("parameter is : "+parimeter);
}
public static void main(String[] args)
{

[Link]("main method started");


sqare(4.2);
[Link]("---------------------------");
sqare(5.7);
[Link]("---------------------------");
Scanner sc=new Scanner([Link]);
[Link]("enter the side : ");
double s=[Link]();
sqare(s);
}
}
........................................................................
//define aa method to return true if the number is special two digits
number otherwise return false
import [Link];
class SpeTwoDigitNum
{
static int specialNumber(int x)
{
int sum=0;
int mul=1;
int temp=x;
do{
int a=x%10;
sum+=a;
mul*=a;
x=x/10;
}while(x!=0);
if (temp==(sum+mul))
[Link]("Special two digit number ");
else
[Link](" Not a Special two digit
number ");
return temp;
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter three number : ");
int n=[Link]();
int num=specialNumber(n);
[Link](num);
}
}
.........................................................................
......
//define a method to return a simple interest

import [Link];
class SimpleInterest
{
static void simInterest(double p, double r ,double t)
//calling method
{
double interest=(p*r*t)/100;
[Link]("simple interest is: "+interest);
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the principle amount : ");
double a=[Link]();
[Link]("Enter the rate of interest : ");
double b=[Link]();
[Link]("Enter the time : ");
double c=[Link]();

simInterest(a,b,c);
}
}
*************************************************************************
***************
........................................17/01/2022.......................
..........
//WAJP to check number is happy or not
class HappyNo
{
public static void main(String [] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter your no :");
int x = [Link]();
boolean rs=isHappy(x);
if (rs==true)
[Link]("Happy number : ");
else
[Link]("not happy number : ");
}
static boolean isHappy(int n){
while(n>9){
int sum=0;
do{
int d=n%10;
sum=sum+d*d;
n=n/10;
}while(n!=0);
n=sum;
}
if(n==1 || n==7)
return true;
else
return false;
}
}

......................................................................
//WAJP to check number is strong or not
class StrongNum
{
public static void main(String [] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter your no :");
int x = [Link]();
boolean rs=isStrong(x);
if (rs==true)
[Link]("Strong number : ");
else
[Link]("not Strong number : ");
}
static boolean isStrong(int n){
int sum=0;temp=n;
do{
int d=n%10;
int fact=1;
while(d>1)
{
fact*=d;
d--;
}
sum=sum+fact;
n=n/10;
}while(n!=0);
if(sum==temp)
return true;
else
return false;
}
}
...........................................................
//WAJP to check number is Armstrong or not
class ArmStrongNum
{
public static void main(String [] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter your no :");
int x = [Link]();
boolean rs=isArmStrong(x);
if (rs==true)
[Link](x+"ArmStrong number ");
else
[Link](x+"not ArmStrong
number");
}
static boolean isArmStrong(int n)
{
int sum=0;temp=n;
do{
int d=n%10;
int sum+=d*d*d;
n=n/10;
}while(n!=0);

if(sum==temp)
return true;
else
return false;
}
}
.................................Assignment..............................
...........
//WajP to define a method to return true if Palindrome oth erwise false

import [Link];
class Palindrome
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int x=[Link]();
boolean re=isPalindrome(x);
if (re==true)
[Link](x+" : is Palindrome number ");
else
[Link](x+" : is not Palindrome number ");

}
static boolean isPalindrome(int n)
{
int rev=0;
int temp=n;
do{
int d=n%10;
rev=rev*10+d;
n=n/10;
}while(n!=0);

if(rev==temp)
return true;
else
return false;
}
}
.........................................................................

// WAJP to return reverse of a number


import [Link];
class ReverseNumber
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int x=[Link]();
boolean re=isReverse(x);
if (re==true)
[Link](x+" : is Reverse number ");
else
[Link](x+" : is not Reverse number ");

}
static boolean isReverse(int n)
{
int rev=0;
int temp=n;
do{
int d=n%10;
rev=rev*10+d;
n=n/10;
}while(n!=0);

if(rev==temp)
return true;
else
return false;
}
}
.........................................................................
...........

// Wajp to define a method to return n to the power p

import [Link];
class PowerOfNum
{
static int isPower(int n, int p)
{
int i=1;
int pow=1;
while(i<=p){
pow*=n;
i++;
}
[Link]("Power is: "+pow);
return pow;
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the 1st number : ");
int x=[Link]();
[Link]("Enter the 1st number : ");
int y=[Link]();
int power=isPower(x,y);

}
}
.........................................................................
.

// wajp to define a method return factorial of n

import [Link];
class FactorialOfNum
{
static int isFact(int n)
{
int fact=1;
while(n>0){
fact=fact*n;
n--;
}
[Link]("Factorial is: "+fact);
return fact;
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the 1st number : ");
int x=[Link]();
int res=isFact(x);
}
}
......................................................................

// wajp to define a method return sum of digits in the number

import [Link];
class SumOfDigitsNum
{
static int sumDigits(int n)
{
int sum=0;
do{
int d=n%10;
sum=sum+d;
n=n/10;
}while(n!=0);
[Link]("Sum of digits in number is
: "+sum);
return sum;
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int x=[Link]();
int re=sumDigits(x);
}
}
.........................................................................
............
********************************18/012022********************************
*****
ABSENT
********************************19/01/2022*******************************
*****
class XYX
{
public static void main(String[] args)
{
String st ="";
for (int i=0;i<=5 ;i++ )
{
st=st+i;
}
[Link](st);
for (int i=0;i<=5 ;i++ )
{
st=i+st;
}
[Link](st);
}
}
...........................................................
class Converation
{
static String decToBinary(int dec)
{
String bin ="";
do{
int b=dec%2;
bin=b+bin;
dec=dec/2;
}while(dec!=0);
return bin;
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the decimal number : ");
int d=[Link]();
String bin=decTo Binary(d);

[Link]("Binary equalent is : "+bin);


}
}
.............................................................
class Converation
{
static String decToOctal(int dec)
{
String oct ="";
do{
int b=dec%8;
oct=b+oct;
dec=dec/8;
}while(dec!=0);
return oct;
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the octal number : ");
int d=[Link]();
String oct=decToOctal(d);

[Link]("Octal equalent is : "+oct);


}
}
...................................................................
import [Link];
class Converation
{
static String decToHexadec(int dec)
{
String hexa ="";
do{
int r=dec%16; //78------
78/16=(rem=14 and quetient=4)=4E
if (r<10)
hexa=r+hexa;
else
hexa=(char)(r+55)+hexa;
dec=dec/16;
}while(dec!=0);
return hexa;
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the decimal number : ");
int d=[Link]();
String hexa=decToHexadec(d);
[Link]("Hexadecimal equalent is : "+hexa);
}
}
.....................................................................
import [Link];
class Convert
{
static int binToDec(int bin)
{
int dec=0,pw=1;
do{
int r=bin%10; //
1011=1*2^3+0*2^2+1*2^1+1*2^0=11
dec=dec+r*pw;
pw=pw*2;
bin=bin/10;
}while(bin!=0);
return dec;
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the binary number : ");
int bin=[Link]();
int dc=binToDec(bin);

[Link]("decimal equalent is : "+dc);


}
}
..................................................................
import [Link];
class Convert
{
static int octToDec(int bin)
{
int dec=0,pw=1;
do{
int r=oct%10;
dec=dec+r*pw;
pw=pw*8;
oct=oct/10;
}while(oct!=0);
return dec;
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the octal number : ");
int oct=[Link]();
int dc=octToDec(oct);

[Link]("decimal equalent is : "+dc);


}
}
...................................................................
*************************20/01/22*********************************
.....................PATTERN PRINTING.............................

class Abcd
{
public static void main(String[] args)
{
for (int i=0; i<5;i++ ){

for (int j=0; j<5;j++ )


{
[Link]("*"+" ");
}
[Link]();
}

}
..............

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
.......................................................................

class Abcd
{
public static void main(String[] args)
{
for (int i=0; i<5;i++ ){

for (int j=0; j<5;j++ )


{
if(i>=j)
[Link]("*"+" ");
else
[Link](" ");
}
[Link]();
}
}
}
....................
*
* *
* * *
* * * *
* * * * *
................
import [Link];
class Abcd
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=1; i<=n;i++ )
{
for (int j=1; j<=i;j++ )
{
[Link]("*"+" ");
}
[Link]();
}
}
}
................................................................
import [Link];
class Abcd
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=1; i<=n;i++ )
{
for (int j=1; j<=n;j++ )
{
if(i<=j)
[Link]("*"+" ");
else
[Link](" ");
}
[Link]();
}
}
}
................
* * * * *
* * * *
* * *
* *
*
........................................................
import [Link];
class Abcd
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=1; i<=n;i++ )
{
for (int j=1; j<=n;j++ )
{
if(i<=j)
[Link]("*"+" ");
else
[Link](" "+" ");
}
[Link]();
}
}
}
.................
* * * * *
* * * *
* * *
* *
*
.....................................................................
import [Link];
class Abcd
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=1; i<=n;i++ )
{
for (int j=1; j<=n;j++ )
{
if(i==j)
[Link]("*"+" ");
else
[Link](" "+" ");
}
[Link]();
}
}
}
......................
*
*
*
*
*
..................................................................
import [Link];
class Abcd
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=1; i<=n;i++ )
{
for (int j=1; j<=n;j++ )
{
if(i+j==n+1)
[Link]("*"+" ");
else
[Link](" "+" ");
}
[Link]();
}
}
}
......................
*
*
*
*
*
...............................................................
import [Link];
class Abcd
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=1; i<=n;i++ )
{
for (int j=1; j<=n;j++ )
{
[Link](j+" ");
}
[Link]();
}
}
}
....................
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
............................................................
import [Link];
class Abcd
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=1; i<=n;i++ )
{
for (int j=1; j<=n;j++ )
{
[Link](i+" ");
}
[Link]();
}
}
}
........................
Enter the number : 5
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
......................................................
import [Link];
class Abcd
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
int k=1;
for (int i=1; i<=n;i++ )
{
for (int j=1; j<=n;j++ )
{
if(i>=j)
[Link](k+" ");
else
[Link](" "+" ");
}
[Link]();
}
}
}
...................

1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
.........................................................................
....
import [Link];
class Abcd
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=1; i<=n;i++ )
{
for (int j=1; j<=n;j++ )
{
[Link](i%2+" ");
}
[Link]();
}
}
}
.........................
Enter the number : 5
1 1 1 1 1
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0
1 1 1 1 1
............................................................
import [Link];
class Abcd
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=1; i<=n;i++ )
{
for (int j=i; j<=n;j++ )
{
[Link](j+" ");
}
[Link]();
}
}
}
..............
Enter the number : 5
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
...........................Assignment............................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=1; i<=n;i++ )
{
for (int j=i; j<=n;j++)
{
[Link](i+" ");
}
[Link]();
}
}
}
...............
1 1 1 1 1
2 2 2 2
3 3 3
4 4
5
...............................................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=1; i<=n;i++ )
{
for (int j=i; j<=n;j++)
{
[Link](j%2+" ");
}
[Link]();
}
}
}
.................
1 0 1 0 1
0 1 0 1
1 0 1
0 1
1
...............................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=1; i<=n;i++ )
{
for (int j=1; j<=i;j++)
{
[Link](i%2+" ");
}
[Link]();
}
}
}
.................
1
0 0
1 1 1
0 0 0 0
1 1 1 1 1
.................................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=1; i<=n;i++ )
{
for (int j=1; j<=i;j++)
{
[Link](j%2+" ");
}
[Link]();
}
}
}
...............
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
.....................................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
int k=65;
for (int i=1; i<=n;i++ )
{
for (int j=1; j<=n;j++)
{
if(i>=j)
{
[Link]((char)k+" ");
k+=1;
}
else
[Link](" "+" ");
}

[Link]();
}
}
}

A
B C
D E F
G H I J
K L M N O
.................................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
int k=1;
for (int i=1; i<=n;i++ )
{
for (int j=1; j<=n;j++)
{
if(i>=j)
{
[Link](k+" ");
k+=1;
}
else
[Link](" "+" ");
}

[Link]();
}
}
}

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
.........or.................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
int k=1;
for (int i=1; i<=n;i++ )
{
for (int j=1; j<i+1;j++)
{
[Link]((k++)+" ");
}
[Link]();
}
}
}
..............................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
int k=1;
for (int i=1; i<=n;i++ )
{
for (int j=1; j<=n;j++)
{
if(i>=j)
{
[Link](k%2+" ");
k+=1;
}
else
[Link](" "+" ");
}

[Link]();
}
}
}
....................................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=1; i<=n;i++ )
{
for (int j=1; j<=n;j++)
{
if(i>=j)
{
[Link](j+" ");
}
else
[Link](" "+" ");
}
[Link]();
}
}
}
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
...........................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=1; i<=n;i++ )
{
for (int j=1; j<=n;j++)
{
if(i+j<=n+1)
{
[Link](j+" ");
}
else
[Link](" "+" ");
}
[Link]();
}
}
}
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
.............................................

1
0 1
0 1 0
1 0 1 0
1 0 1 0 1
.......................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=n; i>=1;i-- )
{
for (int j=n; j>=i ;j--)
{
[Link](j+" ");
}
[Link]();
}
}
}
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
........................................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=n; i>=1;i-- )
{
for (int j=i; j>=1 ;j--)
{
[Link](j+" ");
}
[Link]();
}
}
}
.................
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
.....................................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i=1; i<=n;i++ )
{
for (int j=i; j>=1 ;j--)
{
[Link](j+" ");
}
[Link]();
}
}
}
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
......................................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i = 1; i <= n; i++)
{
int num;
if(i%2 == 0)
{
num = 0;
for (int j = 1; j <= n; j++)
{
[Link](num+" ");
num = (num == 0)? 1 : 0;
}
}
else
{
num = 1;
for (int j = 1; j <= n; j++)
{
[Link](num+" ");
num = (num == 0)? 1 : 0;
}
}
[Link]();
}
}
}
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
...............................................

import [Link];
public class Test
{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
[Link]("Number of number");
int n= [Link]();

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


{
int num = i;
for (int j = 1; j <= i; j++)
{
[Link](num+" ");
num = num+n-j;
}
[Link]();
}

}
}
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
........................................................
*********************************21/01/2022******************************
***
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i = 1; i <= n; i++)
{
for (int j=1;j<=n-i ;j++ )
{
[Link](" ");
}
for (int j=1;j<=2*i-1 ;j++ )
{
[Link]("* ");
}
[Link]();
}
}
}
....................................
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
...................................................................

import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
for (int i = 1; i <= n; i++)
{
for (int j=1;j<=n-i ;j++ )
{
[Link](" ");
}
for (int j=1;j<=2*i-1 ;j++ )
{
[Link](j%2+" " );
}
[Link]();
}
}
}
..................
1
1 0 1
1 0 1 0 1
1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1
................................................................

mport [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);

[Link]("Enter the number : ");


int n=[Link]();
for (int i = 1; i <= n; i++)
{
for (int j=1;j<=n-i ;j++ )
{
[Link](" ");
}
for (int j=1;j<=2*i-1 ;j++ )
{
[Link](i%2+" " );

}
[Link]();
}
}
}
.............
1
0 0 0
1 1 1 1 1
0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1
.................................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);

[Link]("Enter the number : ");


int n=[Link]();
for (int i = 1; i <= n; i++)
{
for (int j=1;j<=n-i ;j++ )
{
[Link](" ");
}
for (int j=1;j<=2*i-1 ;j++ )
{
[Link]((char)(j+64)+" " );

}
[Link]();
}
}
}
......................
A
A B C
A B C D E
A B C D E F G
A B C D E F G H I
................................................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);

[Link]("Enter the number : ");


int n=[Link]();
for (int i = 1; i <= n; i++)
{
for (int j=1;j<=n-i ;j++ )
{
[Link](" ");
}
for (int j=1;j<=2*i-1 ;j++ )
{
[Link]((char)(j+96)+" " );

}
[Link]();
}
}
}
....................
a
a b c
a b c d e
a b c d e f g
a b c d e f g h i

........................................................

import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number : ");
int n=[Link]();
int k=1;
for (int i = 1; i <= n; i++)
{
for (int j=1;j<=n-i ;j++ )
{
[Link](" ");
}
for (int j=1;j<=2*i-1 ;j++ )
{
[Link](k%2+" " );
k++;
}
[Link]();
}
}
}
..............................

1
0 1 0
1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
..................................................................

import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);

[Link]("Enter the number : ");


int n=[Link]();

for (int i = n; i >=1; i--)


{
for (int j=1;j<=n-i;j++ )
{
[Link](" ");
}
for (int j=1;j<=2*i-1 ;j++ )
{
[Link]("*"+" " );
}
[Link]();
}
}
}
................

* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
...............................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);

[Link]("Enter the number : ");


int n=[Link]();

for (int i = n; i >=1; i--)


{
for (int j=1;j<=n-i;j++ )
{
[Link](" ");
}
for (int j=1;j<=2*i-1 ;j++ )
{
[Link](j%2+" " );
}
[Link]();
}
}
}
..............

1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1
1 0 1 0 1
1 0 1
1
..................................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);

[Link]("Enter the number : ");


int n=[Link]();

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


{
for (int j=n;j>i;j-- )
{
[Link](" ");
}
for (int k=1;k<=i ;k++ )
{
[Link](k+" " );
}
for (int m=i-1;m>=1 ;m-- )
{
[Link](m+" " );
}
[Link]();
}
}
}
...............

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
...................................
*********************22/02/2022************************
import [Link];
class Patterns
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int star=1;
int space=n/2;
for (int i=1;i<=n ;i++ )
{
for (int j=1;j<=space;j++ )
{
[Link](" ");
}
for (int j=1;j<=star ;j++ )
{
[Link]("* ");
}
if(i<=n/2)
{
space--;
star=star+2;
}
else
{
space++;
star=star-2;
}
[Link]();
}
}
}
.......................
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
...........................................................
import [Link];
class Patterns
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int star=n;
int space=0;
for (int i=1;i<=n ;i++ )
{
for (int j=1;j<=space;j++ )
{
[Link](" ");
}
for (int j=1;j<=star ;j++ )
{
[Link]("* ");
}
if(i<=n/2)
{
space++;
star=star-2;

}
else
{
space--;
star=star+2;
}
[Link]();
}
}
}
.........................
* * * * * * *
* * * * *
* * *
*
* * *
* * * * *
* * * * * * *
............................................................
import [Link];
class Patterns
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int star=n;
int space=0;
for (int i=1;i<=n ;i++ )
{
for (int j=1;j<=space;j++ )
{
[Link](" ");
}
for (int j=1;j<=star ;j++ )
{
[Link](j%2+" ");
}
if(i<=n/2)
{
space++;
star=star-2;

}
else
{
space--;
star=star+2;
}
[Link]();
}
}
}
............................
1 0 1 0 1 0 1
1 0 1 0 1
1 0 1
1
1 0 1
1 0 1 0 1
1 0 1 0 1 0 1
...........................................
import [Link];
class Patterns
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int star=n;
int space=0;
for (int i=1;i<=n ;i++ )
{
for (int j=1;j<=space;j++ )
{
[Link](" ");
}
for (int j=1;j<=star ;j++ )
{
[Link](i%2+" ");
}
if(i<=n/2)
{
space++;
star=star-2;

}
else
{
space--;
star=star+2;
}
[Link]();
}
}
}
...................
1 1 1 1 1 1 1
0 0 0 0 0
1 1 1
0
1 1 1
0 0 0 0 0
1 1 1 1 1 1 1
..........................................................
import [Link];
class Patterns
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int star=n;
int space=0;
for (int i=1;i<=n ;i++ )
{
for (int j=1;j<=space;j++ )
{
[Link](" ");
}
for (int j=1;j<=star ;j++ )
{
[Link](j+" ");
}
if(i<=n/2)
{
space++;
star=star-2;

}
else
{
space--;
star=star+2;
}
[Link]();
}
}
}
...............................
1 2 3 4 5 6 7
1 2 3 4 5
1 2 3
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
...........................................................
import [Link];
class Patterns
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int star=n;
int space=0;
for (int i=1;i<=n ;i++ )
{
for (int j=1;j<=space;j++ )
{
[Link](" ");
}
int k=1;
for (int j=1;j<=star ;j++ )
{
[Link](k+" ");
if(j<=star/2)
k++;
else
k--;
}
if(i<=n/2)
{
space++;
star=star-2;

}
else
{
space--;
star=star+2;
}
[Link]();
}
}
}
......................
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
......................................................
import [Link];
class Patterns
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int star=n;
int space=0;
for (int i=1;i<=n ;i++ )
{
for (int j=1;j<=space;j++ )
{
[Link](" ");
}
int k=star/2+1;
for (int j=1;j<=star ;j++ )
{
[Link](k+" ");
if(j<=star/2)
k--;
else
k++;
}
if(i<=n/2)
{
space++;
star=star-2;

}
else
{
space--;
star=star+2;
}
[Link]();
}
}
}
..................
4 3 2 1 2 3 4
3 2 1 2 3
2 1 2
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
......................................................
import [Link];
class Patterns
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int star=n;
int space=0;
for (int i=1;i<=n ;i++ )
{
for (int j=1;j<=space;j++ )
{
[Link](" ");
}
int k=star/2+1;
for (int j=1;j<=star ;j++ )
{
[Link]((char)(64+k)+" ");
if(j<=star/2)
k--;
else
k++;
}
if(i<=n/2)
{
space++;
star=star-2;

}
else
{
space--;
star=star+2;
}
[Link]();
}
}
}
.................................
D C B A B C D
C B A B C
B A B
A
B A B
C B A B C
D C B A B C D
.................................................................
import [Link];
class Patterns
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int star=1;
int space=n/2;
for (int i=1;i<=n ;i++ )
{
for (int j=1;j<=space;j++ )
{
[Link](" ");
}
for (int j=1;j<=star ;j++ )
{
if(j==1 || j==star)
[Link]("* ");
else
[Link](" ");
}
if(i<=n/2)
{
space--;
star=star+2;
}
else
{
space++;
star=star-2;
}
[Link]();
}
}
}
.................
*
* *
* *
* *
* *
* *
*
........................................................
import [Link];
class Patterns
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int star=1;
int space=n/2;
for (int i=1;i<=n ;i++ )
{
for (int j=1;j<=space;j++ )
{
[Link](" ");
}
int k=1;
for (int j=1;j<=star ;j++ )
{
[Link](k+" ");
if(j<=star/2)
k++;
else
k--;
}
if(i<=n/2)
{
space--;
star=star+2;
}
else
{
space++;
star=star-2;
}
[Link]();
}
}
}
.............
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
....................................................
import [Link];
class Patterns
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int star=1;
int space=n/2;
for (int i=1;i<=n ;i++ )
{
for (int j=1;j<=space;j++ )
{
[Link](" ");
}
int k=1;
for (int j=1;j<=star ;j++ )
{
[Link]((char)(96+k)+" ");
if(j<=star/2)
k++;
else
k=k-1;
}
if(i<=n/2)
{
space--;
star=star+2;
}
else
{
space++;
star=star-2;
}
[Link]();
}
}
}
..................
a
a b a
a b c b a
a b c d c b a
a b c b a
a b a
a
......................................................
import [Link];
class Patterns
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int star=1;
int space=n/2;
for (int i=1;i<=n ;i++ )
{
for (int j=1;j<=space;j++ )
{
[Link](" ");
}
int k=star/2+1;
for (int j=1;j<=star ;j++ )
{
[Link](k+" ");
if(j<=star/2)
k--;
else
k++;
}
if(i<=n/2)
{
space--;
star=star+2;
}
else
{
space++;
star=star-2;
}
[Link]();
}
}
}
..........................

1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
3 2 1 2 3
2 1 2
1
.......................................................

import [Link];
class Patterns
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
for (int i=1;i<=n ;i++ )
{
for (int j=1;j<=n ;j++ )
{
if(i==1|| i==n || j==1 ||j==n ||i==n/2+1 ||
j==n/2+1 || i==j || i+j==n+1)
[Link]("* ");
else
[Link](" ");
}
[Link]();
}
}
}
.......................................
* * * * * * * * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * * * * * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * * * * * * * *
.........................................................................
....................
import [Link];
class Patterns
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();
int star=1;
int space=n/2;
for (int i=1;i<=n ;i++ )
{
for (int j=1;j<=space;j++ )
{
[Link](" ");
}
int k=space+1;
for (int j=1;j<=star ;j++ )
{
[Link](k+" ");
if(j<=star/2)
k--;
else
k++;
}
if(i<=n/2)
{
space--;
star=star+2;
}
else
{
space++;
star=star-2;
}
[Link]();
}
}
}
..........................
4
3 4 3
2 3 4 3 2
1 2 3 4 3 2 1
2 3 4 3 2
3 4 3
4

***************************************24/01/2022************************
********************
import [Link];
class Patterns
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();

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


{
for (int j=1;j<=space;j++ )
{
[Link](" ");
}
int k=1;
for (int j=1;j<=star ;j++ )
{
[Link](k+" ");
k=k*(i-j)/j; ///PASCAL TRIANGLE

}
[Link]();
}
}
}
......................
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
.......................................................................
import [Link];
class Patterns
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number : ");
int n=[Link]();

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


{
for (int j=1;j<=space;j++)
{
[Link](" ");
}
for (int j=1;j<=star ;j++ )
{
[Link](j+" ");
if(i!=j)
[Link]("* ");

}
[Link]();
}
}
}
....................
1
1 * 2
1 * 2 * 3
1 * 2 * 3 * 4
1 * 2 * 3 * 4 * 5
.........................................................................
.....
******************************25/01/2022*********************************
**

####### ARRAY ##########

Array is an object in java it contains group of elements..


Array elements are stored in a contiguous memory location....
Array elements are stored in heap area....
to handle multiple elements or group of elements in the same name....

To differentiate every elements in java we have to index....


index start from zero...1st element in oth index and 2nd element in 1st
index....

Array object has one non static final variable in the name length.....
Length: it gives how many elements present in the array......

All the array objects created to the dynamically created class


and array objects extends from object class implements from Serializable
and
cloneable interface

single dimentional array


multidimensional array
***"Array is fixed Size in java"

<data type> varName[]; //syntax

int ar[]; or int []ar; or int[] ar;

int a,b,c[];
int []p,q,r;
int[] x,y,z;

int a,b[],c;
* int p,[]q,r; /// not possible
.................................................
int ar[]={23,45,65,38,29}; // declaration / initializzation

[Link](ar); // base address


[Link]([Link]); //5
[Link](ar[0]); //23
[Link](ar[1]); //45
[Link](ar[[Link]-1]); //29
.........................................................................
for(int i=0; i<[Link];i++) // 0 1 2 3 4
{ // 23 45 65 38 29
[Link](i+"-->"+ar[i]);
}
.........................................................................
............

** <data type> <var name>[]= new <data types>[size]; //declaration


and allocation

int ar[]= new int[5] // declaration and allocation

ar[0]=56;
ar[1]=86;
ar[3]=45;
ar[4]=96; // output :
for(int i=0; i<[Link];i++) // 0 1 2 3 4
{ // 56 86 0 45 95
[Link](i+"-->"+ar[i]);
}

output: 0 -->56
1 -->86
2 -->0
3 -->45
4 -->95

...................................................................
import [Link];
class MainArray
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the size of Array : ");
int n=[Link]();
int x[]=new int[n];
[Link]("enter "+n+"values");
for(int i=0; i<n;i++)
{
x[]=new int[n];
}

[Link]("USER ENTERD ARRAY");


for(int i=0; i<n;i++)
{
[Link](x[i]+" "+i);
}
}
.......................Assignment........................................
.

// 1. WAJp to calculate the sum of integer array elements?


import [Link];
class SumArrays
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the size of Array : ");
int n=[Link]();
int x[]=new int[n];
[Link]("enter "+n+" values");
for(int i=0; i<[Link];i++)
{
x[i]=[Link]();
}
[Link]("User entered array");

int sum=0;
for(int i=0; i<[Link];i++)
{
sum+=x[i];
}
[Link]("The sum of array element is :
"+sum);
}
}
.........................
output:

Enter the size of Array : 5


enter 5 values
34
65
23
54
44
User entered array
The sum of array element is : 220
.........................................................................
........
// 2. WAJp to find the biggest element from the array?
import [Link];
class BiggestElement
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the size of Array : ");
int n=[Link]();
int x[]=new int[n];
[Link]("enter "+n+" values");
for(int i=0; i<[Link];i++)
{
x[i]=[Link]();
}
[Link]("User entered array");
int big=x[0];
//int m[]=[Link]();
for(int i=0; i<[Link];i++)
{
if(x[i]>big)
big=x[i];
}
[Link]("The Biggest element of array is :
"+big);
}
}
........OR.........

import [Link];
class BiggestNo
{
public static void main(String [] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter Array Size :");
int i =[Link]();
int a [] = new int [i];
int p;
for ( p=0;p<[Link] ;p++ )
{
a[p]=[Link]();
}
for (p=0;p<[Link] ;p++ )
{
for(int q=p; q<[Link];q++)
{
if (a[p]>a[q])
{
[Link](a[p]);
break;
}
[Link]();
}
}
}
}

.........................................................................
..................................

//3. WAJp to calculate avg of double type array element?


import [Link];
class AvgArray
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the size of Array : ");
int n=[Link]();
double x[]=new double[n];
[Link]("enter "+n+" values");
for(int i=0; i<[Link];i++)
{
x[i]=[Link]();
}
[Link]("User entered array");

double sum=0.0;
for(double d : x)
{
sum=sum+d;
}
double avg=sum/[Link];
[Link]("The avg of array element is :
"+avg);
}
}

............................
Enter the size of Array : 4
enter 4 values
43
31
42
56
User entered array
The avg of array element is : 43.0
.........................................................................
..........................

//4. WAJP to create the duplicate of array?

//4. WAJP to create the duplicate of array?


class DuplicateArr
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the size of Array : ");
int n=[Link]();
int ar[]=new int[n];
[Link]("enter "+n+" values");
for(int i=0; i<[Link];i++)
{
ar[i]=[Link]();
}
[Link]("User entered array");
int br[]=ar; // alies

int cr[] = new int[[Link]];

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

}
}
................OR......................

import [Link];
class DuplicateArray
{
public static void main(String[] args)
{
int [] a = {10,20,30,40,50};
for (int s:a)
[Link](s+" ");
[Link]();
int []b = a;
for (int d:b)
[Link](d+" ");
}
}

.........................................................................
..............................

5. WAJP to read n names from the user and store it in the array?

import [Link];
class MainArrayStore
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the size of array : ");
int s=[Link]();
String names[]= new String[s];
[Link]("Enter "+s+" names");

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

}
[Link]("Enter the names : ");

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


{
[Link](names[i]);
}
}
}
.................................................................

*********************************26/01/2022******************************
******
// WAJP to read n names from the user and store it in the array ND
display in reverse order?

import [Link];
class MainArrayStore
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the size pf array : ");
int s=[Link]();
String names[]= new String[s];
[Link]("Enter "+s+" names");

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

}
[Link]("Enter the names in reverse ordered : ");

for(int i=[Link]-1;i>=0;i++)
{
[Link](names[i]);
}
}
}
.........................................................................
..........
int[] getArray()
{

<return type>[] <methodname>([parameterlist])


{

}
void displayArray(int ar[]) or void displayArray(int[] ar)
{ {

} }
<returntype><methodname>(<datatype><varname>[])
{

}
.........................................................................
.........

**********************************27/01/2022*****************************
********
//define the method to return the smallest elements from the integer
array?

import [Link];
class MainSmallest
{
static int getSmallest(int ar[])
{
int sm=ar[0];
for (int i=0;i<[Link];i++ )

{
if(ar[i]<sm)
sm=ar[i];
}
return sm;
}

public static void main(String[] args);


{
Scanner sc =new Scanner([Link]);
[Link]("enter the size of an array : ");
int n=[Link]();
int ar[]=new int[n];
[Link]("enter "+n+" values");
for (int i=0;i<[Link] ;i++ )
{
ar[i]=[Link]();
}
int small=getSmallest(ar);
[Link]("Smalest is : "+small);
}
}
>>>>>OR<<<<<>>>>

class SmallestArrayElement
{
public static void main(String [] args)
{
[Link]("Smallest Element is :"+smallestEle());

static int smallestEle()


{
int a[] = {22,56,82,21,56};
int min = a[0];

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


{
if (a[i]<min)
{
min = a[i];
}
}
return min;

}
}
.........................................................................

//define the method to return how many prime numbers present in the
array?

import [Link];
class MainPrime
{
static boolean isPrimeNumber(int n)
{
for (int i=2; i<n/2;i++ )
{
if(n%2==0)
return false;
}
return true;
}
static int countPrime(int []x);
{
int count=0;
for (int i=0;i<[Link] ;i++ )
{
boolean re=isPrimeNimber(x[i]);
if(re==true);
count++;
}
return count;
}

public static void main(String[] args);


{
Scanner sc =new Scanner([Link]);
[Link]("enter the size of an array : ");
int n=[Link]();
int ar[]=new int[n];
[Link]("enter "+n+" values");
for (int i=0;i<[Link] ;i++ )
{
ar[i]=[Link]();
}
int pc=countPrime(ar);
[Link]("Number of prime numbers are : "+pc);
}
}

.........................................................................
...........

//merge to array and display in single array


class MainMerge

{
static int()[] merge(int a[], int b[])
{
int c[]=new int[[Link]+[Link]];
for(int i=0; i<[Link];i++)
{
c[i]=a[i];
}

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


{
c[[Link]+j)]=b[j];
}
return c;
}

static int readArr()


{
Scanner sc =new Scanner([Link]);
[Link]("enter the size of an array : ");
int n=[Link]();
int ar[]=new int[n];
[Link]("enter "+n+" values");
for (int i=0;i<[Link] ;i++ )
{
ar[i]=[Link]();
}
return ar;
}

static int dispArr(int ar[])


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

public static void main(String[] args)


{
[Link]("Read the first array ");
int x[]=readArr();

[Link]("Read the second array");


int y[]=readArr();

int z[]=merge(x,y);

[Link]("user entred first array ");


dispArr(x);
[Link]("user entred second array : ");
dispArr(y);
[Link]("After merge two arrays : ");
dispArr(z);
}
}
.........................................................................
.............

//define a method to reverse the array

class MainMerge

{
static int readArr()
{
Scanner sc =new Scanner([Link]);
[Link]("enter the size of an array : ");
int n=[Link]();
int ar[]=new int[n];
[Link]("enter "+n+" values");
for (int i=0;i<[Link] ;i++ )
{
ar[i]=[Link]();
}
return ar;
}

static int dispArr(int ar[])


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

public static void main(String[] args)


{
int x[]=readArr(); // 8 7 6 5 9

[Link]("Before the reverse : ");

dispArr(x); // 8 7 6 5 9
[Link]("After the reverse: ");
reverse(x);
dispArr(x); // 9 5 6 7 8

}
static void reverse(int a[])
{
for (int i=0;i<[Link]/2;i++ )
{
int temp=a[i];
a[i]=a[[Link]-1-i];
a[[Link]-1-i]=temp;
}
}
}

....................................evening
lecture...............................
// search element present or not
class MainSearch
{
static int searchArr(int x[], int ele)
{
for (int i=0;i<[Link] ;i++ )
{
if(x[i]==ele)
return i;
}
return -1;
}
public static void main(String[] args)
{
int ar[] {34,56,43,28,67};
int in=searchAr(ar,78);
if(in==-1)
[Link]("element is not there");
else
[Link]("element is present at "+in);

}
}
.......................................................................

// define a method to count how many elements between the specific range?
class CountInRange
{
static int countInTheRange(int x[], int start, int end)
{
int count=0;
for (int i=0;i<[Link] ;i++ )
{
if(x[i]>=start && x[i]<=end)
count++;
}
return count;
}
public static void main(String[] args)
{
int ar[] ={34,56,43,343,45,28,67};
int in=countInTheRange(ar,34,67);
[Link]("element is present at "+in);

}
}

*********************************28/01/2022******************************
*********

class MainArrayMerge
{
static int()[] mergeS(int []x, int []y)
{
int z[]=new int[[Link]+[Link]];
int i=0; j=0; k=0;
while(i<[Link] && j<[Link])
{
if(x[i]<y[j])
{
z[k]=x[i];
i++;
}
else
{
z[k]=y[j];
j++;
}
k++;
}
while (i<[Link])
{
z[k]=x[i];
i++;
k++;
}
while (j<[Link])
{
z[k]=y[j];
j++;
k++;
}
return z;
}
public static void main(String[] args)
{
int a[]={13,24,37,46,52};
int b[]={25,28,32,45};
int c[]=mergeS(a,b);
for(int i=0; i<[Link];i++)
{
[Link](c[i]+" ");
//13,24,25,28,32,37,45,46,52
}
}
.............................................................
//delete the index in the specified index...

class MainDelete

{
static int()[] deleteArr(int []x, int in)
{
if(in<0||in>[Link])
{
[Link](" index not in the range");
return x;
}
int y[]= new int[[Link]-1];

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


{
if(i<in)
y[i]=x[i];
else
y[i]=x[i+1];
}
return y;

public static void main(String[] args)


{

int a[]={23,24,37,46,52};

a=deleteArr(a,2);

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


{
[Link](a[i]+" "); //23,24,46,52
}
}
}
.................................................................

//insert the index in the specified index...

class MainInsert

{
static int[] insertArr(int []x, int in,element)
{
if(in<0||in>[Link])
{
[Link](" index not in the range");
return x;
}
int y[]= new int[[Link]+1];

y[in]=element;

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


{
if(i<in)
y[i]=x[i];
else
y[i+1]=x[i];
}
return y;

public static void main(String[] args)


{
int a[]={13,24,37,46,52};

a=deleteArr(a,2,39);

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


{
[Link](a[i]+" "); //13,24,39,37,46,52
}
}
}
.........................................................................
.....

// how many odd numbers and even numbers present in the array

class MainEO

{
static int[] countEO(int []x)
{
int ec=0, oc=0;

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


{
if(x[i]%2==0)
ec++;
else
oc++;
}
int count[]={ce,co};
return count;

public static void main(String[] args)


{
int a[]={13,24,37,46,52};

int c[]=countEO(a);

[Link](c[1]+" number of odd numbers ");


[Link](c[0]+" nember of even numbers");

}
}
.........................................................................
................

Common questions

Powered by AI

Using a while loop for calculating factorial involves initializing a result variable and a counter, iteratively multiplying the result by the counter while decrementing the number until it reaches one. This approach is straightforward but requires careful management of loop control. In contrast, a nested for loop encapsulates this logic succinctly with clearer structure, iterating explicitly over the range while calculating factorial products directly, making it easier to integrate with other loops when needed (e.g., sum of factorials). Both approaches are effective, but for loops offer more clear and concise control for factorial computations within broader contexts .

In Java, a program can simultaneously calculate the sum of even and odd digits by initializing two accumulator variables for even and odd sums. Using a loop, each digit is extracted via modulus operation, and based on modulo operation (d%2), the digit is added to the respective accumulator (even or odd) using conditional statements. The loop proceeds until all digits are processed, after which both sums are printed .

The algorithm to determine if a number is a 'strong number' involves calculating the sum of the factorials of its digits and comparing it to the original number. The program uses a loop to extract each digit using modulus operation, computes its factorial in a nested loop, and adds the factorial to a cumulative sum. After processing all digits, the resulting sum is compared with the original number; if they are equal, the number is strong .

A happy number is characterized by forming a sequence starting with any positive integer where each number is replaced by the sum of the squares of its digits, and this process is repeated until the sequence converges at 1. Programmatically, a loop is used to perform these transformations, continuously replacing the number with the sum of the squares of its digits. If the sequence results in 1, it’s a happy number; otherwise, it's not (e.g., if it settles into a cycle that does not include 1).

Reversing a number involves initializing a result variable to zero, then repeatedly extracting the last digit of the number by taking the modulus with 10. The extracted digit is then added to the result after multiplying the current result by 10 to shift previous digits one place to the left. The original number is reduced by dividing it by 10 to remove the processed digit. This loop continues until all digits are processed, resulting in a reversed number which is then printed .

The method to check if a number is an Armstrong number involves calculating the sum of the cubes of its digits and comparing it to the original number; if they are equal, the number is an Armstrong number . In contrast, a perfect number is determined by summing its divisors (excluding the number itself) and checking if the sum equals the original number .

To print the multiplication table for a given number using a for loop, initialize a loop with a counter from 1 to 10, corresponding to the multiplication factors. In each iteration, multiply the given number by the loop counter and print the result. The loop iterates ten times, providing a complete table from 1 to 10 times the number .

To calculate the product of digits in a number using a do-while loop, the program initializes a product variable to 1 and repeatedly multiplies it by the last digit, obtained through the modulus operation with 10. After each multiplication, the number is divided by 10 to remove the processed digit. This loop continues until all digits are processed, resulting in the total product being printed .

The method to count prime digits in a number involves iterating through each digit of the number, obtained by modulus and division operations, and checking if the digit is one of the prime numbers (2, 3, 5, 7). A counter is incremented for each match. This approach is efficient for single-digit primes and leverages logical checks within a loop for digit extraction and evaluation, avoiding unnecessary computation .

To determine if a number is a palindrome, the program reverses the digits of the number and compares the reversed number to the original. If they are identical, the number is a palindrome. This is accomplished using a loop that extracts each digit using modulus operation, constructs the reverse incrementally by multiplying the result by 10 and adding the next digit, and checks equivalency with the original .

You might also like