0% found this document useful (0 votes)
2 views

Javapractice Prog for Lab

The document contains multiple Java programs demonstrating various programming concepts such as variable initialization, operators, simple interest calculation, area calculations, and conditional statements. Each program is accompanied by sample outputs illustrating their functionality. Additionally, there are classes defined for specific tasks like calculating rental charges for a bike and determining voting eligibility based on age.

Uploaded by

sherr3786
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Javapractice Prog for Lab

The document contains multiple Java programs demonstrating various programming concepts such as variable initialization, operators, simple interest calculation, area calculations, and conditional statements. Each program is accompanied by sample outputs illustrating their functionality. Additionally, there are classes defined for specific tasks like calculating rental charges for a bike and determining voting eligibility based on age.

Uploaded by

sherr3786
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 69

public class VariablesInitialization

{
public static void main(String[] args)
{
// TODO code application logic here
boolean flag=true;
System.out.println(flag);
System.out.println("Flag="+flag);
System.out.println("--------------------");
char ch='A';
System.out.println(ch);
System.out.println("Ch="+ch);
System.out.println("Ch as ASCII code="+(int)ch);
System.out.println("--------------------");
int number=10;
System.out.println(number);
System.out.println("Number="+number);
System.out.println("--------------------");
float f1=10, f2=10.99f;
System.out.println(f1);
System.out.println("Float="+f1);
System.out.println(f2);
System.out.println("Float f2="+f2);
System.out.println("--------------------");
String name="India";
System.out.println(name);
System.out.println("Name="+name);
}
}

Output:

A
Ch=A
Ch as ASCII code=65
--------------------
10
Number=10
--------------------
10.0
Float=10.0
10.99
Float f2=10.99
-------------
public class JavaOperators
{
public static void main(String args[])
{
System.out.println("Example 1:-");
/*Java Unary Operator Example: ++ and --*/
int x = 10;
System.out.println(x++); //10 (11)
System.out.println(++x); //12
System.out.println(x--); //12 (11)
System.out.println(--x); //10
int a = 10;
int b = 10;
System.out.println(a++ + ++a); //10+12=22
System.out.println(b++ + b++); //10+11=21
System.out.println("Example 2:-");
/*Java Unary Operator Example: ~ and !*/
a = 10;
b = -10;
boolean p = true;
boolean q = false;
System.out.println(~a); /*-11 (minus of total positive value which starts
from 0)*/
System.out.println(~b); /*9 (positive of total minus, positive starts from
0)*/
System.out.println(!p); /*false (opposite of boolean value)*/
System.out.println(!q); /*true*/
System.out.println("Example 3:-");
/*Java Arithmetic Operator Example*/
a = 10;
b = 5;
System.out.println(a + b); //15
System.out.println(a - b); //5
System.out.println(a * b); //50
System.out.println(a / b); //2
System.out.println(a % b); //0
System.out.println("Example 4:-");
/*Java Arithmetic Operator Example: Expression*/
System.out.println(10 * 10 / 5 + 3 - 1 * 4 / 2);
System.out.println("Example 5:-");
/*Java AND Operator Example: Logical && and Bitwise &*/
x = 10;
int y = 5;
int z = 20;
System.out.println(x < y && x < z); //false && true = false
System.out.println(x < y & x < z); //false & true = false
System.out.println("Example 6:-");
/*Java OR Operator Example: Logical || and Bitwise |*/
a = 10;
b = 5;
int c = 20;
System.out.println(a > b || a < c); //true || true = true
System.out.println("Example 7:-");
/*Java Ternary Operator Example*/
a = 2;
b = 5;
int min = (a < b) ? a : b;
System.out.println(min);
System.out.println("Example 8:-");
/*Java Assignment Operator Example*/
a = 10;
b = 20;
a += 4; //a=a+4 (a=10+4)
b -= 4; //b=b-4 (b=20-4)
System.out.println(a);
System.out.println(b);
System.out.println("Example 9:-");
/*Java Assignment Operator Example*/
a = 10;
a += 3; //10+3
System.out.println(a);
a -= 4; //13-4
System.out.println(a);
a *= 2; //9*2
System.out.println(a);
a /= 2; //18/2
System.out.println(a);
}
}

Output:

Example 1:-
10
12
12
10
22
21
Example 2:-
-11
9
false
true
Example 3:-
15
5
50
2
0
Example 4:-
21
Example 5:-
false
false
Example 6:-
true
Example 7:-
2
Example 8:-
14
16
Example 9:-
13
9
18
9
b. tech.

import java.util.Scanner;
public class JavaSimpleInterest
{
public static void main(String[] args)
{
int p,r,t,si;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a amount=");
p=sc.nextInt();
System.out.print("Enter a rate=");
r=sc.nextInt();
System.out.print("Enter a time=");
t=sc.nextInt();

si=(p*r*t)/100;
System.out.println("Simple interest="+si);
}
}

Output:

Enter a amount=1000
Enter a rate=10
Enter a time=3
Simple interest=300
import java.util.Scanner;
public class JavaFarenheitToCelsius
{
public static void main(String[] args)
{
float c,f;
Scanner sc=new Scanner(System.in);
System.out.print("Enter Fahrenhiet=");

f=sc.nextFloat();
c=((f-32)*5)/9;
System.out.println("Celcius="+c);
}
}

Output:

Enter Fahrenhiet=102
Celcius=38.88889

import java.util.Scanner;
public class JavaFindAreaRectangle
{
public static void main(String[] args)
{
int l,b,area;
Scanner sc=new Scanner(System.in);
System.out.print("Enter length=");
l=sc.nextInt();
System.out.print("Enter bredth=");
b=sc.nextInt();

area=l*b;
System.out.println("Area of Rectangle="+area);
}
}

Output:

Enter length=3
Enter bredth=4
Area of Rectangle=12
import java.util.Scanner;
public class JavaFindAreaCircle
{
public static void main(String[] args)
{
float area,pi,r;
pi=(float)22/7;
Scanner sc=new Scanner(System.in);
System.out.print("Enter radius=");
r=sc.nextFloat();
area=(float)pi*r*r;
System.out.println("Area of Circle="+area);
}
}

Output:

Enter radius=5
Area of Circle=78.57143
import java.util.Scanner;
public class JavaMarksAverage
{
public static void main(String[] args)
{
int math,english,science,art,computer,total,percent;
Scanner sc=new Scanner(System.in);
System.out.println("Enter marks out of 100");
System.out.print("Enter marks of math=");
math=sc.nextInt();
System.out.print("Enter marks of english=");
english=sc.nextInt();
System.out.print("Enter marks of science=");
science=sc.nextInt();
System.out.print("Enter marks of art=");
art=sc.nextInt();
System.out.print("Enter marks of computer=");
computer=sc.nextInt();

total=math+english+science+art+computer;
System.out.println("Total marks out of 500="+total);

percent=total/5;
System.out.println("Percent of marks="+percent);
}
}

public class JavaSwap


{
public static void main(String[] args)
{
int a=10, b=20, temp;
System.out.println("Before swapping");
System.out.println("value of a="+a);
System.out.println("value of b="+b);
temp=a;
a=b;
b=temp;
System.out.println("After swapping");
System.out.println("value of a="+a);
System.out.println("value of b="+b);
}
}

Output:

Before swapping
value of a=10
value of b=20
After swapping
value of a=20
value of b=10
b. tech. bca icse java java tutorials learn java mca programs

Share Tweet WhatsApp

PreviousNext

Computer language with very easy and simple method. From beginning especially to those
students who has problem in understanding.



