Java Programming Basics and Examples
Java Programming Basics and Examples
@@@@
***********************PROGRAMMING JAVA ONLINE
BATCH*************************
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@
.......................................................
// 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;
}
}
}
.........................................................................
.....
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]();
.........................................................................
...
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]();
}
}
......................................................................
///// 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");
}
}
...................................................................
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");
}
}
...................................................
while(n>0)
{
sum+=n;
n--;
}
[Link](sum); //15 answer
[Link]("thank you");
}
}
.......................................................................
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");
}
}
.......................................................................
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..............................
.....
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++;
}
}
}
........................................................................
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++;
}
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);
}
}
.........................................................................
....
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";
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);
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...........................
..........
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);
}
}
.........................................................................
..........
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);
}
}
.......................................................
{
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);
}
}
.........................................................................
..
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");
}
}
}
.........................................................................
.......
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..........................
......
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.........................
............
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);
}
}
.........................................................................
..
import [Link];
class MultiplicationTable
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter your no :");
int no = [Link]();
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);
.................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]("circumference is : "+cir);
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;
}
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;
}
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;
}
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)
{
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;
}
}
.........................................................................
}
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;
}
}
.........................................................................
...........
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);
}
}
.........................................................................
.
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);
}
}
......................................................................
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);
class Abcd
{
public static void main(String[] args)
{
for (int i=0; i<5;i++ ){
}
..............
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
.......................................................................
class Abcd
{
public static void main(String[] args)
{
for (int i=0; i<5;i++ ){
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]();
}
}
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]();
}
}
}
.............
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]();
}
}
}
......................
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]();
}
}
}
....................
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]);
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
...............................................
import [Link];
class Pattern1
{
public static void main(String[] args)
{
Scanner sc=new Scanner([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]);
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]();
}
[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]();
}
[Link]();
}
}
}
....................
1
1 * 2
1 * 2 * 3
1 * 2 * 3 * 4
1 * 2 * 3 * 4 * 5
.........................................................................
.....
******************************25/01/2022*********************************
**
Array object has one non static final variable in the name length.....
Length: it gives how many elements present in the array......
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
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];
}
int sum=0;
for(int i=0; i<[Link];i++)
{
sum+=x[i];
}
[Link]("The sum of array element is :
"+sum);
}
}
.........................
output:
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]();
}
}
}
}
.........................................................................
..................................
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
.........................................................................
..........................
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 : ");
*********************************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()
{
}
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;
}
class SmallestArrayElement
{
public static void main(String [] args)
{
[Link]("Smallest Element is :"+smallestEle());
}
}
.........................................................................
//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;
}
.........................................................................
...........
{
static int()[] merge(int a[], int b[])
{
int c[]=new int[[Link]+[Link]];
for(int i=0; i<[Link];i++)
{
c[i]=a[i];
}
int z[]=merge(x,y);
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;
}
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];
int a[]={23,24,37,46,52};
a=deleteArr(a,2);
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;
a=deleteArr(a,2,39);
// how many odd numbers and even numbers present in the array
class MainEO
{
static int[] countEO(int []x)
{
int ec=0, oc=0;
int c[]=countEO(a);
}
}
.........................................................................
................
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 .