-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment 1
180 lines (163 loc) · 5.38 KB
/
Assignment 1
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
**{CHECK A No. IS EVEN OR ODD}**
package com.javalearnings;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the number");
int num = input.nextInt();
if (num % 2 == 0) {
System.out.println("even number");
} else {
System.out.println("odd number");
}
}
}
**{ASK NAME AND GREET}**
package com.javalearnings;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your name: ");
String name = input.nextLine();
System.out.println("Hello, " + name + " nice to meet you keep growing & keep shining");
}
}
**{CALCULATE S.I.}**
package com.javalearnings;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter principle amount: ");
Double amt = input.nextDouble();
System.out.println("Enter rate of interest: ");
Float rate = input.nextFloat();
System.out.println("Enter time in years: ");
int t = input.nextInt();
Double totalamt = (amt * rate * t) / 100;
System.out.println("total amt" + totalamt);
}
}
**{Take in two numbers and an operator (+, -, *, /) and calculate the value. (Use if conditions)}**
package com.javalearnings;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter two numbers: ");
Double num1 = input.nextDouble();
Double num2 = input.nextDouble();
System.out.println("Enter the operator: ");
char operator = input.next().charAt(0);
if(operator == '+'){
System.out.println("The sum of the two numbers is: " + (num1 + num2));
}
else if(operator == '-'){
System.out.println("The difference of the two numbers is: " + (num1 - num2));
}
else if(operator == '*'){
System.out.println("The product of the two numbers is: " + (num1 * num2));
}
else if(operator == '/'){
System.out.println("The division of the two numbers is: " + (num1 / num2));
}
else if(operator == '%'){
System.out .println("The remainder of the two numbers is: " + (num1 % num2));
else{
System.out.println("Invalid operator");
}
}
}
**{take two numbers and print the largest one}**
package com.javalearnings;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter two numbers: ");
int num1 = input.nextInt();
int num2 = input.nextInt();
if(num1 > num2){
System.out.println(num1 + " is the largest number.");
}
else{
System.out.println(num2 + " is the largest number.");
}
}
}
**{take input & convert indian ruppees into dollars}**
package com.javalearnings;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the amount in INR: ");
double inr = input.nextDouble();
double usd = inr / 80 ;
System.out.println("The amount in USD is: " + usd);
}
}
**{To calculate Fibonacci Series up to n numbers.}**
package com.javalearnings;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of terms: ");
int n = input.nextInt();
int a = 0;
int b = 1;
System.out.print("The Fibonacci Series is: ");
for(int i = 0; i < n; i++){
System.out.println(a + " ");
int num = a+b;
a=b;
b=num;
}
}
}
**{To find out whether the given String is Palindrome or not.}**
package com.javalearnings;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = input.nextLine();
String rev = "";
for(int i = str.length()-1; i >= 0; i--){
rev = rev + str.charAt(i);
}
if(str.equals(rev)){
System.out.println("The string is a palindrome.");
}
else{
System.out.println("The string is not a palindrome.");
}
}
}
**{To find Armstrong Number between two given number.}**
package com.javalearnings;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the range: ");
int n1 = input.nextInt();
int n2 = input.nextInt();
for(int i = n1; i <= n2; i++){
int num = i;
int sum = 0;
while(num > 0){
int rem = num % 10;
sum = sum + (rem * rem * rem);
num = num / 10;
}
if(sum == i){
System.out.println(i + " is an Armstrong number.");
}
}
}
}