Pages

 Java
 Java Programs
 C Language
 C Programs
 Computer Basics

Courses

 Java - ICSE Class 9th and 10th


 C Programming(B. Tech., MCA, BCA etc.)
 Math ICSE(6th, 7th, 8th)

Notes

 About
 Contact
 Privacy and Policy
 Terms & Conditions
 Cancellation and Refunds Policy

Contact
Call: +91-9670248484

WhatsApp: +91-9670248484

Copyrights ©2023 efaculty.in. All rights reserved.

public class JavaIncrementDecrementOperator


{
public static void main(String[] args)
{
int i=10,j=10,num=0;
System.out.println("i="+i);
System.out.println("j="+j);
System.out.println("num="+num);
System.out.println("---------------------------------------");
i++;
j--;
System.out.println("i="+i);
System.out.println("j="+j);
System.out.println("num="+num);
System.out.println("---------------------------------------");
num=i++;
System.out.println("i="+i);
System.out.println("j="+j);
System.out.println("num="+num);
System.out.println("---------------------------------------");
num=i++ + ++i;
System.out.println("i="+i);
System.out.println("j="+j);
System.out.println("num="+num);
System.out.println("---------------------------------------");
num=i++ + --j;
System.out.println("i="+i);
System.out.println("j="+j);
System.out.println("num="+num);
System.out.println("---------------------------------------");
num=--i + j++;
System.out.println("i="+i);
System.out.println("j="+j);
System.out.println("num="+num);
System.out.println("---------------------------------------");
num=++i + ++j;
System.out.println("i="+i);
System.out.println("j="+j);
System.out.println("num="+num);

}
}

Output:

i=10
j=10
num=0
---------------------------------------
i=11
j=9
num=0
---------------------------------------
i=12
j=9
num=11
---------------------------------------
i=14
j=9
num=26
---------------------------------------
i=15
j=8
num=22
---------------------------------------
i=14
j=9
num=22
---------------------------------------
i=15
j=10
num=25
Define a class called Mobike with the following description:
Instance variables/data members:
String bno – to store the bike’s number(UP65AB1234)
String name – to store the name of the customer
int days – to store the number of days the bike is taken on rent
int charge – to calculate and store the rental charge
Member methods:
void input( ) – to input and store the detail of the customer.
void compute( ) – to compute the rental chargeThe rent for a mobike is
charged on the following basis.
First five days Rs 500 per day;
Next five days Rs 400 per day
Rest of the days Rs 200 per dayvoid display ( ) – to display the details in
the following format:
Bike No. PhoneNo. No. of days Charge

import java.util.Scanner;

public class Mobike


{
String bno;
String name;
int days;
int charge;
public void input()
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your bike number= ");
bno = scanner.nextLine();
System.out.print("Enter your name= ");
name = scanner.nextLine();
System.out.print("Enter number of days= ");
days = scanner.nextInt();
}
public void compute()
{
if (days <= 5)
{
charge = 500 * days;
}
else if (days <= 10)
{
charge = 5 * 500 + (days - 5) * 400;
}
else
{
charge = 5 * 500 + 5 * 400 + (days - 10) * 200;
}
}
public void display()
{
System.out.println("Bike No.="+bno);
System.out.println("Name="+name);
System.out.println("Days="+days);
System.out.println("Charge="+charge);
}
public static void main(String[] args)
{
Mobike mobike=new Mobike();
mobike.input();
mobike.compute();
mobike.display();
}
}

Output:

Enter your bike number= UP65


Enter your name= Afaq Ahmad
Enter number of days= 12
Bike No.=UP65
Name=Afaq Ahmad
Days=12
Charge=4900
A person can votes if age is greater than or equal to 18.

import java.util.Scanner;

public class VotingAge


{
public static void main(String[] args)
{
int age;
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age=");
age = sc.nextInt();
if (age >= 18)
{
System.out.println("You are eligible for vote.");
}
else
{
System.out.println("You are not eligible for vote.");
}
}
}

Output:

Enter your age=19


You are eligible for vote.
import java.util.Scanner;

public class PassingDivision


{
public static void main(String[] args)
{
// TODO code application logic here
int math, english, science, art, computer, total, percent;
Scanner sc = new Scanner(System.in);
System.out.println("Enter marks out of 100");
System.out.print("Enter marks of math=");
math = sc.nextInt();
System.out.print("Enter marks of english=");
english = sc.nextInt();
System.out.print("Enter marks of science=");
science = sc.nextInt();
System.out.print("Enter marks of art=");
art = sc.nextInt();
System.out.print("Enter marks of computer=");
computer = sc.nextInt();
total = math + english + science + art + computer;
System.out.println("Total marks out of 500=" + total);
percent = total / 5;
System.out.println("Percent of marks=" + percent);
if (percent < 33)
{
System.out.println("Fail");
}
else if ((percent >= 33) && (percent < 45))
{
System.out.println("Third Division");
}
else if ((percent >= 45) && (percent < 60))
{
System.out.println("Second Division");
}
else
{
System.out.println("First Division");
}
}
}

Output:

Enter marks out of 100


Enter marks of math=45
Enter marks of english=65
Enter marks of science=78
Enter marks of art=75
Enter marks of computer=42
Total marks out of 500=305
Percent of marks
A day and night is 23 hours, 56 minutes and 4.1 seconds. It takes 365
days, 5 hours, 48 minutes and 45 seconds for the Earth to complete one
round of the sun.
In easy terms, the year that the remaining zero is divided by 4 will be a
leap year, but only that century will be a leap year, which is completely
divided by 400.

import java.util.Scanner;

public class LeapYear


{
public static void main(String[] args)
{
int year;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a year=");
year = sc.nextInt();
if (((year % 400 == 0) || (year % 4 == 0)) && (year % 100 != 0))
{
System.out.println("Leap year.");
}
else
{
System.out.println("Not a leap year");
}
}
}

Output:

Enter a year=1600
Not a leap year
import java.util.Scanner;
public class GreatestNumber {
public static void main(String[] args) {
int n1, n2, n3;
Scanner sc = new Scanner(System.in);

System.out.print("Enter n1 number=");
n1 = sc.nextInt();
System.out.print("Enter n2 number=");
n2 = sc.nextInt();
System.out.print("Enter n3 number=");
n3 = sc.nextInt();

if (n1 > n2) {


if (n1 > n3) {
System.out.println("n1 is greatest.");
} else {
System.out.println("n3 is greatest.");
}
} else {
if (n2 > n3) {
System.out.println("n2 is greatest.");
} else {
System.out.println("n3 is greatest.");
}
}
}
}

Output:

Enter n1 number=15
Enter n2 number=18
Enter n3 number=10
n2 is greatest.
import java.util.Scanner;
public class GreaterNumber {
public static void main(String[] args) {
int n1, n2;
Scanner sc = new Scanner(System.in);

System.out.print("Enter n1 number=");
n1 = sc.nextInt();
System.out.print("Enter n2 number=");
n2 = sc.nextInt();

if (n1 < n2) {


System.out.println("n2 is greater than n1.");
} else {
System.out.println("n1 is greater than n2.");
}
}
}

Output:

Enter n1 number=10
Enter n2 number=12
n2 is greater than n1.
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
int number;
Scanner sc = new Scanner(System.in);

System.out.print("Enter your number=");


number = sc.nextInt();

if (number % 2 == 0) {
System.out.println("Number is even.");
} else {
System.out.println("Number is odd.");
}
}
}

Output:

Enter your number=5


