Skip to content

Commit bd1743c

Browse files
authored
Merge pull request #46 from tushar152/main
Create amstrongNumber.java
2 parents 56f1568 + 4a640b0 commit bd1743c

File tree

7 files changed

+114
-103
lines changed

7 files changed

+114
-103
lines changed

BubbleSort.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
11
import java.lang.*;
2-
public class BubbleSort{
3-
public static void main(String arg[])
4-
{
2+
3+
public class BubbleSort {
4+
public static void main(String arg[]) {
55
int a[]={30,60,35,20,45,32,50};
66
System.out.println("Array Before Sorting");
7-
for(int i=0;i<a.length;i++)
8-
{
7+
for(int i=0;i<a.length;i++) {
98
System.out.print(a[i]+" ");
109
}
1110
System.out.println();
1211
bubbleSort(a);
1312
System.out.println("Array after sorting");
14-
for(int i=0;i<a.length;i++)
15-
{
13+
for(int i=0;i<a.length;i++) {
1614
System.out.print(a[i]+" ");
1715
}
1816
}
19-
static void bubbleSort(int[] a){
17+
static void bubbleSort(int[] a) {
2018
int n = a.length;
2119
int t = 0;
22-
for(int i=0;i<n;i++)
23-
{
24-
for(int j=1;j<n-i;j++)
25-
{
26-
if(a[j-1]>a[j])
27-
{
20+
for(int i=0;i<n;i++) {
21+
for(int j=1;j<n-i;j++) {
22+
if(a[j-1]>a[j]) {
2823
t = a[j-1];
2924
a[j-1] = a[j];
3025
a[j]= t;
3126
}
3227
}
3328
}
3429
}
35-
}
30+
}

Echo.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
class Echo
2-
{
3-
public static void main(String args[])
4-
{
5-
for(int i=0;i<args.length;i++)
6-
{
1+
class Echo {
2+
public static void main(String args[]) {
3+
for(int i=0;i<args.length;i++) {
74
System.out.print(args[i]+" ");
85
System.out.println("\n");
9-
}
10-
System.exit(0);
116
}
7+
System.exit(0);
128
}
13-
9+
}
10+

Fibonacci.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
class FibonacciExample1{
2-
public static void main(String args[])
3-
{
4-
int n1=0,n2=1,n3,i,count=10;
5-
System.out.print(n1+" "+n2);//printing 0 and 1
6-
7-
for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
8-
{
9-
n3=n1+n2;
10-
System.out.print(" "+n3);
11-
n1=n2;
12-
n2=n3;
13-
}
14-
15-
}}
1+
class FibonacciSeries {
2+
public static void main(String args[])
3+
{
4+
int n1=0,n2=1,n3,i,count=10;
5+
6+
//printing 0 and 1
7+
System.out.print(n1+" "+n2);
8+
9+
//loop starts from 2 because 0 and 1 are already printed
10+
for(i=2;i<count;++i) {
11+
n3=n1+n2;
12+
System.out.print(" "+n3);
13+
n1=n2;
14+
n2=n3;
15+
}
16+
}
17+
}
1618

Harry.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
class Harry
2-
{
3-
public static void main(String args[])
4-
{
5-
int i=0;
6-
i=Integer.parseInt(args[0]);
7-
System.out.println("The value of i in Integer is"+i);
1+
class Harry {
2+
public static void main(String args[]) {
3+
int i = 0;
4+
i = Integer.parseInt(args[0]);
5+
System.out.println("The value of i in Integer is"+i);
6+
}
87
}
9-
}

QuickSort.java

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,48 @@
1-
class QuickSort
2-
{
3-
public static void main(String[]args)
4-
{
1+
class QuickSort {
2+
public static void main(String[]args) {
53
int a[]={10,75,-15,17,0,-2};
64
QSort(a,0,a.length-1);
75
System.out.println("Sorted Array : ");
8-
System.out.print("[");
9-
for(int i=0;i<a.length;i++)
10-
{
11-
if(i == a.length-1)
12-
System.out.print(a[i]);
13-
else
14-
System.out.print(a[i] + ", ");
15-
}
16-
System.out.println("]");
6+
System.out.print("[");
7+
for(int i=0;i<a.length;i++) {
8+
if(i == a.length-1)
9+
System.out.print(a[i]);
10+
else
11+
System.out.print(a[i] + ", ")
12+
}
13+
System.out.println("]");
1714
}
18-
19-
public static void QSort(int a[],int l,int h)
20-
{
21-
if(l<h)
22-
{
15+
16+
public static void QSort(int a[],int l,int h) {
17+
if(l<h) {
2318
int loc=Partion(a,l,h);
2419
QSort(a,l,loc-1);
2520
QSort(a,loc+1,h);
2621
}
2722
}
2823

29-
public static int Partion(int a[],int l,int h)
30-
{
24+
public static int Partion(int a[],int l,int h) {
3125
int pivot = a[l];
3226
int start=l;
3327
int end=h;int tmp;
3428

35-
while(start < end)
36-
{
37-
while(start<=h && a[start]<=pivot)
38-
{
29+
while(start < end) {
30+
while(start<=h && a[start]<=pivot) {
3931
start++;
4032
}
41-
while(a[end]>pivot)
42-
{
33+
while(a[end]>pivot) {
4334
end--;
4435
}
45-
if(start<end)
46-
{
36+
if(start<end) {
4737
tmp=a[start];
4838
a[start]=a[end];
4939
a[end]=tmp;
50-
51-
5240
}
53-
5441
}
55-
5642

5743
tmp=a[l];
5844
a[l]=a[end];
5945
a[end]=tmp;
6046
return end;
6147
}
62-
}
48+
}

ShellSort.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
1-
public class ShellSort
2-
{
3-
public static void main(String[] args)
4-
{
1+
public class ShellSort {
2+
public static void main(String[] args) {
53
int a[]={25,-17,8,92,-21,0};
64

7-
for(int gap=a.length/2;gap>0;gap/=2)
8-
{
9-
10-
for(int i=gap;i<a.length;i++)
11-
{
5+
for(int gap=a.length/2;gap>0;gap/=2) {
6+
for(int i=gap;i<a.length;i++) {
127
int tmp=a[i];
138
int j=i;
149

15-
while(j>=gap &&a[j-gap]>tmp)
16-
{
10+
while(j>=gap &&a[j-gap]>tmp) {
1711
a[j]=a[j-gap];
1812
j-=gap;
1913
}
2014
a[j]=tmp;
21-
2215
}
2316
}
24-
25-
System.out.println("Sorted Array : ");
26-
System.out.print("[");
27-
for(int i=0;i<a.length;i++)
28-
{
29-
if(i == a.length-1)
30-
System.out.print(a[i]);
31-
else
32-
System.out.print(a[i] + ", ");
17+
18+
System.out.println("Sorted Array : ");
19+
System.out.print("[");
20+
21+
for(int i=0;i<a.length;i++) {
22+
if(i == a.length-1)
23+
System.out.print(a[i]);
24+
else
25+
System.out.print(a[i] + ", ");
3326
}
3427
System.out.println("]");
3528
}
36-
37-
}
29+
}

armstrongNumber.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Program to determine whether the number is Armstrong number or not
2+
public class Armstrong {
3+
// Function to calculate x raised to the power y
4+
int power(int x, long y) {
5+
if( y == 0)
6+
return 1;
7+
if (y%2 == 0)
8+
return power(x, y/2)*power(x, y/2);
9+
return x*power(x, y/2)*power(x, y/2);
10+
}
11+
12+
// Function to calculate order of the number
13+
int order(int x) {
14+
int n = 0;
15+
while (x != 0) {
16+
n++;
17+
x = x/10;
18+
}
19+
return n;
20+
}
21+
22+
boolean isArmstrong (int x) {
23+
// Calling order function
24+
int n = order(x);
25+
int temp=x, sum=0;
26+
while (temp!=0) {
27+
int r = temp%10;
28+
sum = sum + power(r,n);
29+
temp = temp/10;
30+
}
31+
return (sum == x);
32+
}
33+
34+
public static void main(String[] args) {
35+
Armstrong ob = new Armstrong();
36+
int x = 153;
37+
System.out.println(ob.isArmstrong(x));
38+
x = 1253;
39+
System.out.println(ob.isArmstrong(x));
40+
}
41+
}

0 commit comments

Comments
 (0)