Output
Program to swap two numbers
Before swapping:
a is 10
b is 20
After swapping:
a is 20
b is 10
Variable name Data type Description
a int To store value of first
number
b int To store value of second
number
temp int Temporary variable
Sourcecode
// Program to swap two numbers
public class Swap
public static void main()
[Link]("\t Program to swap two numbers");
int a = 10,b = 20,temp;
[Link]("Before swapping:");
[Link]("a is " + a);
[Link]("b is " + b);
temp = a;
a = b;
b = temp;
[Link]("After swapping:");
[Link]("a is " + a);
[Link]("b is " + b);
}
Output
Celsius: 25.0
Fahrenheit: 77.0
Variable name Data type Description
celsius double To store value of celsius
fahrenheit double
Sourcecode
public class C_F
public static void main()
double celsius = 25,fahrenheit;
fahrenheit = (celsius * 9 / 5) + 32;
[Link]("Celsius: " + celsius);
[Link]("Fahrenheit: " + fahrenheit);
}
Output
Program to swap two numbers without using a third variable
Enter first number
Enter second number
After swapping first number 9
After swapping second number 2
Variable name Data type Description
n1 int To store the first number
n2 int To store the second
number
Sourcecode
// Program to swap two numbers without using a third variable
import [Link];
class Swap_no_third_variable
public static void main()
Scanner in=new Scanner([Link]);
[Link]("\t Program to swap two numbers without using a third
variable");
int n1,n2;
[Link]("Enter first number");
n1=[Link]();
[Link]("Enter second number");
n2=[Link]();
n1=n1+n2;
n2=n1-n2;
n1=n1-n2;
[Link]("After swapping first number "+n1);
[Link]("After swapping second number "+n2);
}
output
Program to calculate the salary of an employee.
Enter the basic pay :
10000
Dearness Allowance : 2500.0
House rent Allowance : 1500.0
Provident fund : 830.0
Net pay : 14000.0
Gross pay : 13170.0
Variable name Data type Description
da double To store the calculated
value of Dearness
Allowance
hra double To store the calculated
value of House rent
Allowance
pf double To store the calculated
value of Provident fund
np double To store the calculated
value of Net pay
gp double To store the calculated
value of Gross pay
bp double To store the value of
Basic pay
sourcecode
// Program to calculate the salary of an employee.
import [Link];
class Salary_Calculation
public static void main()
[Link]("\t Program to calculate the salary of an employee.");
Scanner in = new Scanner([Link]);
double da,hra,pf,np,gp,bp;
[Link]("Enter the basic pay :");
bp = [Link]();
da = bp*(25/100.0);
hra = bp*(15/100.0);
pf = bp*(8.3/100.0);
np = bp+da+hra;
gp=np-pf;
[Link]("Dearness Allowance : "+da);
[Link]("House rent Allowance : "+hra);
[Link]("Provident fund : "+pf);
[Link]("Net pay : "+np);
[Link]("Gross pay : "+gp);
}
Output
Program to calculate the electricity bill based on the unit consumed.
Enter the name of consumer
bhuvan
Enter the consumer's number
5930562768
Enter the number of unit consumed
500
name of consumer: bhuvan
consumer's number: 5930562768
number of unit consumed: 500
the bill price is: 3350.0
Variable name Data type Description
unit int To store the units
cno long To store the customers
number
amt double To store the calculated
value of Amount
Sourcecode
// Program to calculate the electricity bill based on the unit consumed.
import [Link];
class EBill
public static void main()
Scanner in=new Scanner([Link]);
[Link]("\t Program to calculate the electricity bill based on the unit
consumed.");
String name;
int unit;
long cno;
double amt;
[Link]("Enter the name of consumer");
name=[Link]();
[Link]("Enter the consumer's number");
cno=[Link]();
[Link]("Enter the number of unit consumed");
unit=[Link]();
if (unit<=100)
amt=unit*5.50;
else if (unit>100 && unit<=300)
amt=100*5.50+(unit-100)*6.50;
else if (unit>300 && unit<=600)
amt=100*5.50+200*6.50+(unit-300)*7.50;
else
amt=100*5.50+200*6.50+300*7.50+(unit-600)*7.50;
[Link]("name of consumer: "+name);
[Link]("consumer's number: "+cno);
[Link]("number of unit consumed: "+unit);
[Link]("the bill price is: "+amt);
}
Output
Program to calculate the fibernet charges
Enter the name of the customer
bhuvan
Enter the usage
100
the name of the customer: bhuvan
the usage:100
the bill to be paid:2150
Variable name Data type Description
usage int To store the usage
name String To store the customers
name
charge int To store the calculated
charge
Sourcecode
// Program to calculate the fibernet charges
import [Link];
class Fibernet
{
static public void main()
{
Scanner in = new Scanner([Link]);
[Link]("\t Program to calculate the fibernet charges");
int usage,charge;
String name;
[Link]("Enter the name of the customer");
name=[Link]();
[Link]("Enter the usage");
usage=[Link]();
if (usage<=40)
charge=usage*30;
else if (usage>40 && usage<=75)
charge=40*30+(usage-40)*20;
else if (usage>75 && usage<=125)
charge=40*30+35*20+(usage-75)*10;
else
charge=40*30+35*20+50*10+(usage-125)*5;
[Link]("the name of the customer:"+name);
[Link]("the usage:"+usage);
[Link]("the bill to be paid:"+charge);
}
}
Output
Program to calculate the term deposit amount as per the years and rate of
intrest
Enter the amount of deposit
10000
Enter the time period
5
Enter the age
60
Deposited amount: 10000.0
Age: 60
Sum assured: 11100.0
Variable name Data type Description
p double To store the value of
principle
sum double To store the calculated
sum
age int To store the age of
customer
t int To store the time period
Sourcecode
// Program to calculate the term deposit amount as per the years and rate of intrest
import [Link];
class Term_deposit
{
public static void main()
{
Scanner in=new Scanner([Link]);
[Link]("\t Program to calculate the term deposit amount as per the
years and rate of intrest");
double p,sum;
int age,t;
[Link]("Enter the ammount of deposit");
p=[Link]();
[Link]("Enter the time period");
t=[Link]();
[Link]("Enter the age");
age=[Link]();
if (age<60)
{
if (t==1)
sum=p+p*7.5/100;
else if (t==2)
sum=p+p*8.5/100;
else if (t==3)
sum=p+p*9.5/100;
else
sum=p+p*10.0/100;
else
{
if (t==1)
sum=p+p*8.0/100;
else if (t==2)
sum=p+p*9.0/100;
else if (t==3)
sum=p+p*10.0/100;
else
sum=p+p*11.0/100;
}
[Link]("Deposited amount: "+p);
[Link]("Age: "+age);
[Link]("Sum assured: "+sum);
}
}
Output
Enter the name of customer
bhuvan
Enter the number of days
20
The name of customer: bhuvan
The number of days: 20
Ammount to be paid: 7000.0
Variable name Data type Description
Name String To store the name of the
customer
Days int To store the number of
days
amount double To store the calculated
value of ammount
Sourcecode
import [Link];
class E_service
{
public static void main()
{
Scanner in=new Scanner([Link]);
String name ;
int days;
double ammount;
[Link]("Enter the name of customer");
name=[Link]();
[Link]("Enter the number of days");
days=[Link]();
if (days>=1 && days<=5)
ammount=200*days;
else if (days>5 && days<=10)
ammount=250*days;
else if (days>10 && days<=15)
ammount=300*days;
else
ammount=350*days;
[Link]("The name of customer: "+name);
[Link]("The number of days: "+days);
[Link]("Ammount to be paid: "+ammount);
}
}
Output
program to calculate the depreciated value of a car based on the number of
years used
Enter the original price
100000
Enter the number of years
4
The original value of car: 100000.0
The number of years used: 4
The depreciated value of a car: 50000.0
The final price: 50000.0
Variable name Data type Description
Org double To store the original value
Dep double To store the depreciated
value
F double To store the calculated
final value
Y int To store the number of
years
Sourcecode
// program to calculate the depriciated value of a car based on the number of years
used
import [Link];
class car_Depriciation
{
public static void main()
{
Scanner in=new Scanner([Link]);
[Link]("\t program to calculate the depriciated value of a car based
on the number of years used");
double org,dep,fin;
int y;
[Link]("Enter the original price");
org=[Link]();
[Link]("Enter the number of years");
y=[Link]();
if (y==1)
dep=org*10.0/100;
else if (y==2)
dep=org*20.0/100;
else if (y==3)
dep=org*30.0/100;
else if (y==4)
dep=org*50.0/100;
else
dep=org*60.0/100;
fin=org-dep;
[Link]("The original value of car: "+org);
[Link]("The number of years used: "+y);
[Link]("The depriciated value of a car: "+dep);
[Link]("The final price: "+fin);
}
}
Output
Program to perform arithmatic operation on two given numbers
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your choice
1
Enter the number 1
1
Enter the number 2
1
Addition:2
Variable name Data type Description
n1 Int To store the value first
number
n2 Int To store the value second
number
Ans Int To store the calculated
value of the answer
Ch Int choice
Sourcecode
// Program to perform arithmatic operation on two given numbers
import [Link];
public class Arithimatic_operation
{
static public void main()
{
Scanner in = new Scanner([Link]);
[Link]("\t Program to perform arithmatic operation on two given
numbers ");
int n1,n2,ans,ch;
[Link]("[Link]");
[Link]("[Link]");
[Link]("[Link]");
[Link]("[Link]");
[Link]("[Link]");
[Link]("Enter your choice");
ch=[Link]();
[Link]("Enter the number 1");
n1=[Link]();
[Link]("Enter the number 2");
n2=[Link]();
switch(ch)
{
case 1:
ans=n1+n2;
[Link]("Sum: "+ans);
Break;
case 2:
ans=n1-n2;
[Link]("Difference: "+ans);
break;
case 3:
ans=n1*n2;
[Link]("multiplication: "+ans);
break;
case 4:
ans=n1/n2;
[Link]("Quotient: "+ans);
break;
case 5:
ans=n1%n2;
[Link]("Remainder: "+ans);
break;
default :
[Link]("Invalid choice");
}
}
}
Output
Program to check if the given number is Armstrong or not
Enter the number
153
153 is Armstrong Number
Variable Data type Description
n Int To store the number
temp Int Temporary Variable
Sourcecode
// Program to check if the given number is Armstrong or not
import [Link];
class Armstrong_Number
{
static public void main()
{
Scanner in = new Scanner([Link]);
[Link]("\t Program to check if the given number is Armstrong or
not");
int n,sum=0,temp;
[Link]("Enter the number");
n=[Link]();
temp=n;
while (n!=0)
{
int r=n%10;
sum+=r*r*r;
n=n/10;
}
if (sum==temp)
[Link](temp+" is Aromstromg Number");
else
[Link](temp+" is not Aromstromg Number");
}
}
Output
Program to check if the given number is Harshad or not
Enter the number
81
81 is Harshad Number
Variable name Data type Description
temp int Temporary variable
sum int Accumulator variable
n int To store the value of a
number
Sourcecode
// Program to check if the given number is Harshad or not
import [Link];
class Harshad_Number
{
static public void main()
{
Scanner in = new Scanner([Link]);
[Link]("\t Program to check if the given number is Harshad or not");
int n,sum=0,temp;
[Link]("Enter the number");
n=[Link]();
temp=n;
while (n!=0)
{
int r=n%10;
sum+=r;
n=n/10;
}
if (temp%sum==0)
[Link](temp+" is Harshad Number");
else
[Link](temp+" is not Harshad Number");
}
}
Output
program to check if the number is duck number or not
Enter number
100
100 is duck number
Variable Description
Variable Data type Description
n int To store the number
flag int To control the flow of program
Sourcecode
// program to check if the number is duck number or not
import [Link];
class Duck_Number
{
public static void main ()
{
Scanner in=new Scanner([Link]);
[Link]("\t program to check if the number is duck number or not");
int n,flag=0;
[Link]("Enter number");
n=[Link]();
while(n!=10)
{
int r=n%10;
if(r==0)
{
flag=1;
break;
}
n=n/10;
}
if(flag==1)
[Link](n+" is duck number");
else
[Link](n+" is not duck number");
}
}
Output
Program to check if the given number is special two digit number or not
Enter the number
97
97 is not a special two digit number
Variable name Data type Description
temp int Temporary variable
sum int Accumulator variable
n int To store the value of a
number
Sourcecode
// Program to check if the given number is special two digit number or not
import [Link];
class Special_twodigit
{
static public void main()
{
Scanner in = new Scanner([Link]);
[Link]("\t Program to check if the given number is special two digit
number or not");
int n,sum=0,prod=1,temp;
[Link]("Enter the number");
n=[Link]();
temp=n;
while(n!=0)
{
int r=n%10;
sum+=r;
prod*=r;
n=n/10;
}
if((sum+prod)==temp)
[Link](temp+" is a special two digit number");
else
[Link](temp+" is not a special two digit number");
}
}
Output
Program to find fiboniacci
Enter the number of terms
9
0 1 1 2 3 5 8 13 21
Variable datatype Description
n int to store the number of terms
t1 int to store the first number
t2 int to store the second number
t3 int to store the third number
Sourcecode
// Program to find fiboniacci
import [Link];
class Fibonacci
{
static public void main()
{
Scanner in = new Scanner([Link]);
[Link]("\t Program to find fiboniacci");
int n,t1=0,t2=1,t3;
[Link]("Enter the number of terms");
n=[Link]();
[Link](t1+" "+t2);
int i=3;
while(i<=n)
{
t3=t1+t2;
[Link](" "+t3);
t1=t2;
t2=t3;
i++;
}
}
}
Output
program to print series
1 11 111 1111 11111
1 12 1234 12345
1 11 111 1111
2 23 234 2345
Variable Description
Variable Data type Description
sum1 int Accumulator Variable
sum2 int Accumulator Variable
Sourcecode
// program to print series 1 11 111 1111 11111 and 1 12 1234 12345
import [Link];
class Series_pattern
{
public static void main ()
{
Scanner in=new Scanner([Link]);
[Link]("\t program to print series 1 11 111 1111 11111 and 1 12 1234
12345");
int sum1=0,sum2=0,i,j;
for(i=2;i<=5;i++)
{
sum1=sum1*10+1;
[Link](sum1+" ");
}
[Link]();
for(j=2;j<=5;j++)
{
sum2=sum2*10+j;
[Link](sum2+" ");
}
}
}
Output
program to print S1 and S2
enter a
enter n
s1= 16.40226034285632
S2= 13.589285714285714
Variable datatype Description
a int to store the value of a
n int to store the value of
number
s1 double accumulator
Sourcecode
// program to print S1 and S2
import [Link];
class S1_S2
{
public static void main ()
{
Scanner in=new Scanner([Link]);
[Link]("\t // program to print S1 and S2");
double s1=0.0,s2=0.0;
int a,n;
[Link]("enter a");
a=[Link]();
[Link]("enter n");
n=[Link]();
for(double i=1;i<=19;i++)
{
s1+=i/(i+1);
}
[Link]("s1="+s1);
for(double j=1;j<=n;j++)
{
s2+=a/j;
}
[Link]("S2="+s2);
}
}
Output
12
123
1234
12345
12345
1234
123
12
21
321
4321
54321
54321
5432
543
54
5
1
23
456
7 8 9 10
10 9 8 7
654
32
Variable datatype description
i int Accumulator variable
j int Accumulator variable
Sourcecode
// program to print series
class Nested_Loops
public static void main()
for(int i=1;i<=5;i++)
for(int j=1;j<=i;j++)
[Link](j+" ");
[Link]();
[Link]();
for(int i=5;i>=1;i--)
for(int j=1;j<=i;j++)
[Link](j+" ");
[Link]();
[Link]();
for(int i=1;i<=5;i++)
{
for(int j=i;j>=1;j--)
[Link](j + " ");
[Link]();
[Link]();
for(int i=1;i<=5;i++)
for(int j=5;j>=i;j--)
[Link](j + " ");
[Link]();
[Link]();
int k=1;
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i;j++)
{
[Link](k++ + " ");
}
[Link]();
}
[Link]();
int m=10;
for(int i=4;i>=1;i--)
for(int j=1;j<=i;j++)
[Link](m-- +" ");
[Link]();
}
Output
Program to print the Alphabet Pattern
AB
ABC
ABCD
ABCDE
BB
CCC
DDDD
EEEEE
BC
DEF
GHIJ
ABCDE
ABCD
ABC
AB
A
ABCDE
BCDE
CDE
DE
ZY
ZYX
ZYXW
Variable Data type description
i int Accumulator variable
j int Accumulator variable
k char Accumulator variable
m char Accumulator variable
Sourcecode
// Program to print the Alphabet Pattern
class Alphabet_Pattern
public static void main()
[Link]("\t Program to print the Alphabet Pattern");
for(int i=1;i<=5;i++)
char k='A';
for(int j=1;j<=i;j++)
[Link](k+" ");
k++;
[Link]();
[Link]();
char m='A';
for(int i=1;i<=5;i++)
for(int j=1;j<=i;j++)
[Link](m+" ");
m++
[Link]();
[Link]();
char p='A';
for(int i=1;i<=4;i++)
for(int j=1;j<=i;j++)
[Link](p++ +" ");
[Link]();
[Link]();
for(int i=5;i>=1;i--)
char k=65;
for(int j=1;j<=i;j++)
[Link](k+" ");
k++;
[Link]();
[Link]();
for(int i=65;i<=69;i++)
{
for(int j=i;j<=69;j++)
[Link]((char)j+" ");
[Link]();
[Link]();
for(int i=1;i<=4;i++)
char k='Z';
for(int j=1;j<=i;j++)
[Link](k+" ");
k--;
[Link]();