Number is odd.
import java.util.Scanner;
public class EqualNumber
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number=");
n = sc.nextInt();
if (n == 10)
{
System.out.println("n is equal to 10.");
}
}
}

import java.util.Scanner;

public class NameOfMonth


{
public static void main(String[] args)
{
int month;
Scanner sc = new Scanner(System.in);
System.out.print("Enter month=");
month = sc.nextInt();
switch (month)
{
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Enter valid month.");
}
}
}

Output:

Enter month=9
September
import java.util.Scanner;

public class NameOfDay


{
public static void main(String[] args)
{
int day;
Scanner sc=new Scanner(System.in);
System.out.print("Enter day=");
day=sc.nextInt();
switch(day)
{
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Enter valid day.");
}
}
}

Output:

Enter day=5
Friday
wo different numbers are said to be so Amicable Numbers if each sum
of divisors is equal to the other number.
Amicable Numbers are: (220, 284), (1184, 1210), (2620, 2924), (5020,
5564), (6232, 6368) etc.
Example– 220 and 284 are Amicable Numbers.

Divisors of 220 = 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110

1+2+4+5+10+11+20+22+44+55+110=284

Divisors of 284 = 1, 2, 7, 71, 142

1+2+7+71+142=220

import java.util.Scanner;
public class AmicableNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter 1st number= ");
int a = sc.nextInt();
System.out.print("Enter 2nd number: ");
int b = sc.nextInt();
int sumA = 0, sumB = 0;
for (int i = 1; i < a; i++)
{
if (a % i == 0)
{
sumA += i;
}
}
for (int i = 1; i < b; i++)
{
if (b % i == 0)
{
sumB += i;
}
}
if (sumA == b && sumB == a)
{
System.out.println("The numbers are Amicable Number.");
}
else
{
System.out.println("The numbers are not Amicable Number");
}
}
}

Output:

Enter 1st number= 284


Enter 2nd number: 220
The numbers are Amicable Number.
Armstrong Number is a positive number if it is equal to the
sum of cubes of its digits is called Armstrong number and if
its sum is not equal to the number then its not a Armstrong
number.
Armstrong Number Program is very popular in java, c language, python
etc.

Examples: 153 is Armstrong

(1*1*1)+(5*5*5)+(3*3*3) = 153

import java.util.Scanner;

/**
* @author AFAQ
*/
public class ArmstrongNumber
{
public static void main(String[] args)
{
int n,
cubeSum = 0, num, r;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
cubeSum = cubeSum + (r * r * r);
num = num / 10;
}
if (n == cubeSum)
{
System.out.println("Armstrong Number");
}
else
{
System.out.println("Not Armstrong Number");
}
}
}

Output:

Enter number=153
Armstrong Number
What is Armstrong Number?
Armstrong Number is a positive number if it is equal to the sum of cubes of its
digits is called Armstrong number and if its sum is not equal to the number then
its not a Armstrong number.
Armstrong Number Program is very popular in java, c language, python etc.
Examples: 153 is Armstrong
(1*1*1)+(5*5*5)+(3*3*3) = 153

What is Armstrong Number in Java?


Armstrong Number is a positive number if it is equal to the sum of cubes of its
digits is called Armstrong number and if its sum is not equal to the number then
its not a Armstrong number.
Armstrong Number Program is very popular in java, c language, python etc.
Examples: 153 is Armstrong
(1*1*1)+(5*5*5)+(3*3*3) = 153

What is Armstrong Number in C Language?


Armstrong Number is a positive number if it is equal to the sum of cubes of its
digits is called Armstrong number and if its sum is not equal to the number then
its not a Armstrong number.
Armstrong Number Program is very popular in java, c language, python etc.
Examples: 153 is Armstrong
(1*1*1)+(5*5*5)+(3*3*3) = 153

An Automorphic number is a number whose square “ends” in


the same digits as the number itself.
Examples: 5*5 = 25, 6*6 = 36, 25*25 = 625

import java.util.Scanner;

public class AutomorphicNumber


{
public static void main(String[] args)
{
int n, sqrNum, temp,sqrNumRemainder,c = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
temp=n;
while (temp > 0)
{
temp=temp/10;
c++;
}
sqrNum = n * n;
sqrNumRemainder= sqrNum%(int)Math.pow(10, c);
if(sqrNumRemainder==n)
{
System.out.println("Automorphic Number");
}
else
{
System.out.println("Not Automorphic Number");
}

}
}

Output:

Enter number=25
Automorphic Number
What is Automorphic Number?
An Automorphic number is a number whose square “ends” in the same digits as
the number itself. Examples: 5*5 = 25, 6*6 = 36, 25*25 = 625

What is Automorphic Number in Java?


An Automorphic number is a number whose square “ends” in the same digits as
the number itself.
Examples: 5*5 = 25, 6*6 = 36, 25*25 = 625

What is Automorphic Number in C Language?


An Automorphic number is a number whose square “ends” in the same digits as
the number itself.
Examples: 5*5 = 25, 6*6 = 36, 25*25 = 625

b. tech. bca icse java java tutorials learn java

A number is said to be Buzz Number if it ends with 7 or is


divisible by 7.
Example: 1007 is a Buzz Number.

import java.util.Scanner;
public class BuzzNumber
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter n=");
n = sc.nextInt();
if (n % 10 == 7 || n % 7 == 0)
{
System.out.println("Buzz number");
}
else
{
System.out.println("Not Buzz number");
}
}
}

Output:

Enter n=147
Buzz number
What is Buzz Number?
A number is said to be Buzz Number if it ends with 7 or is divisible by 7.
Example: 1007 is a Buzz Number.

What is Buzz Number in Java?


A number is said to be Buzz Number if it ends with 7 or is divisible by 7.
Example: 1007 is a Buzz Number.

A circular prime is a prime number with the property that


the number generated at each intermediate step when
cyclically permuting its digits will be prime. For example,
1193 is a circular prime, since 1931, 9311 and 3119 all are
also prime.

import java.util.Scanner;

public class CircularPrime


{
public static void main(String[] args)
{
boolean flag = true;
int n, num, r, c = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
c++;
num = num / 10;
}
num = n;
for (int i = 1; i <= c; i++)
{
r = num % 10;
num = num / 10;
num = (r * (int) Math.pow(10, c - 1)) + num;
if (!prime(num))
{
flag = false;
break;
}
}

if(flag)
{
System.out.println("Circular Prime");
}
else
{
System.out.println("Not Circular Prime");
}

}
static boolean prime(int n)
{
// TODO code application logic here
int i = 2;
boolean flag = true;
while (n > i)
{
if (n % 2 == 0)
{
flag = false;
break;
}
i++;
}
return flag;
}
}

Output:

Enter number=137
Circular Prime
What is Circular Prime?
A circular prime is a prime number with the property that the number generated
at each intermediate step when cyclically permuting its digits will be prime. For
example, 1193 is a circular prime, since 1931, 9311 and 3119 all are also prime.

What is Circular Prime in Java?


A circular prime is a prime number with the property that the number generated
at each intermediate step when cyclically permuting its digits will be prime. For
example, 1193 is a circular prime, since 1931, 9311 and 3119 all are also prime.

Two integers a and b are said to be relatively prime,


mutually prime, or coprime if the only positive integer that
divides both of them is 1. Example: 13 and 15 are co prime.

