-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple Java Programs
More file actions
140 lines (121 loc) · 3.49 KB
/
Simple Java Programs
File metadata and controls
140 lines (121 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Prime Number
import java.util.Scanner;
class Firstday{
public static void main(String[] args) {
// Prime Number
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int count = 0;
if (num % num == 0){
count++;
if (num % 1 == 0){
count++;
if (num % 2 == 0){
count++;
}
else if (num % 3 == 0){
count++;
}
else if (num % 5 ==0){
count++;
}
}
}
if (count > 2){
System.out.println("The Number is not prime");
}
else{
System.out.println("The number is prime");
}
}
}
// Maximum of three numbers
import java.util.Scanner;
class Firstday{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
int a = sc.nextInt();
System.out.print("Enter b: ");
int b = sc.nextInt();
System.out.print("Enter c: ");
int c = sc.nextInt();
int max = Math.max(c, Math.max(a, b));
System.out.println("The Maximum Number is "+ max);
}
}
// Factorial of a number (Imp)
import java.util.Scanner;
class Secondday{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int i = 1;
int fact = 1;
while (i <= num){
fact = fact * i;
i++;
}
System.out.println("The factorial of the number is "+ fact);
}
}
// Fibonacci series element of a number (Imp)
import java.util.Scanner;
class Secondday{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int a = 0;
int b = 1;
int count = 2;
while (count <= num){
int temp = a;
a = a + b;
b = temp;
count++;
}
System.out.println(a);
}
}
// Armstrong Number
import java.lang.Math;
import java.util.Scanner;
public class ThirdDay {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number: ");
int num = input.nextInt();
int originalnum = num;
int rem;
int count = 0;
while(originalnum != 0){
rem = originalnum % 10;
count += Math.pow(rem, 3);
originalnum = originalnum / 10;
}
if (count == num){
System.out.println("This is an Armstrong Number");
}
else{
System.out.println("This is not an Armstrong Number");
}
}
}
//Swapping of two numbers
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1 = input.nextInt();
int num2 = input.nextInt();
swap(num1, num2);
System.out.println("Values of a and b are swapped which are: " + num1 + " " + num2);
}
static void swap(int a, int b){
int temp = a;
a = b;
b = temp;
// This change will only be valid in this function scope only.
}
}
//In the above code, swapping didn't take place because a and b are being new object inside the swap function that's why outside of the swap method, they aren't getting swapped.