-
Notifications
You must be signed in to change notification settings - Fork 0
/
PracticeSet.txt
1490 lines (1181 loc) · 36.7 KB
/
PracticeSet.txt
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1) Write a program that accepts two item’s weight (floating points' values ) and number of purchase (floating points' values) and calculate the average value of the items.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
float w1, w2, i1, i2, avg;
System.out.print("Weight-Item1: ");
w1= sc.nextFloat();
System.out.print("No. of item1: ");
i1= sc.nextFloat();
System.out.print("Weight-Item2: ");
w2= sc.nextFloat();
System.out.print("No. of item2: ");
i2= sc.nextFloat();
avg= ((w1*i1) + (w2*i2))/(i1+i2);
System.out.println("Average value = " + avg);
}
}
Output:
Weight-Item1: 15
No. of item1: 5
Weight-Item2: 25
No. of item2: 4
Average value = 19.444445
--------------------------------------------------------------------
2) Write a program that accepts an employee's ID, total worked hours of a month and the amount he received per hour. Print the employee's ID and salary (with two decimal places) of a particular month.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String id;
float totalHrs, amtHour;
float salary;
System.out.print("Input the Employees ID(Max. 10 chars): ");
id= sc.next();
System.out.print("Input the working hrs: ");
totalHrs= sc.nextInt();
System.out.print("Salary amount/hr: ");
amtHour= sc.nextInt();
salary= totalHrs * amtHour;
System.out.println("Employees ID = " + id);
System.out.printf("Salary = U$ %.2f" ,salary);
}
}
Output:
Input the Employees ID(Max. 10 chars): 0342
Input the working hrs: 8
Salary amount/hr: 15000
Employees ID = 0342
Salary = U$ 120000.00
------------------------------------------------------------------------
3) Write a program to calculate a bike’s average consumption from the given total distance
(integer value) traveled (in km) and spent fuel (in liters, float number – 2 decimal point).
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int distance;
float fuel, average;
System.out.print("Input total distance in km: ");
distance= sc.nextInt();
System.out.print("Input total fuel spent in litres: ");
fuel= sc.nextInt();
average= distance/fuel;
System.out.printf("Average consumption (km/lt): %.2f", average);
}
}
Output:
Input total distance in km: 350
Input total fuel spent in litres: 5
Average consumption (km/lt): 70.00
------------------------------------------------------------------------
4) Write a program to convert specified days into years, weeks and days.
Note: Ignore leap year.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int num, years, weeks, days;
System.out.print("Number of days: ");
num= sc.nextInt();
years= num/365;
weeks= (num%365)/7;
days= (num%365)%7;
System.out.println("Years: " + years);
System.out.println("Weeks: " + weeks);
System.out.println("Days: " + days);
}
}
Output:
Number of days: 1329
Years: 3
Weeks: 33
Days: 3
------------------------------------------------------------------------
5) Write a program to convert a given integer (in seconds) to hours, minutes and seconds.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int num, hrs, mins, sec;
System.out.print("Input seconds: ");
num= sc.nextInt();
hrs= num/(60*60);
mins= (num- (hrs*60*60))/60;
sec= num- (hrs*60*60) - (mins*60);
System.out.println("H:M:S - "+ hrs + ":" + mins + ":" + sec);
}
}
Output:
Input seconds: 25300
H:M:S - 7:1:40
------------------------------------------------------------------------
6) Write a program to convert a given integer (in days) to years, months and days, assumes that all months have 30 days and all years have 365 days.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int num, year, month, days;
System.out.print("Input no. of days: ");
num= sc.nextInt();
year= num/365;
month= (num%365)/30;
days= (num%365)%30;
System.out.println(year +" Year(s)");
System.out.println(month + " Month(s)");
System.out.println(days + " Day(s)");
}
}
Output:
Input no. of days: 2535
6 Year(s)
11 Month(s)
15 Day(s)
------------------------------------------------------------------------
7) Write a program to find all numbers which are dividing by 7 and the remainder is equal to 2 or 3 between two given integer numbers.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n1, n2;
System.out.print("Input the first integer: ");
n1= sc.nextInt();
System.out.print("Input the second integer: ");
n2= sc.nextInt();
for(int i=n1; i<=n2; i++){
if(i%7==2 || i%7==3){
System.out.println(i);
}
}
}
}
Output:
Input the first integer: 25
Input the second integer: 45
30
31
37
38
44
45
------------------------------------------------------------------------
8) Write a program that accepts three integers and find the maximum of three.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n1, n2, n3, max=0;
System.out.print("Input the first integer: ");
n1= sc.nextInt();
System.out.print("Input the second integer: ");
n2= sc.nextInt();
System.out.print("Input the third integer: ");
n3= sc.nextInt();
if(n1>n2){
if(n1>n3){
max= n1;
}
else{
max= n3;
}
}
else if(n2>n1){
if(n2>n3){
max= n2;
}
else{
max=n3;
}
}
System.out.println("Maximum value of three integers: " + max);
}
}
Output:
Input the first integer: 25
Input the second integer: 35
Input the third integer: 15
Maximum value of three integers: 35
------------------------------------------------------------------------
9) Write a program that accepts three integers and find the maximum of three.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n1, n2, n3, max=0;
System.out.print("Input the first integer: ");
n1=sc.nextInt();
System.out.print("Input the second integer: ");
n2=sc.nextInt();
System.out.print("Input the third integer: ");
n3=sc.nextInt();
if(n1>n2){
if(n1>n3){
max= n1;
}
else{
max= n3;
}
}
else if(n2>n1){
if(n2>n3){
max= n2;
}
else{
max=n3;
}
}
System.out.println("Maximum value of three integers: " + max);
}
}
Output:
Input the first integer: 25
Input the second integer: 35
Input the third integer: 15
Maximum value of three integers: 35
------------------------------------------------------------------------
10) Write a program to calculate a bike’s average consumption from the given total distance (integer value) traveled (in km) and spent fuel (in liters, float number – 2 decimal point).
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int distance;
float fuel, avg;
System.out.print("Input total distance in km: ");
distance=sc.nextInt();
System.out.print("Input total fuel spent in liters: ");
fuel= sc.nextFloat();
avg= distance/fuel;
System.out.printf("Average consumption(km/lt) %.2f" ,avg);
}
}
Output:
Input total distance in km: 350
Input total fuel spent in liters: 5
Average consumption(km/lt) 70.00
------------------------------------------------------------------------
11) Write a program to read an amount (integer value) and break the amount into smallest possible number of bank notes.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int amount, hundred, fifty, twenty, ten, five, two, one;
System.out.print("Input the amount: ");
amount= sc.nextInt();
hundred= amount/100;
fifty= (amount- (hundred*100))/50;
twenty= (amount- (hundred*100) - (fifty*50))/20;
ten= (amount- (hundred*100) - (fifty*50) - (twenty*20))/10;
five= (amount- (hundred*100) - (fifty*50) - (twenty*20) - (ten*10))/5;
two= (amount- (hundred*100) - (fifty*50) - (twenty*20) - (ten*10) - (five*5))/2;
one= (amount- (hundred*100) - (fifty*50) - (twenty*20) - (ten*10) - (five*5) - (two*2));
System.out.println(hundred + "Notes(s) of 100");
System.out.println(fifty + "Notes(s) of 50");
System.out.println(twenty + "Notes(s) of 20");
System.out.println(ten + "Notes(s) of 10");
System.out.println(five + "Notes(s) of 5");
System.out.println(two + "Notes(s) of 2");
System.out.println(one + "Notes(s) of 1");
}
}
Output:
Input the amount: 375
3Notes(s) of 100
1Notes(s) of 50
1Notes(s) of 20
0Notes(s) of 10
1Notes(s) of 5
0Notes(s) of 2
0Notes(s) of 1
------------------------------------------------------------------------
12) Write a program that read 5 numbers and sum of all odd values between them.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n[]= new int[5];
for(int i=0; i<n.length; i++){
System.out.print("Input number " + (int)(i+1)+ ": ");
n[i]= sc.nextInt();
}
int sum=0;
for(int i=0; i<n.length; i++){
if(n[i]%2==1){
sum+= n[i];
}
}
System.out.println("Sum of all odd values: " + sum);
}
}
Output:
Input number 1: 11
Input number 2: 17
Input number 3: 13
Input number 4: 12
Input number 5: 5
Sum of all odd values: 46
------------------------------------------------------------------------
13) Write a program that reads three floating values and check if it is possible to make a triangle with them. Also calculate the perimeter of the triangle if the said values are valid.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
float n1, n2, n3, perimeter;
System.out.print("Input the first number: ");
n1= sc.nextInt();
System.out.print("Input the second number: ");
n2= sc.nextInt();
System.out.print("Input the third number: ");
n3= sc.nextInt();
if(n1+n2 >n3 && n2+n3 >n1 && n1+n3 >n2){
perimeter= n1+n2+n3;
System.out.printf("Perimeter = %.1f" ,perimeter);
}
else{
System.out.println("Invalid triangle values");
}
}
}
Output:
Input the first number: 25
Input the second number: 15
Input the third number: 35
Perimeter = 75.0
------------------------------------------------------------------------
14) Write a program that reads two integers and checks whether they are multiplied or not.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n1, n2;
System.out.print("Input the first number: ");
n1= sc.nextInt();
System.out.print("Input the second number: ");
n2= sc.nextInt();
if(n2%n1==0){
System.out.printf("Multiplied!");
}
else{
System.out.println("Not multiplied!");
}
}
}
Output:
Input the first number: 5
Input the second number: 15
Multiplied!
------------------------------------------------------------------------
15) Write a program that reads an integer between 1 and 12 and print the month of the year in
English.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n;
System.out.print("Input a number between 1 to 12 to get the month name: ");
n= sc.nextInt();
switch(n){
case 1:
System.out.println("January"); break;
case 2:
System.out.println("February"); break;
case 3:
System.out.println("March"); break;
case 4:
System.out.println("April"); break;
case 5:
System.out.println("May"); break;
case 6:
System.out.println("June"); break;
case 7:
System.out.println("July"); break;
case 8:
System.out.println("August"); break;
case 9:
System.out.println("September"); break;
case 10:
System.out.println("October"); break;
case 11:
System.out.println("November"); break;
case 12:
System.out.println("December"); break;
case 13:
System.out.println("Invalid month number");
}
}
}
Output:
Input a number between 1 to 12 to get the month name: 8
August
------------------------------------------------------------------------
16) Write a program that prints all even numbers between 1 and 50 (inclusive).
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
for(int i=1; i<=50; i++){
if(i%2==0){
System.out.print(i + " ");
}
}
}
}
Output:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
------------------------------------------------------------------------
17) Write a program that read 5 numbers and counts the number of positive numbers and negative numbers.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n[]= new int[5];
for(int i=0; i<n.length; i++){
System.out.print("Input number " + (int)(i+1)+ ": ");
n[i]= sc.nextInt();
}
int p_count=0, n_count=0;
for(int i=0; i<n.length; i++){
if(n[i]>=0){
p_count +=1;
}
else{
n_count +=1;
}
}
System.out.println("Number of positive numbers: " + p_count);
System.out.println("Number of negative numbers: " + n_count);
}
}
Output:
Input number 1: 5
Input number 2: -4
Input number 3: 10
Input number 4: 15
Input number 5: -1
Number of positive numbers: 3
Number of negative numbers: 2
------------------------------------------------------------------------
18) Write a program that read 5 numbers and counts the number of positive numbers and print the average of all positive values.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n[]= new int[5];
for(int i=0; i<n.length; i++){
System.out.print("Input number " + (int)(i+1)+ ": ");
n[i]= sc.nextInt();
}
int p_count=0, n_count=0, sum=0;
float avg;
for(int i=0; i<n.length; i++){
if(n[i]>=0){
p_count +=1;
sum +=n[i];
}
}
avg= sum/p_count;
System.out.println("Number of positive numbers: " + p_count);
System.out.println("Average value of the said positive numbers: " + avg);
}
}
Output:
Input number 1: 5
Input number 2: 8
Input number 3: 10
Input number 4: -5
Input number 5: 25
Number of positive numbers: 4
Average value of the said positive numbers: 12.0
------------------------------------------------------------------------
19) Write a program that read 5 numbers and sum of all odd values between them.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n[]= new int[5];
for(int i=0; i<n.length; i++){
System.out.print("Input number " + (int)(i+1)+ ": ");
n[i]= sc.nextInt();
}
int sum=0;
for(int i=0; i<n.length; i++){
if(n[i]%2==1){
sum +=n[i];
}
}
System.out.println("Sum of all odd values: " + sum);
}
}
Output:
Input number 1: 5
Input number 2: 7
Input number 3: 9
Input number 4: 10
Input number 5: 13
Sum of all odd values: 34
------------------------------------------------------------------------
20) Write a program to find and print the square of each one of the even values from 1 to a specified value.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n, square;
System.out.print("Enter a positive value: ");
n= sc.nextInt();
System.out.println("List of square of each one of the even values from 1 to " + n + ": ");
for(int i=1; i<=n; i++){
if(i%2==0){
System.out.println(i + "^" + i + " = " + i*i);
}
}
}
}
Output:
Enter a positive value: 15
List of square of each one of the even values from 1 to 15:
2^2 = 4
4^4 = 16
6^6 = 36
8^8 = 64
10^10 = 100
12^12 = 144
14^14 = 196
------------------------------------------------------------------------
21) Write a program to check whether a given integer is positive even, negative even, positive odd or negative odd. Print even if the number is 0.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n;
System.out.print("Enter an integer: ");
n= sc.nextInt();
if(n==0){
System.out.println("Even");
}
else if(n>0){
if(n%2==0){
System.out.println("Positive even");
}
else{
System.out.println("Positive odd");
}
}
else if(n<0){
if(n%2==0){
System.out.println("Negative even");
}
else{
System.out.println("Negative odd");
}
}
}
}
Output:
Enter an integer: -4
Negative even
------------------------------------------------------------------------
22) Write a program that accepts some integers from the user and find the highest value and the input position.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n[]= new int[5];
System.out.println("Input 5 integers: ");
for(int i=0; i<n.length; i++){
n[i]= sc.nextInt();
}
int max= n[0];
int position=0;
for(int i=1; i<n.length; i++){
if(max<n[i]){
max= n[i];
position=i;
}
}
System.out.println("Highest value: " + max);
System.out.println("Position: " + position);
}
}
Output:
Input 5 integers:
5
7
15
23
45
Highest value: 45
Position: 4
------------------------------------------------------------------------
23) Write a program to print all numbers between 1 to 100 which divided by a specified number and the remainder will be 3.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n[5];
System.out.print("Input 5 integers: ");
n= sc.nextInt();
for(int i=1; i<=100; i++){
if(i%n==3){
System.out.println(i);
}
}
}
}
Output:
Input an integer: 25
3
28
53
78
------------------------------------------------------------------------
24) Write a program to read a password until it is correct. For wrong password print "Incorrect password" and for correct password print "Correct password" and quit the program. The correct password is 1234.
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
String password;
System.out.print("Input the password: ");
password= sc.next();
if(password.equals("1234")){
System.out.println("Correct password");
}
else{
System.out.println("The correct password is 1234");
}
}
}
Output:
Input the password: 1234
Correct password
------------------------------------------------------------------------
25) Write a program to compute the sum of consecutive odd numbers from a given pair of integers.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n1, n2;
System.out.print("Input first number of the pair: ");
n1= sc.nextInt();
System.out.print("Input second number of the pair: ");
n2= sc.nextInt();
int sum=0;
for(int i=n2; i<=n1; i++){
if(i%2==1){
System.out.println(i);
sum+= i;
}
}
System.out.println("Sum= " + sum);
}
}
Output:
Input first number of the pair: 10
Input second number of the pair: 2
3
5
7
9
Sum= 24
------------------------------------------------------------------------
26) Write a program to check whether two numbers in a pair is in ascending order or descending order.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n1, n2;
System.out.println("Input a pair of numbers: ");
System.out.print("Input first number of the pair: ");
n1= sc.nextInt();
System.out.print("Input second number of the pair: ");
n2= sc.nextInt();
if(n1>n2){
System.out.println("The pair is in descending order!");
}
else{
System.out.println("The pair is in ascending order!");
}
}
}
Output:
Input a pair of numbers:
Input first number of the pair: 10
Input second number of the pair: 2
The pair is in descending order!
------------------------------------------------------------------------
27) Write a program that reads two numbers and divide the first number by second number. If the division not possible print "Division not possible".
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n1, n2;
float div;
System.out.println("Input two numbers: ");
System.out.print("x: ");
n1= sc.nextInt();
System.out.print("y: ");
n2= sc.nextInt();
if(n1/n2 >0){
div= n1/n2;
System.out.println("Output: " + div);
}
else{
System.out.println("Division not possible");
}
}
}
Output:
Input two numbers:
x: 25
y: 5
Output: 5.0
------------------------------------------------------------------------
28) Write a program to calculate the sum of all number not divisible by 17 between two given integer numbers.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n1, n2, sum=0;
System.out.print("Input the first integer: ");
n1= sc.nextInt();
System.out.print("Input the second integer: ");
n2= sc.nextInt();
for(int i=n1; i<=n2; i++){
if(i%17!=0){
sum +=i;
}
}
System.out.println("Sum = " + sum);
}
}
Input the first integer: 50
Input the second integer: 99
Sum = 3521
------------------------------------------------------------------------
29) Write a program to find all numbers which are dividing by 7 and the remainder is equal to 2 or 3 between two given integer numbers.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n1, n2, sum=0;
System.out.print("Input the first integer: ");
n1= sc.nextInt();
System.out.print("Input the second integer: ");
n2= sc.nextInt();
for(int i=n1; i<=n2; i++){
if(i%7==2 || i%7==3){
System.out.println(i);
}
}
}
}
Output:
Input the first integer: 25
Input the second integer: 45
30
31
37
38
44
45
------------------------------------------------------------------------