import java.util.Scanner;
public class CoPrimeNumbers
{
public static void main(String[] args)
{
int a, b, gcd = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a=");
a = sc.nextInt();
System.out.print("Enter b=");
b = sc.nextInt();
int min, max;
min = a;
if (min > b)
{
min = b;
max = a;
}
else
{
min = a;
max = b;
}
while (max > min)
{
int r = max % min;
if (r == 0)
{
gcd = min;
break;
}
else
{
max = min;
min = r;
}
}
if (gcd == 1)
{
System.out.println("Co Prime Numbers");
}
else
{
System.out.println("Not Co Prime Numbers");
}
}
}

Output:

Enter a=13
Enter b=15
Co Prime Numbers
What is CoPrime Numbers
Two integers a and b are said to be relatively prime, mutually prime, or coprime
if the only positive integer that divides both of them is 1. Example: 13 and 15 are
co prime.

What is CoPrime Numbers in Java?


Two integers a and b are said to be relatively prime, mutually prime, or coprime
if the only positive integer that divides both of them is 1. Example: 13 and 15 are
co prime.

A number is called Disarium number if the sum of its power


of the positions from left to right is equal to the number.
Example:

11 + 32 + 53 = 1 + 9 + 125 = 135

import java.util.Scanner;

public class DisariumNumber


{
public static void main(String[] args)
{
int r, n, num,digits=0,
sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
digits++;
num = num / 10;
}
num = n;
while (num > 0)
{
r = num % 10;
sum = sum + (int)Math.pow(r, digits);
num = num / 10;
digits--;
}

if(n==sum)
{
System.out.println("Disarium Number");
}
else
{
System.out.println("Not Disarium Number");
}

}
}

Output:

Enter number=135
Disarium Number
What is Disarium Number?
A number is called Disarium number if the sum of its power of the positions from
left to right is equal to the number.

What is Disarium Number in Java?


A number is called Disarium number if the sum of its power of the positions from
left to right is equal to the number.

A Duck number is a number which has zeroes present in it,


but there should be no zero present in the beginning of the
number. For example 3210

import java.util.Scanner;

public class DuckNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int r, n, num;
boolean flag=false;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
if(r==0)
{
flag=true;
}
num = num / 10;
}
if(flag)
{
System.out.println("Duck Number");
}
else
{
System.out.println("Not Duck Number");
}

}
}

Output:

Enter number=205
Duck Number
What is Duck Number?
A Duck number is a number which has zeroes present in it, but there should be
no zero present in the beginning of the number. For example 3210

What is Duck Number in Java?


A Duck number is a number which has zeroes present in it, but there should be
no zero present in the beginning of the number. For example 3210

Factorial is the product of all positive integers less than or


equal to n. Examples: 4! = 4 × 3 × 2 × 1 = 24

import java.util.Scanner;

public class Factorial


{
public static void main(String[] args)
{
// TODO code application logic here
int n,
fact = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of series=");
n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
fact = fact * i;
}
System.out.println("Factorial=" + fact);
}
}

Output:

Enter number of series=5


Factorial=120
Frequently Asked Questions
What is Factorial Program ?
Factorial is the product of all positive integers less than or equal to n. Examples:
4! = 4 × 3 × 2 × 1 = 24

What is Factorial Program in Java?


Factorial is the product of all positive integers less than or equal to n. Examples:
4! = 4 × 3 × 2 × 1 = 24

Factor a number or algebraic expression that divides


another number or expression evenly—i.e., with no
remainder. For example, 3 and 6 are factors of 12 because
12 ÷ 3 = 4 exactly and 12 ÷ 6 = 2 exactly. The other factors
of 12 are 1, 2, 4, 6 and 12.
Factors of 12: 1, 2, 3, 4, 6, 12.

import java.util.Scanner;

public class Factors


{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
{
System.out.println(i);
}
}
}
}

Output:

Enter number=6
1
2
3
6
What is Factors Program?
Factor a number or algebraic expression that divides another number or
expression evenly—i.e., with no remainder. For example, 3 and 6 are factors of
12 because 12 ÷ 3 = 4 exactly and 12 ÷ 6 = 2 exactly. The other factors of 12
are 1, 2, 4, 6 and 12.
Factors of 12: 1, 2, 3, 4, 6, 12.

What is Factors Program in Java?


Factor a number or algebraic expression that divides another number or
expression evenly—i.e., with no remainder. For example, 3 and 6 are factors of
12 because 12 ÷ 3 = 4 exactly and 12 ÷ 6 = 2 exactly. The other factors of 12
are 1, 2, 4, 6 and 12.
Factors of 12: 1, 2, 3, 4, 6, 12.

A series of numbers in which each number ( Fibonacci


number ) is the sum of the two preceding numbers. The
simplest is the series 0, 1, 1, 2, 3, 5, 8, etc.

import java.util.Scanner;

public class FibonacciSeries


{
public static void main(String[] args)
{
// TODO code application logic here
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of series=");
n = sc.nextInt();
int a = 0,
b = 1, c;
for (int i = 1; i <= n; i++)
{
System.out.println(a);
c = a + b;
a = b;
b = c;
}
}
}
1
23
456
7 8 9 10
11 12 13 14 15

public class FloydTriangle


{
public static void main(String[] args)
{
int k = 1;
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(k + " ");
k++;
}
System.out.println();
}
}
}
Greatest Common Divisor Program in Java
the greatest common divisor (gcd) of two or more integers,
which are not all zero, is the largest positive integer that
divides each of the integers. For example, the gcd of 8 and
12 is 4.

import java.util.Scanner;

public class GCDProgram


{
public static void main(String[] args)
{
int a, b, gcd = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a=");
a = sc.nextInt();
System.out.print("Enter b=");
b = sc.nextInt();
int min, max;
min = a;
if (min > b)
{
min = b;
max = a;
}
else
{
min = a;
max = b;
}
while (max > min)
{
int r = max % min;
if (r == 0)
{
gcd = min;
break;
}
else
{
max = min;
min = r;
}
}
System.out.println("GCD=" + gcd);
}
}
Output:

Enter a=15
Enter b=18
GCD=3
What is Greatest Common Divisor?
the greatest common divisor (gcd) of two or more integers, which are not all
zero, is the largest positive integer that divides each of the integers. For
example, the gcd of 8 and 12 is 4.

What is Greatest Common Divisor in Java?


the greatest common divisor (gcd) of two or more integers, which are not all
zero, is the largest positive integer that divides each of the integers. For
example, the gcd of 8 and 12 is 4.

A happy number is a natural number in a given number base


that eventually reaches 1 when iterated over the perfect
digital invariant function for. Those numbers that do not end
in 1 are -unhappy numbers.
import java.util.Scanner;

public class HappyNumber


{
public static void main(String[] args)
{
int n, r = 1, num, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 9)
{
while (num > 0)
{
r = num % 10;
sum = sum + (r * r);
num = num / 10;
}
num = sum;
sum = 0;
}
if (num == 1)
{
System.out.println("Happy Number");
}
else
{
System.out.println("Not Happy Number");
}
}
}
Output:

Enter number=31
Happy Number
What is Happy Number?
A happy number is a natural number in a given number base that eventually
reaches 1 when iterated over the perfect digital invariant function for. Those
numbers that do not end in 1 are -unhappy numbers.

What is Happy Number in Java?


A happy number is a natural number in a given number base that eventually
reaches 1 when iterated over the perfect digital invariant function for. Those
numbers that do not end in 1 are -unhappy numbers.

The least common multiple, lowest common multiple, or


smallest common multiple of two integers a and b, usually
denoted by LCM(a, b), is the smallest positive integer that is
divisible by both a and b.

import java.util.Scanner;

public class LCMProgram


