-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConditional
More file actions
222 lines (202 loc) · 4.49 KB
/
Conditional
File metadata and controls
222 lines (202 loc) · 4.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
Q1 - Write a program which takes the values of length and breadth from user and check if it is a square or
not.
Enter length:
5
Enter breadth:
4
It is a rectangle
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int length;
System.out.print("Enter the length : ");
length=sc.nextInt();
int breadth;
System.out.print("Enter the breadth : ");
breadth=sc.nextInt();
if(length==breadth)
{
System.out.print("It is a square");
}else{
System.out.print("It is not a square");
}
}
}
Sample Input :
Sample Output :
Assignment Solutions
Cracking the Coding Interview in JAVA - Foundation
Q2 - Write a program to print absolute value of a number entered by the user.
-1
1
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num;
System.out.print("Enter the number : ");
num=sc.nextInt();
if(num<0)
{
num=num*-1;
}
System.out.print("The absolute value is : " + num);
}
}
Sample Input :
Sample Output :
Assignment Solutions
Cracking the Coding Interview in JAVA - Foundation
Q3 - Write a program to take input from user for Cost Price (C.P.) and Selling Price(S.P.) and calculate Profit
or Loss.
Explanation : Formula for profit and loss
Profit = S.P - C.P
Loss = C.P - S.P
(S.P is Selling Price and C.P is Cost Price)
Enter cost price: 4000
Enter selling price: 9560
Profit = 5560
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int cp;
int sp;
System.out.print("Enter the Cost Price : ");
cp=sc.nextInt();
System.out.print("Enter the Selling Price : ");
sp=sc.nextInt();
int amt;
if(cp>sp)
{
amt=cp-sp;
System.out.print("The loss is : "+amt);
}else if(cp<sp)
{
amt=sp-cp;
System.out.print("The profit is : "+amt);
}else{
System.out.print("No Profit No Loss");
}
}
}
Sample Input :
Sample Output :
Assignment Solutions
Cracking the Coding Interview in JAVA - Foundation
Assignment Solutions
Cracking the Coding Interview in JAVA - Foundation
Q4 - Write a program to print positive number entered by the user, if user enters a negative number, it is
skipped.
Enter an integer: -6
The number is negative and skipped
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x;
System.out.print("Enter the Number : ");
x=sc.nextInt();
if(x>=0)
{
System.out.print("You entered a positive number");
}else{
System.out.print("You number is negative and is skipped");
}
}
}
Sample Input :
Sample Output :
Assignment Solutions
Cracking the Coding Interview in JAVA - Foundation
Q5 - Create a calculator using switch statement to perform addition, subtraction, multiplication and division.
Enter an operator (+, -, *, /): -
Enter two numbers:
6
8
6 - 8 = -2
import java.util.*;
class Main {
public static void main(String[] args) {
char op;
Double num1;
Double num2;
Double ans;
Scanner sc = new Scanner(System.in);
System.out.print("Enter an operator: (+, -, *, or /)");
op = sc.next().charAt(0);
System.out.print("Enter first number : ");
num1 = sc.nextDouble();
System.out.print("Enter second number : ");
num2 = sc.nextDouble();
switch (op) {
case '+':
ans = num1 + num2;
System.out.println(num1 + " + " + num2 + " = " + ans);
break;
case '-':
ans = num1 - num2;
System.out.println(num1 + " - " + num2 + " = " + ans);
break;
case '*':
ans = num1 * num2;
System.out.println(num1 + " * " + num2 + " = " + ans);
break;
case '/':
ans = num1 / num2;
System.out.println(num1 + " / " + num2 + " = " + ans);
break;
default:
System.out.println("Error! The operator is not correct");
break;
}
}
}
Sample Input :
Sample Output :
Assignment Solutions
Cracking the Coding Interview in JAVA - Foundation
Q6 - Write a program to calculate marks to grades . Follow the conversion rule as given below :
Enter Your Marks: 98
Sample Output :
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int marks;
System.out.print("Enter the marks : ");
marks=sc.nextInt();
if(marks>=90)
{
System.out.print("Your Grade is A+");
}else if(marks>=80)
{
System.out.print("Your Grade is A");
}else if(marks>=70)
{
System.out.print("Your Grade is B+");
}else if(marks>=60)
{
System.out.print("Your Grade is B");
}else if(marks>=50)
{
System.out.print("Your Grade is C");
}else if(marks>=40)
{
System.out.print("Your Grade is D");
}else if(marks>=30)
{
System.out.print("Your Grade is E");
}else if(marks<30){
System.out.print("Your Grade is f");
}else{
System.out.print("Enter valid marks");
}
}
}