Javapractice Prog for Lab
Javapractice Prog for Lab
{
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);
}
}
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
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
Notes
About
Contact
Privacy and Policy
Terms & Conditions
Cancellation and Refunds Policy
Contact
Call: +91-9670248484
WhatsApp: +91-9670248484
}
}
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;
Output:
import java.util.Scanner;
Output:
Output:
import java.util.Scanner;
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();
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();
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);
if (number % 2 == 0) {
System.out.println("Number is even.");
} else {
System.out.println("Number is odd.");
}
}
}
Output:
import java.util.Scanner;
Output:
Enter month=9
September
import java.util.Scanner;
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.
1+2+4+5+10+11+20+22+44+55+110=284
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:
(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
import java.util.Scanner;
}
}
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
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.
import java.util.Scanner;
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.
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.
11 + 32 + 53 = 1 + 9 + 125 = 135
import java.util.Scanner;
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.
import java.util.Scanner;
}
}
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
import java.util.Scanner;
Output:
import java.util.Scanner;
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.
import java.util.Scanner;
import java.util.Scanner;
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.
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.
import java.util.Scanner;
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
import java.util.Scanner;
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.
import java.util.Scanner;
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
import java.util.Scanner;
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.
import java.util.Scanner;
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.
import java.util.Scanner;
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,
*
**
***
****
*****
1
22
333
4444
55555
1
12
123
1234
12345
1
00
111
0000
11111
1
10
101
1010
10101
0
10
010
1010
01010
Output:
1
23
456
7 8 9 10
11 12 13 14 15
1
2 3
3 45
4 567
5 6789
1
01
101
0101
10101
*
##
***
####
*****
A
BB
CCC
DDDD
EEEEE
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:
*
**
***
****
*****
1
22
333
4444
55555
1
12
123
1234
12345
1
00
111
0000
11111
1
10
101
1010
10101
Output:
0
01
010
0101
01010
1
23
345
4567
56789
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
Output:
1
121
12321
1234321
123454321
import java.util.Scanner;
Output:
*
***
*****
*******
*********
1
222
33333
4444444
555555555
1
121
12321
1234321
123454321
Square Number Series Program in Java
import java.util.Scanner;
Output:
import java.util.*;
Output:
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.*;
Output:
Output:
Output:
Output:
Output:
System.out.println("Sorted order:");
for (int i = 0; i < 10; i++)
{
System.out.println(ar[i]);
}
}
}
Output:
System.out.println("Merged array:");
for (int i = 0; i < totalLength; i++)
{
System.out.println("ar3[" + i + "]:" + ar3[i]);
}
}
}
Output:
}
}
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:
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:
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:
int acc_num;
String title;
String author;
Program 2- Given below is a hypothetical table showing rates of Income Tax for
male citizens below the age of 65 years:
Is greater than 5,00,000 and less than or equal to 8,00,000 [ (TI - 5,00,000 ) *20% ] + 34,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;
Solution.
import java.util.Scanner;
Example:
(i) Input value of n=2, ch=’O’
Output:
OO
OO
(ii) Input value of x=2, y=5
Output:
@@@@@
@@@@@
(iii) Output:
*
**
***
Solution.
System.out.print(ch);
System.out.println();
System.out.print("@");
System.out.println();
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;
System.out.println("Menu");
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) {
break;
default:
System.out.println("Invalid Choice");
import java.util.Scanner;
import java.util.Scanner;
cities[i] = scanner.next();
std[i] = scanner.nextInt();
if (cities[i].equals(target)) {
System.out.println("Search successful");
searchSuccessful = true;
break;
if (!searchSuccessful) {