{
public static void main(String[] args)
{
int a, b, gcd = 1, lcm = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a=");
a = sc.nextInt();
System.out.print("Enter b=");
b = sc.nextInt();
int min, max;
min = a;
if (min > b)
{
min = b;
max = a;
}
else
{
min = a;
max = b;
}
while (max > min)
{
int r = max % min;
if (r == 0)
{
gcd = min;
break;
}
else
{
max = min;
min = r;
}
}
lcm = (a * b) / gcd;
System.out.println("LCM:" + lcm);
}
}

Output:

Enter a=15
Enter b=18
LCM:90
Magic number is the if the sum of its digits recursively are
calculated till a single digit If the single digit is 1 then the
number is a magic number. Magic number is very similar
with Happy Number.
Example- 226 is said to be a magic number

2+2+6=10 sum of digits is 10 then again 1+0=1 now we get a single


digit number is 1.if we single digit number will now 1 them it would not a
magic number.

import java.util.Scanner;

public class MagicNumber


{
public static void main(String[] args)
{
int n, r = 1, num, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 9)
{
while (num > 0)
{
r = num % 10;
sum = sum + r;
num = num / 10;
}
num = sum;
sum = 0;
}
if (num == 1)
{
System.out.println("Magic Number");
}
else
{
System.out.println("Not Magic Number");
}
}
}
Output:

Enter number=226
Magic Number
What is Magic Number?
Magic number is the if the sum of its digits recursively are calculated till a single
digit If the single digit is 1 then the number is a magic number. Magic number is
very similar with Happy Number.

What is Magic Number in Java?


Magic number is the if the sum of its digits recursively are calculated till a single
digit If the single digit is 1 then the number is a magic number. Magic number is
very similar with Happy Number.

If a number=1234, then 1*2*3*4 ,Multiply of digit=24

import java.util.Scanner;

public class MultiplyOfDigit


{
public static void main(String[] args)
{
// TODO code application logic here
int r, n, num,
mul = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
mul = mul * r;
num = num / 10;
}
System.out.println("Multiply of digit=" + mul);
}
}

Output:

Enter number=1234
Multiply of digit=24
What isMultiply Of Digit?
If a number=1234, then 1*2*3*4 ,Multiply of digit=24

What is Multiply Of Digit in Java?


If a number=1234, then 1*2*3*4 ,Multiply of digit=24
Neon Number Program in Java
A neon number is a number where the sum of digits of
square of the number is equal to the number. For example if
the input number is 9, its square is 9*9 = 81 and sum of the
digits is 9. i.e. 9 is a neon number.

import java.util.Scanner;

public class NeonNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int n,
sqr = 1,
sum = 0, r;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
sqr = n * n;
while (sqr > 0)
{
r = sqr % 10;
sum = sum + r;
sqr = sqr / 10;
}
if (n == sum)
{
System.out.println("Neon Number");
}
else
{
System.out.println("Not Neon Number");
}
}
}

Output:

Enter number=9
Neon Number
What is Neon Number ?
A neon number is a number where the sum of digits of square of the number is
equal to the number. For example if the input number is 9, its square is 9*9 = 81
and sum of the digits is 9. i.e. 9 is a neon number.

What isNeon Number in Java?


A neon number is a number where the sum of digits of square of the number is
equal to the number. For example if the input number is 9, its square is 9*9 = 81
and sum of the digits is 9. i.e. 9 is a neon number.
In mathematics, a Niven number (or harshad number) in a
given number base is an integer that is divisible by the sum
of its digits when written in that base.

import java.util.Scanner;

public class NivenNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int n, num, r,
sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
sum = sum + r;
num = num / 10;
}
if (n % sum == 0)
{
System.out.println("Niven Number");
}
else
{
System.out.println("Not Niven Number");
}
}
}

Output:

Enter number=204
Niven Number
What is Niven Number?
In mathematics, a Niven number (or harshad number) in a given number base is
an integer that is divisible by the sum of its digits when written in that base.

What is Niven Number in Java?


In mathematics, a Niven number (or harshad number) in a given number base is
an integer that is divisible by the sum of its digits when written in that base

A palindromic number is a number that remains the same


when its digits are reversed. Like 16461, for example,

import java.util.Scanner;

public class PalindromeNumber


{
public static void main(String[] args)
{
int n, num, r,
rev = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
rev = (rev * 10) + r;
num = num / 10;
}
if (n == rev)
{
System.out.println("Palindrome Number");
}
else
{
System.out.println("Not Palindrome Number");
}
}
}

Output:

Enter number=12321
Palindrome Number
What is Palindrome Number?
A palindromic number is a number that remains the same when its digits are
reversed. Like 16461, for example,

What is Palindrome Number in Java?


A palindromic number is a number that remains the same when its digits are
reversed. Like 16461, for example,

public class Pattern1


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output:

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

public class Pattern2


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
Output:

1
22
333
4444
55555

public class Pattern3


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
Output:

1
12
123
1234
12345

public class Pattern4


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i % 2);
}
System.out.println();
}
}
}
Output:

1
00
111
0000
11111

public class Pattern5


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j % 2);
}
System.out.println();
}
}
}
Output:

1
10
101
1010
10101

public class Pattern6


{
public static void main(String[] args)
{
int k;
for (int i = 1; i <= 5; i++)
{
k = i;
for (int j = 1; j <= i; j++)
{
System.out.print((k+1) % 2);
k++;
}
System.out.println();
}
}
}
Output:

0
10
010
1010
01010

public class Pattern7


{
public static void main(String[] args)
{
int k = 1;
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(k + " ");
k++;
}
System.out.println();
}
}
}

Output:

1
23
456
7 8 9 10
11 12 13 14 15

public class Pattern8


{
public static void main(String[] args)
{
int k;
for (int i = 1; i <= 5; i++)
{
k = i;
for (int j = 1; j <= i; j++)
{
System.out.print(k + " ");
k++;
}
System.out.println();
}
}
}
Output:

1
2 3
3 45
4 567
5 6789

public class Pattern9


{
public static void main(String[] args)
{
int k;
for (int i = 1; i <= 5; i++)
{
k = i;
for (int j = 1; j <= i; j++)
{
System.out.print(k % 2);
k++;
}
System.out.println();
}
}
}
Output:

1
01
101
0101
10101

public class Pattern10


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
if (i % 2 == 0)
{
System.out.print("#");
}
else
{
System.out.print("*");
}
}
System.out.println();
}
}
}
Output:

*
##
***
####
*****

public class Pattern43


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print((char) (i + 64));
}
System.out.println();
}
}
}
Output:

A
BB
CCC
DDDD
EEEEE

public class Pattern42


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = i; j >= 1; j--)
{
System.out.print(2 * j - 1);
}
System.out.println();
}
}
}
Output:

1
31
531
7531
97531
public class Pattern21
{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 5 - i; j >= 1; j--)
{
System.out.print(" ");
}
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output:

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

public class Pattern22


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 5 - i; j >= 1; j--)
{
System.out.print(" ");
}
for (int j = 1; j <= i; j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
Output:

1
22
333
4444
55555

public class Pattern23


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 5 - i; j >= 1; j--)
{
System.out.print(" ");
}
for (int j = 1; j <= i; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
Output:

1
12
123
1234
12345

public class Pattern24


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 5 - i; j >= 1; j--)
{
System.out.print(" ");
}
for (int j = 1; j <= i; j++)
{
System.out.print(i % 2);
}
System.out.println();
}
}
}
Output:

1
00
111
0000
11111

public class Pattern25


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 5 - i; j >= 1; j--)
{
System.out.print(" ");
}
for (int j = 1; j <= i; j++)
{
System.out.print(j % 2);
}
System.out.println();
}
}
}
Output:

1
10
101
1010
10101

public class Pattern26


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 5 - i; j >= 1; j--)
{
System.out.print(" ");
}
for (int j = 1; j <= i; j++)
{
System.out.print((j+1) % 2);
}
System.out.println();
}
}
}

Output:

0
01
010
0101
01010

public class Pattern28


{
public static void main(String[] args)
{
int k;
for (int i = 1; i <= 5; i++)
{
for (int j = 5 - i; j >= 1; j--)
{
System.out.print(" ");
}
k = i;
for (int j = 1; j <= i; j++)
{
System.out.print(k);
k++;
}
System.out.println();
}
}
}
Output:

1
23
345
4567
56789

public class Pattern29


{
public static void main(String[] args)
{
int k;
for (int i = 1; i <= 5; i++)
{
for (int j = 5 - i; j >= 1; j--)
{
System.out.print(" ");
}
k = i;
for (int j = 1; j <= i; j++)
{
System.out.print(k % 2);
k++;
}
System.out.println();
}
}
}
Output:

1
01
101
0101
10101
public class Pattern30
{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 5 - i; j >= 1; j--)
{
System.out.print(" ");
}
for (int j = 1; j <= i; j++)
{
if (i % 2 == 0)
{
System.out.print("#");
}
else
{
System.out.print("*");
}
}
System.out.println();
}
}
}
Output:

*
##
***
####
*****
public class Pattern31
{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 5 - i; j >= 1; j--)
{
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output:

*
***
*****
*******
*********
public class Pattern32
{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 5 - i; j >= 1; j--)
{
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
Output:

1
222
33333
4444444
555555555

public class Pattern33


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 5 - i; j >= 1; j--)
{
System.out.print(" ");
}
for (int j = 1; j <= i; j++)
{
System.out.print(j);
}
for (int j = i - 1; j >= 1; j--)
{
System.out.print(j);
}
System.out.println();
}
}
}

Output:

1
121
12321
1234321
123454321

import java.util.Scanner;

public class NumberSeries


{
public static void main(String[] args)
{
int n, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms=");
n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
sum = sum + i;
if (n == i)
{
System.out.println(i);
}
else
{
System.out.print(i + "+");
}
}
System.out.println("Sum of series=" + sum);
}
}

Output:

Enter number of terms=5


1+2+3+4+5
Sum of series=15

public class Pattern31


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 5 - i; j >= 1; j--)
{
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output:

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

public class Pattern32


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 5 - i; j >= 1; j--)
{
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
Output:

1
222
33333
4444444
555555555

public class Pattern33


{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 5 - i; j >= 1; j--)
{
System.out.print(" ");
}
for (int j = 1; j <= i; j++)
{
System.out.print(j);
}
for (int j = i - 1; j >= 1; j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
Output:

1
121
12321
1234321
123454321
Square Number Series Program in Java
import java.util.Scanner;

public class SquareNumberSeries


{
public static void main(String[] args)
{
int n,sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms=");
n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
int t=i*i;
sum=sum+t;
if (n == i)
{
System.out.println(t);
}
else
{
System.out.print(t+"+");
}
}
System.out.println("Sum of series="+sum);
}
}

Output:

Enter number of terms=5


1+4+9+16+25
Sum of series=55

import java.util.*;

public class FirstArray


{
public static void main(String[] args)
{
int ar[] = new int[10];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{
System.out.print("Enter the number ar[" + i + "]:");
ar[i] = sc.nextInt();
}
System.out.println("The numbers are:");
for (int i = 0; i < 10; i++)
{
System.out.println("ar[" + i + "]:" + ar[i]);
}
}
}

Output:

Enter the number ar[0]:5


Enter the number ar[1]:4
Enter the number ar[2]:2
Enter the number ar[3]:11
Enter the number ar[4]:32
Enter the number ar[5]:56
Enter the number ar[6]:14
Enter the number ar[7]:32
Enter the number ar[8]:14
Enter the number ar[9]:58
The numbers are:
ar[0]:5
ar[1]:4
ar[2]:2
ar[3]:11
ar[4]:32
ar[5]:56
ar[6]:14
ar[7]:32
ar[8]:14
ar[9]:58
Reverse Array Program in Java
import java.util.*;

public class Reverse Array


{
public static void main(String[] args)
{
int ar[] = new int[10];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{
System.out.print("Enter the number ar[" + i + "]:");
ar[i] = sc.nextInt();
}
System.out.println("Reverse Array are:");
for (int i = 9; i > 0; i--)
{
System.out.println("ar[" + i + "]:" + ar[i]);
}
}
}

Output:
Enter the number ar[0]:1
Enter the number ar[1]:5
Enter the number ar[2]:6
Enter the number ar[3]:4
Enter the number ar[4]:10
Enter the number ar[5]:25
Enter the number ar[6]:36
Enter the number ar[7]:41
Enter the number ar[8]:10
Enter the number ar[9]:78
Reverse Array are:
ar[9]:78
ar[8]:10
ar[7]:41
ar[6]:36
ar[5]:25
ar[4]:10
ar[3]:4
ar[2]:6
ar[1]:5
Smallest in Array Program in Java
import java.util.*;

public class SmallestInArray


{
public static void main(String[] args)
{
int ar[] = new int[10];
int s;
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{
System.out.print("Enter the number ar[" + i + "]:");
ar[i] = sc.nextInt();
}
s=ar[0];

for (int i = 9; i > 0; i--)


{
if(ar[i]

Output:

Enter the number ar[0]:1


Enter the number ar[1]:5
Enter the number ar[2]:4
Enter the number ar[3]:6
Enter the number ar[4]:7
Enter the number ar[5]:45
Enter the number ar[6]:2
Enter the number ar[7]:77
Enter the number ar[8]:48
Enter the number ar[9]:63
Smallest=1
Enter the number ar[0]:4
Enter the number ar[1]:6
Enter the number ar[2]:15
Enter the number ar[3]:24
Enter the number ar[4]:57
Enter the number ar[5]:86
Enter the number ar[6]:45
Enter the number ar[7]:31
Enter the number ar[8]:32
Enter the number ar[9]:18
Largest=86

Largest in Array Program in Java


import java.util.*;

public class LargestInArray


{
public static void main(String[] args)
{
int ar[] = new int[10];
int l;
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{
System.out.print("Enter the number ar[" + i + "]:");
ar[i] = sc.nextInt();
}
l = ar[0];
for (int i = 9; i > 0; i--)
{
if (ar[i] > l)
{
l = ar[i];
}
}
System.out.println("Largest=" + l);
}
}

Output:

Enter the number ar[0]:4


Enter the number ar[1]:6
Enter the number ar[2]:15
Enter the number ar[3]:24
Enter the number ar[4]:57
Enter the number ar[5]:86
Enter the number ar[6]:45
Enter the number ar[7]:31
Enter the number ar[8]:32
Enter the number ar[9]:18
Largest=86
Linear Search Program in Java
import java.util.*;

public class LinearSearch


{
public static void main(String[] args)
{
int n,
pos = -1;
boolean flag = false;
int ar[] = new int[10];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{
System.out.print("Enter the number ar[" + i + "]:");
ar[i] = sc.nextInt();
}
System.out.print("Enter number to be search:");
n = sc.nextInt();
for (int i = 0; i < 9; i++)
{
if (ar[i] == n)
{
pos = i + 1;
flag = true;
break;
}
}
if (flag)
{
System.out.println("Position=" + pos);
}
else
{
System.out.println("Number not found.");
}
}
}

Output:

Enter the number ar[0]:15


Enter the number ar[1]:25
Enter the number ar[2]:45
Enter the number ar[3]:36
Enter the number ar[4]:12
Enter the number ar[5]:32
Enter the number ar[6]:47
Enter the number ar[7]:65
Enter the number ar[8]:69
Enter the number ar[9]:58
Enter number to be search:32
Position=6
pos = mid + 1;
break;
}
else if (ar[mid] > n)
{
ub = mid - 1;
}
else
{
lb = mid + 1;
}
}
if (pos == -1)
{
System.out.println("Number not found.");
}
else
{
System.out.println("Position=" + pos);
}
}
}

Output:

Enter the number ar[0]:4


Enter the number ar[1]:8
Enter the number ar[2]:9
Enter the number ar[3]:12
Enter the number ar[4]:15
Enter the number ar[5]:24
Enter the number ar[6]:26
Enter the number ar[7]:28
Enter the number ar[8]:35
Enter the number ar[9]:45
Enter number to be search:15
Position=5

Bubble Sort Program in Java


Bubble Sort Program in Java to sort numbers of an array in
ascending or descending order.
import java.util.*;

public class BubbleSort


{
public static void main(String[] args)
{
int temp;
int ar[] = new int[10];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{
System.out.print("Enter the number ar[" + i + "]:");
ar[i] = sc.nextInt();
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 9 - i; j++)
{
if (ar[j] > ar[j + 1])
{
temp = ar[j];
ar[j] = ar[j + 1];
ar[j + 1] = temp;
}
}
}
System.out.println("Sorted order:");
for (int i = 0; i < 10; i++)
{
System.out.println(ar[i]);
}
}
}

Output:

Enter the number ar[0]:12


Enter the number ar[1]:54
Enter the number ar[2]:21
Enter the number ar[3]:36
Enter the number ar[4]:25
Enter the number ar[5]:14
Enter the number ar[6]:47
Enter the number ar[7]:58
Enter the number ar[8]:69
Enter the number ar[9]:85
Sorted order:
12
14
21
25
36
47
54
58
69
85
Selection Sort Program in Java
Selection sort is an in-place comparison sorting algorithm. It
has an O(n²) time complexity, which makes it inefficient on
large lists, and generally performs worse than the similar
insertion sort.
import java.util.*;

public class SelectionSort


{
public static void main(String[] args)
{
int ar[] = new int[10];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{
System.out.print("Enter the number ar[" + i + "]:");
ar[i] = sc.nextInt();
}
System.out.println("The numbers are:");
int n = ar.length;
for (int i = 0; i < n-1; i++)
{
int min = i;
for (int j = i+1; j < n; j++)
{
if (ar[j] < ar[min])
{
min = j;
}
}
int temp = ar[min];
ar[min] = ar[i];
ar[i] = temp;
}

System.out.println("Sorted order:");
for (int i = 0; i < 10; i++)
{
System.out.println(ar[i]);
}
}
}

Output:

Enter the number ar[0]:12


Enter the number ar[1]:54
Enter the number ar[2]:21
Enter the number ar[3]:36
Enter the number ar[4]:25
Enter the number ar[5]:14
Enter the number ar[6]:47
Enter the number ar[7]:58
Enter the number ar[8]:69
Enter the number ar[9]:85
Sorted order:
12
14
21
25
36
47
54
58
69
85
Merge Array Program in Java
import java.util.Scanner;

public class MergeArray


{
public static void main(String[] args)
{
int ar1[] = new int[10];
int ar2[] = new int[10];
Scanner sc = new Scanner(System.in);
System.out.println("Enter first array:");
for (int i = 0; i < 10; i++)
{
System.out.print("Enter the number ar1[" + i + "]:");
ar1[i] = sc.nextInt();
}
System.out.println("Enter second array:");
for (int i = 0; i < 10; i++)
{
System.out.print("Enter the number ar2[" + i + "]:");
ar2[i] = sc.nextInt();
}

int totalLength = ar1.length + ar2.length;


int idx=0;
int ar3[] = new int[totalLength];
for (int i = 0; i < ar1.length; i++)
{
ar3[i] = ar1[i];
idx++;
}
for (int j = 0; j < ar2.length; j++)
{
ar3[idx++] = ar2[j];
}

System.out.println("Merged array:");
for (int i = 0; i < totalLength; i++)
{
System.out.println("ar3[" + i + "]:" + ar3[i]);
}
}
}

Output:

Enter first array:


Enter the number ar1[0]:1
Enter the number ar1[1]:2
Enter the number ar1[2]:3
Enter the number ar1[3]:4
Enter the number ar1[4]:5
Enter the number ar1[5]:6
Enter the number ar1[6]:7
Enter the number ar1[7]:8
Enter the number ar1[8]:9
Enter the number ar1[9]:10
Enter second array:
Enter the number ar2[0]:11
Enter the number ar2[1]:22
Enter the number ar2[2]:33
Enter the number ar2[3]:44
Enter the number ar2[4]:55
Enter the number ar2[5]:66
Enter the number ar2[6]:77
Enter the number ar2[7]:88
Enter the number ar2[8]:99
Enter the number ar2[9]:110
Merged array:
ar3[0]:1
ar3[1]:2
ar3[2]:3
ar3[3]:4
ar3[4]:5
ar3[5]:6
ar3[6]:7
ar3[7]:8
ar3[8]:9
ar3[9]:10
ar3[10]:11
ar3[11]:22
ar3[12]:33
ar3[13]:44
ar3[14]:55
ar3[15]:66
ar3[16]:77
ar3[17]:88
ar3[18]:99
ar3[19]:110
Matrix Program in Java
import java.util.*;

public class Matrix


{
public static void main(String[] args)
{
int ar[][] = new int[3][3];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
System.out.print("Enter the number ar[" + i + "][" + j + "]:");
ar[i][j] = sc.nextInt();
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
System.out.print(ar[i][j]+" ");
}
System.out.println();
}

}
}

Output:

run:
Enter the number ar[0][0]:4
Enter the number ar[0][1]:5
Enter the number ar[0][2]:6
Enter the number ar[1][0]:3
Enter the number ar[1][1]:2
Enter the number ar[1][2]:1
Enter the number ar[2][0]:7
Enter the number ar[2][1]:8
Enter the number ar[2][2]:9
456
321
789
Sentence Reverse Program in Java
Enter a sentence or string and print its reverse string in
java.
import java.util.Scanner;

/**
*
* @author AFAQ
*/
public class SentenceReverse
{
public static void main(String[] args)
{
String st,revSt="";
int l;
Scanner sc=new Scanner(System.in);

System.out.print("Enter String:");
st=sc.nextLine();
l=st.length();
for (int i = l-1; i >= 0; i--)
{
char ch;
ch=st.charAt(i);
revSt=revSt+ch;
}
System.out.println("Reverce String:"+revSt);
}
}

Output:

Enter String:this is a boy


Reverce String:yob a si siht
Words in Sentence Program in Java
Enter a sentence or string and print its words of string in
java.
import java.util.Scanner;
/**
*
* @author AFAQ
*/
public class WordsInSentence
{
public static void main(String[] args)
{
String st,wd="";
int count=0,l;
Scanner sc=new Scanner(System.in);

System.out.print("Enter String: ");


st=sc.nextLine();

st=st+" ";

l=st.length();
for (int i = 0; i < l; i++)
{
if(st.charAt(i)==' ')
{
System.out.println(wd);
wd="";
}
else
{
char ch;
ch=st.charAt(i);
wd=wd+ch;
}
}
}
}

Output:

Enter String: this is a boy


this
is
a
boy
import java.util.Scanner;

public class ShortNameFormat


{
public static void main(String[] args)
{
String st,wd="",shortSt="";
int l;
Scanner sc=new Scanner(System.in);

System.out.print("Enter String: ");


st=sc.nextLine();

st=st+" ";

l=st.length();
for (int i = 0; i < l; i++)
{
if(st.charAt(i)==' ')
{
if(i==l-1)
{
shortSt=shortSt+wd;
}
else
{
shortSt=shortSt+wd.charAt(0)+". ";
}
wd="";
}
else
{
char ch;
ch=st.charAt(i);
wd=wd+ch;
}
}
System.out.println(shortSt);
}
}

Output:

Enter String: Afaq Ahmad Khan


A. A. Khan
Program 1- Define a class called Library with the following description:
Instance variables/data members:
Int acc_num – stores the accession number of the book
String title – stores the title of the book stores the name of the author
Member Methods:
(i) void input() – To input and store the accession number, title and author.
(ii)void compute – To accept the number of days late, calculate and display and fine
charged at the rate of Rs.2 per day.
(iii) void display() To display the details in the following format:
Accession Number Title Author
Write a main method to create an object of the class and call the above member
methods.
Solution -

public class Library {

int acc_num;
String title;
String author;

public void input() throws IOException {


BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter accession number: ");
acc_num = Integer.parseInt(br.readLine());
System.out.print("Enter title: ");
title = br.readLine();
System.out.print("Enter author: ");
author = br.readLine();
}

public void compute() throws IOException {


BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter number of days late: ");
int daysLate = Integer.parseInt(br.readLine());;
int fine = 2 * daysLate;
System.out.println("Fine is Rs " + fine);
}

public void display() {


System.out.println("Accession Number\tTitle\tAuthor");
System.out.println(acc_num + "\t" + title + "\t" + author);
}

public static void main(String[] args) throws IOException {


Library library = new Library();
library.input();
library.compute();
library.display();
}
}

Program 2- Given below is a hypothetical table showing rates of Income Tax for
male citizens below the age of 65 years:

Taxable Income (TI) in Income Tax in

Does not exceed 1,60,000 Nil


Is greater than 1,60,000 and less than or equal to 5,00,000 ( TI – 1,60,000 ) * 10%

Is greater than 5,00,000 and less than or equal to 8,00,000 [ (TI - 5,00,000 ) *20% ] + 34,000

Is greater than 8,00,000 [ (TI - 8,00,000 ) *30% ] + 94,000

Write a program to input the age, gender (male or female) and Taxable Income of a
person.If the age is more than 65 years or the gender is female, display “wrong
category*.
If the age is less than or equal to 65 years and the gender is male, compute and
display the Income Tax payable as per the table given above.

Solution.
import java.util.Scanner;

public class IncomeTax {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter age: ");
int age = scanner.nextInt();
System.out.print("Enter gender: ");
String gender = scanner.next();
System.out.print("Enter taxable income: ");
int income = scanner.nextInt();
if (age > 65 || gender.equals("female")) {
System.out.println("Wrong category");
} else {
double tax;
if (income <= 160000) { tax =
0; } else if (income > 160000 && income <= 500000)
{ tax = (income - 160000) * 10 / 100;
} else if (income >= 500000 && income <= 800000) {
tax = (income - 500000) * 20 / 100 + 34000;
} else {
tax = (income - 800000) * 30 / 100 + 94000;
}
System.out.println("Income tax is " + tax);
}
}
}
Program 3- Write a program to accept a string. Convert the string to uppercase.
Count and output the number of double letter sequences that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE
Sample Output: 4

Solution.
import java.util.Scanner;

public class StringOperations {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a String: ");
String input = scanner.nextLine();
input = input.toUpperCase();
int count = 0;
for (int i = 1; i < input.length(); i++) {
if (input.charAt(i) == input.charAt(i - 1)) {
count++;
}
}
System.out.println(count);
}

Program4- W Design a class to overload a function polygon() as follows:


(i) void polygon(int n, char ch) : with one integer argument and one character
type argument that draws a filled square of side n using the character stored
in ch.
(ii) void polygon(int x, int y) : with two integer arguments that draws a
filled rectangle of length x and breadth y, using the symbol ‘@’
(iii)void polygon( ) : with no argument that draws a filled triangle shown
below.

Example:
(i) Input value of n=2, ch=’O’
Output:
OO
OO
(ii) Input value of x=2, y=5
Output:
@@@@@
@@@@@
(iii) Output:
*
**
***

Solution.

public class Overloading {

public void polygon(int n, char ch) {

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

for (int j = 1; j <= n; j++) {

System.out.print(ch);

System.out.println();

public void polygon(int x, int y) {

for (int i = 1; i <= x; i++) {


for (int j = 1; j <= y; j++) {

System.out.print("@");

System.out.println();

public void polygon() {

for (int i = 1; i <= 3; i++) {

for (int j = 1; j <= i; j++) {

System.out.print("*");

System.out.println();

Program 5- Using the switch statement, writw a menu driven program to:
(i) Generate and display the first 10 terms of the Fibonacci
series 0,1,1,2,3,5….The first two Fibonacci numbers are 0 and 1, and each
subsequent number is the sum of the previous two.
(ii)Find the sum of the digits of an integer that is input.
Sample Input: 15390
Sample Output: Sum of the digits=18
For an incorrect choice, an appropriate error message should be displayed

Solution.

import java.util.Scanner;

public class Menu {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Menu");

System.out.println("1. Fibonacci Sequence");

System.out.println("2. Sum of Digits");

System.out.print("Enter choice: ");

int choice = scanner.nextInt();

switch (choice) {

case 1:

int a = 0;

int b = 1;

System.out.print("0 1 ");
for (int i = 3; i <= 10; i++)
{ int c = a + b;
System.out.print(c + " "); a =
b; b = c; }
break; case 2:
System.out.print("Enter a number: "); int num =
scanner.nextInt(); int sum = 0;
while (num > 0) {

int rem = num % 10;

sum = sum + rem;

num = num / 10;

System.out.println("Sum of digits is " + sum);

break;

default:

System.out.println("Invalid Choice");

Program6- Write a program to accept the names of 10 cities in a single dimension


string array and their STD (Subscribers Trunk Dialing) codes in another single
dimension integer array. Search for a name of
a city input by the user in the list. If found, display “Search Successful” and print the
name of the city along with its STD code, or else display the message “Search
Unsuccessful, No such city in the list’.
Solution.

import java.util.Scanner;

import java.util.Scanner;

public class Cities {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

String[] cities = new String[10];

int[] std = new int[10];

for (int i = 0; i < 10; i++) {

System.out.print("Enter city: ");

cities[i] = scanner.next();

System.out.print("Enter std code: ");

std[i] = scanner.nextInt();

System.out.print("Enter city name to search: ");

String target = scanner.next();


boolean searchSuccessful = false;

for (int i = 0; i < 10; i++) {

if (cities[i].equals(target)) {

System.out.println("Search successful");

System.out.println("City : " + cities[i]);

System.out.println("STD code : " + std[i]);

searchSuccessful = true;

break;

if (!searchSuccessful) {

System.out.println("Search Unsuccessful, No such city


in the list");

You might also like