└── README.md /README.md: -------------------------------------------------------------------------------- 1 | 2 | 1.calculateSal 3 | Read the question carefully and follow the input and output format. 4 | 5 | Karen got salary for this month and she spends 20% of her salary for food and 30% of her salary for travel. If she takes care of other shifts she will get 2% of the salary per day. Given her salary and the number of shifts she handled. Calculate how much she can save in her pocket after spending all these? 6 | 7 | Input and Output Format : 8 | First line of input consists of an integer, salary. Next line correspond to the number of shifts. Output consist of an integer, which is saving. 9 | 10 | 1) Print "Salary too large" when salary is greater than 8000. 11 | 2) Print "Shifts too small" when the shift is less than 0. 12 | 3) Print "Salary too small" when the salary is less than 0. 13 | 14 | Include a function named calculateSal(int salary, int shifts) whose return type is an integer, which is the saving. 15 | 16 | Sample Input 1: 17 | 7000 18 | 5 19 | Sample Output 1: 20 | 4200 21 | 22 | Sample Input 2: 23 | 8001 24 | 25 | Sample Output 2: 26 | Salary too large 27 | import java.util.Scanner; 28 | public class Main 29 | { 30 | public static void main(String[] args) 31 | { 32 | int salary=0,shifts=0; 33 | double savings=0; 34 | Scanner in=new Scanner(System.in); 35 | salary = in.nextInt(); 36 | shifts = in.nextInt(); 37 | if(salary > 8000) 38 | System.out.print("Salary too large "); 39 | else if(shifts<0) 40 | System.out.print ("Shifts too small\n"); 41 | else if(salary<0) 42 | System.out.print ("Salary too small"); 43 | else 44 | { 45 | savings = (salary*0.5)+(salary*0.02*shifts); 46 | System.out.printf ("%.0f",savings); 47 | } 48 | } 49 | } 50 | 51 | 2.Repeated Salary Count 52 | John is working as a clerk in an organization where N number of people are working. His boss has asked him to get the count of employees who get same salary. Help him to get the count of repeated salary. 53 | Include a function named countRepeaters that accepts 2 arguments and returns an int. The first argument is the input array and the second argument is an int that corresponds to the size of the array. The function returns an int that corresponds to the number of repeaters. 54 | If the size of the array is negative or if any of the array elements are negative, print “Invalid Input” and terminate the program. 55 | Input and Output Format: 56 | Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next 'n' integers correspond to the elements in the array. 57 | Output consists of an integer that corresponds to the number of repeaters. 58 | Assume that utmost one element in the array would repeat. 59 | Assume that the maximum number of elements in the array is 20. 60 | 61 | Sample Input 1: 62 | 5 63 | 1000 64 | 2000 65 | 3500 66 | 2000 67 | 5000 68 | 69 | Sample Output 1: 70 | 2 71 | 72 | Sample Input 2: 73 | -5 74 | 75 | Sample Output 2: 76 | Invalid Input 77 | 78 | Sample Input 3: 79 | 5 80 | 1000 81 | -2000 82 | 83 | Sample Output 3: 84 | Invalid Input 85 | 86 | import java.util.*; 87 | public class Main 88 | { 89 | public static void main(String[] args) 90 | { 91 | int n, i,j,k,count=1; 92 | Scanner in=new Scanner(System.in); 93 | n = in.nextInt(); 94 | if(n < 0) 95 | { 96 | System.out.print("Invalid array size"); 97 | System.exit(0); 98 | } 99 | else 100 | { 101 | int a[]=new int[100]; 102 | for(i = 0; i< n; i++) 103 | { 104 | a[i] = in.nextInt(); 105 | if(a[i] < 0) 106 | { 107 | System.out.print("Invalid input"); 108 | System.exit(0); 109 | } 110 | } 111 | for(i=0;i32767) 189 | { 190 | System.out.println ("Invalid Input"); 191 | System.exit(0); 192 | } 193 | 194 | else 195 | { 196 | while(n!=0) 197 | { 198 | rem=n%10; 199 | prod=prod*rem; 200 | n=n/10; 201 | } 202 | System.out.println(prod); 203 | } 204 | } 205 | } 206 | 207 | 5.findCricketerId 208 | Read the question carefully and follow the input and output format. 209 | 210 | Given an input array first Index indicates the cricketer’s id and second index indicates the score and so on……Find out the cricketer's id who scored more than given score 211 | 212 | Input and Output Format : 213 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements.The next line of the input consists of an integer that corresponds to the given score. Output consist of an integer array, which contains cricketer's id who have scored more than the given score. 214 | 215 | 1) Print "Invalid array size" when size of the array is negative and terminate the program . 216 | 2) Print "Invalid input" when there is any negative numbers available in the input array and terminate the program. 217 | 3) Print "Invalid score" when the score is negative. 218 | 219 | Include a function named findCricketerId(int array[], int size, int score) whose return type is void. 220 | The output array is stored in a global variable named cricketer. 221 | 222 | Sample Input 1: 223 | 6 224 | 1 225 | 1000 226 | 5 227 | 2000 228 | 3 229 | 4000 230 | 1000 231 | 232 | Sample Output 1: 233 | 5 234 | 3 235 | 236 | Sample Input 2: 237 | 6 238 | 1 239 | 1000 240 | 5 241 | 3000 242 | 3 243 | 4000 244 | -1000 245 | 246 | Sample Output 2: 247 | Invalid score 248 | import java.util.Scanner; 249 | public class Main 250 | { 251 | public static void main(String[] args) 252 | { 253 | int n, i,j=0,score; 254 | Scanner in=new Scanner(System.in); 255 | n = in.nextInt(); 256 | if(n < 0) 257 | { 258 | System.out.print("Invalid array size"); 259 | System.exit(0); 260 | } 261 | else 262 | { 263 | int a[]=new int[n]; 264 | for(i = 0; i< n; i++) 265 | { 266 | a[i] = in.nextInt(); 267 | if(a[i] < 0) 268 | { 269 | System.out.print("Invalid input"); 270 | System.exit(0); 271 | } 272 | } 273 | score = in.nextInt(); 274 | if(score<0) 275 | { 276 | System.out.print ("Invalid score"); 277 | System.exit(0); 278 | } 279 | int cricketer[]=new int[100]; 280 | for(i=1;iscore) 283 | { 284 | cricketer[j]=a [i-1]; 285 | j++; 286 | } 287 | } 288 | for(i=0;ib[j+1]) 439 | { 440 | c[k]=a[i]; 441 | c[++k]=a[i+1]; 442 | k++; 443 | } 444 | else 445 | { 446 | c[k]=a[i]; 447 | c[++k]=b[j+1]; 448 | k++; 449 | } 450 | } 451 | } 452 | if(count==0) 453 | { 454 | c[k]=a[i]; 455 | c[++k]=a[i+1]; 456 | k++; 457 | } 458 | } 459 | for(i=0;i 607 | #include 608 | int findElementCount(int,int[],int); 609 | int main(){ 610 | int n=0,flag=0,i,input[20],search=0; 611 | int count=0; 612 | scanf("%d",&n); 613 | if(n<0){ 614 | printf("Invalid Input"); 615 | getchar(); 616 | getchar(); 617 | exit(0); 618 | } 619 | for(i=0;i32767) 684 | { 685 | System.out.println ("Number too large "); 686 | System.exit(0); 687 | } 688 | 689 | else 690 | { 691 | if(n==0) 692 | { 693 | System.out.println(yes); 694 | System.exit(0); 695 | } 696 | 697 | while(n!=1) 698 | { 699 | if(n%2!=0) 700 | { 701 | System.out.println ("no "); 702 | System.exit(0); 703 | } 704 | n=n/2; 705 | } 706 | System.out.println("yes"); 707 | } 708 | } 709 | } 710 | 711 | 712 | 11.Count of 3 Multiples 713 | 714 | Write a program to find the count of 3 multiples in a given input integer array. 715 | 716 | Include a function named divisibleBy3 that accepts 2 arguments and returns an int. The first argument is the input array and the second argument is an int that corresponds to the size of the array. The function returns an int that corresponds to the count of 3 multiples. 717 | 718 | If the size of the array is negative or if any element in the array is negative, print “Invalid Input” and terminate the program. 719 | 720 | Input and Output Format: 721 | Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next 'n' integers correspond to the elements in the array. 722 | Output consists of an integer that corresponds to the count of 3 multiples 723 | 724 | Assume that the maximum number of elements in the array is 20. 725 | 726 | Sample Input 1: 727 | 8 728 | 1 729 | 6 730 | 3 731 | 5 732 | 61 733 | 80 734 | 102 735 | 9 736 | 737 | Sample Output 1: 738 | 4 739 | 740 | Sample Input 2: 741 | -5 742 | 743 | Sample Output 2: 744 | Invalid Input 745 | 746 | Sample Input 3: 747 | 5 748 | 23 749 | 2 750 | -200 751 | 752 | Sample Output 3: 753 | Invalid Input 754 | import java.util.Scanner; 755 | public class Main 756 | { 757 | public static void main(String[] args) 758 | { 759 | int n, i,count=0,flag=0; 760 | Scanner in=new Scanner(System.in); 761 | n = in.nextInt(); 762 | if(n < 0) 763 | { 764 | System.out.print("Invalid array size"); 765 | System.exit(0); 766 | } 767 | else 768 | { 769 | int a[]=new int[n]; 770 | for(i = 0; i< n; i++) 771 | { 772 | a[i] = in.nextInt(); 773 | if(a[i] < 0) 774 | { 775 | flag=1; 776 | System.out.print("Invalid input"); 777 | System.exit(0); 778 | } 779 | } 780 | if(flag!=1) 781 | { 782 | for(i=0;i11111) 913 | System.out.print("Invalid Input") ; 914 | else 915 | { 916 | binary = num; 917 | power = 0; 918 | while (binary != 0) 919 | { 920 | rem = binary % 10; 921 | a= Math.pow(2, power); 922 | decimal = decimal+ rem * a; 923 | power++; 924 | binary = binary / 10; 925 | } 926 | System.out.printf("%.0f",decimal); 927 | } 928 | } 929 | } 930 | 931 | 14.Arithmetic Operation 932 | 933 | Write a program to perform a specific arithmetic operation 934 | 935 | Include a function named performArithmeticOperation that accepts 3 integer arguments and returns an integer that corresponds to the result. The first and second arguments correspond to the input numbers and the third argument corresponds to the choice of arithmetic operation. 936 | 937 | If argument 3 =1, calculate the sum of input1 and input2 938 | If argument 3 =2, calculate the difference of input1 and input2 939 | If argument 3 =3, calculate the product of input1 and input2 940 | If argument 3 =4, calculate the quotient of input1 divided by input 2 941 | 942 | If the first two argument's values is negative or greater than 32767 , the function returns -1. 943 | If the third argument's value is not in the range 1 to 4, the function returns -1. 944 | 945 | If the function returns -1, print Invalid Input. 946 | 947 | Input and Output Format: 948 | Input consists of 3 integers. 949 | Output consists of an integer. 950 | Refer sample output for formatting specifications. 951 | 952 | 953 | Sample Input 1: 954 | 4 955 | 12 956 | 3 957 | 958 | Sample Output 1: 959 | 48 960 | 961 | Sample Input 2: 962 | -67 963 | 2 964 | 1 965 | 966 | Sample Output 2: 967 | Invalid Input 968 | 969 | import java.util.Scanner; 970 | public class Main 971 | { 972 | public static void main(String[] args) 973 | { 974 | int a,b,choice,result=0; 975 | Scanner in=new Scanner(System.in); 976 | a = in.nextInt(); 977 | b = in.nextInt(); 978 | choice = in.nextInt(); 979 | if(a<0 || b<0 || a>32767 || b>32767) 980 | System.out.print("Invalid input"); 981 | else if((choice<1)||(choice>4)) 982 | System.out.print("Invalid input"); 983 | else 984 | { 985 | switch(choice) 986 | { 987 | case 1: 988 | result = a+b; 989 | break; 990 | case 2: 991 | result = a-b; 992 | break; 993 | case 3: 994 | result = a*b; 995 | break; 996 | case 4: 997 | result = a/b; 998 | break; 999 | } 1000 | System.out.print(result); 1001 | } 1002 | } 1003 | } 1004 | 15.digitFactorial 1005 | Read the question carefully and follow the input and output format. 1006 | 1007 | In a given input number , find out the factorial of each individual digit and assign it to output array. 1008 | 1009 | Input and Output Format: 1010 | Input consists of a single integer. Output consists of an Integer array, the individual factorials. 1011 | 1012 | Print "Number too large" when the given input numbers is greater than 32767 . 1013 | Print "Number too small" when the given input is a negative number. 1014 | 1015 | Include a function named digitFactorial(int number) whose return type is void. 1016 | The output array is stored in a global variable named factorial. 1017 | 1018 | Sample Input 1: 1019 | 123 1020 | 1021 | Sample Output 1: 1022 | 1 1023 | 2 1024 | 6 1025 | 1026 | 1027 | Sample Input 2: 1028 | -2526 1029 | 1030 | Sample Output 2: 1031 | Number too small 1032 | import java.util.Scanner; 1033 | public class Main 1034 | { 1035 | public static void main(String[] args) 1036 | { 1037 | int n,i,fact,k=0,rem; 1038 | Scanner in=new Scanner(System.in); 1039 | n = in.nextInt(); 1040 | if(n<0) 1041 | { 1042 | System.out.println ("Number too small"); 1043 | System.exit(0); 1044 | } 1045 | if(n>32767) 1046 | { 1047 | System.out.println ("Number too large"); 1048 | System.exit(0); 1049 | 1050 | } 1051 | int factorial[]=new int[100]; 1052 | while(n!=0) 1053 | { 1054 | rem=n%10; 1055 | fact=1; 1056 | for(i=1;i<=rem;i++) 1057 | { 1058 | fact = fact * i; 1059 | } 1060 | factorial[k]=fact; 1061 | k++; 1062 | n=n/10; 1063 | } 1064 | for(i=k-1;i>=0;i--) 1065 | System.out.println(factorial[i]); 1066 | } 1067 | } 1068 | 1069 | 16.searchKeys 1070 | Read the question carefully and follow the input and output format. 1071 | 1072 | Given an integer array, first index represents the key & second index represents the value. Find keys for the given value. 1073 | 1074 | Input and Output Format: 1075 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements. The next line consistts of an integer that represents the value to be searched. 1076 | Output consist of an integer array. 1077 | 1078 | 1) Print "Invalid array size" when size of the array is negative and terminate the program. 1079 | 2) Print "Invalid input" when there is any negative numbers available in the input array and terminate the program. 1080 | 3) Print "Key not found" when there is no keys found. 1081 | 1082 | Include a function named searchKeys(int array[], int size) whose return type is void. 1083 | The output array is stored in a global variable named found. 1084 | 1085 | Sample Input 1: 1086 | 8 1087 | 1 1088 | 4 1089 | 2 1090 | 4 1091 | 3 1092 | 4 1093 | 5 1094 | 6 1095 | 4 1096 | 1097 | Sample Output 1: 1098 | 1 1099 | 2 1100 | 3 1101 | 1102 | Sample Input 2: 1103 | 5 1104 | 5 1105 | 6 1106 | 7 1107 | 8 1108 | 9 1109 | -5 1110 | 1111 | Sample Output 2: 1112 | Key not found 1113 | import java.util.Scanner; 1114 | public class Main 1115 | { 1116 | public static void main(String[] args) 1117 | { 1118 | int n, i,k=0,se,flag=0,f=0; 1119 | Scanner in=new Scanner(System.in); 1120 | n = in.nextInt(); 1121 | if(n < 0) 1122 | { 1123 | System.out.print("Invalid array size"); 1124 | System.exit(0); 1125 | } 1126 | else 1127 | { 1128 | int a[]=new int[n]; 1129 | for(i = 0; i< n; i++) 1130 | { 1131 | a[i] = in.nextInt(); 1132 | if(a[i] < 0) 1133 | { 1134 | System.out.print("Invalid input"); 1135 | System.exit(0); 1136 | } 1137 | } 1138 | se=in.nextInt(); 1139 | if(se<0) 1140 | { 1141 | System.out.print("Key not found"); 1142 | System.exit(0); 1143 | } 1144 | int found[]=new int[100]; 1145 | for(i=0;i=70 then He /she Cleared the exam. Array size is always even. 1170 | 1171 | Input and Output Format : 1172 | 1173 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements. Output consist of an integer, 1174 | 1175 | 1) Print "Invalid array size" when size of the array is a negative number and terminate the program. 1176 | 2) Print "Invalid input" when there is any negative number available in the input array and terminate the program. 1177 | 1178 | Include a function named passCount(int array[], int size) whose return type is an integer , the count. 1179 | 1180 | Sample Input 1: 1181 | 8 1182 | 1 1183 | 70 1184 | 2 1185 | 55 1186 | 3 1187 | 75 1188 | 4 1189 | 80 1190 | 1191 | Sample Output 1: 1192 | 3 1193 | 1194 | Sample Input 2: 1195 | 5 1196 | 6 1197 | 2 1198 | 8 1199 | -2 1200 | Sample Output 2: 1201 | Invalid input 1202 | 1203 | import java.util.Scanner; 1204 | public class Main 1205 | { 1206 | public static void main(String[] args) 1207 | { 1208 | int n, i,count=0; 1209 | Scanner in=new Scanner(System.in); 1210 | n = in.nextInt(); 1211 | if(n < 0 || n%2!=0) 1212 | { 1213 | System.out.print("Invalid array size"); 1214 | System.exit(0); 1215 | } 1216 | else 1217 | { 1218 | int a[]=new int[n]; 1219 | for(i = 0; i< n; i++) 1220 | { 1221 | a[i] = in.nextInt(); 1222 | if(a[i] < 0) 1223 | { 1224 | 1225 | System.out.print("Invalid input"); 1226 | System.exit(0); 1227 | } 1228 | } 1229 | for(i=1;i=70) 1232 | count++; 1233 | } 1234 | System.out.print(count); 1235 | } 1236 | } 1237 | } 1238 | 18.5 Multiples --- Average 1239 | 1240 | Write a program to find the average of multiples of 5 upto 'n'. n is given as input. 1241 | 1242 | Include a function named findAverageBy5s that accepts an integer argument and returns a float that corresponds to the average of multiples of 5. 1243 | 1244 | If the input value is negative or greater than 32767, print Invalid Input and terminate the program. 1245 | 1246 | Input and Output Format: 1247 | Input consists of a single integer. 1248 | Output consists of a floating point number. Output is displayed correct to 2 decimal places. 1249 | Refer sample output for formatting specifications. 1250 | 1251 | 1252 | Sample Input 1: 1253 | 10 1254 | 1255 | Sample Output 1: 1256 | 7.50 1257 | 1258 | Sample Input 2: 1259 | -67 1260 | 1261 | Sample Output 2: 1262 | Invalid Input 1263 | 1264 | import java.util.Scanner; 1265 | public class Main 1266 | { 1267 | public static void main(String[] args) 1268 | { 1269 | int n,i,count=0; 1270 | double sum=0,avg; 1271 | Scanner in=new Scanner(System.in); 1272 | n = in.nextInt(); 1273 | if(n<0 || n>32767) 1274 | { 1275 | System.out.println ("Number too small"); 1276 | System.exit(0); 1277 | } 1278 | for(i=5;i<=n;i++) 1279 | { 1280 | if(i%5==0) 1281 | { 1282 | sum=sum+i; 1283 | count++; 1284 | } 1285 | } 1286 | avg = sum/count; 1287 | System.out.printf("%.2f",avg); 1288 | } 1289 | } 1290 | 1291 | 19.Sum of Odd Digits 1292 | 1293 | Write a program to find the sum of the odd digits in a number. 1294 | 1295 | Include a function named sumOddDigits that accepts an integer argument and returns an integer that corresponds to the sum of the odd digits. The function returns -1 if the input is less than zero or if it is greater than 32767. 1296 | 1297 | If the function returns -1, print “Invalid Input”. 1298 | 1299 | Input and Output Format: 1300 | 1301 | The input consists of an integer. 1302 | The output consists of an integer that corresponds to the sum of the odd digits in the number. 1303 | 1304 | Sample Input 1: 1305 | 3487 1306 | 1307 | Sample Ouput 1: 1308 | 10 1309 | 1310 | Sample Input 2: 1311 | -8 1312 | 1313 | Sample Output 2: 1314 | Invalid Input 1315 | 1316 | import java.util.Scanner; 1317 | public class Main 1318 | { 1319 | public static void main(String[] args) 1320 | { 1321 | int n,i,rem,sum=0; 1322 | Scanner in=new Scanner(System.in); 1323 | n = in.nextInt(); 1324 | if(n<0 || n>32767) 1325 | { 1326 | System.out.println ("Number too small"); 1327 | System.exit(0); 1328 | } 1329 | else 1330 | { 1331 | while(n!=0) 1332 | { 1333 | rem=n%10; 1334 | if(rem%2!=0) 1335 | sum=sum+rem; 1336 | n=n/10; 1337 | } 1338 | System.out.println (sum); 1339 | } 1340 | } 1341 | } 1342 | 20.Largest Array 1343 | 1344 | Write a program which takes two arrays of the same size as a input and compares the first element of first array with the first element of second array and stores the largest of these into the first element of the output array. Repeat the process till the last element of the first array is checked with the last element of the second array. 1345 | 1346 | Include a function named largestArray that accepts 3 arguments and its return type is void. The first argument is input array 1, the second argument is input array 2 and the third argument is an int that corresponds to the size of the array. The output array is stored in a global variable named output1. 1347 | 1348 | If the size of the array is negative or if any element in the array is negative, print “Invalid Input” and terminate the program. 1349 | 1350 | Input and Output Format: 1351 | Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next 'n' integers correspond to the elements in the array. 1352 | Output consists of the largest array. 1353 | Assume that the maximum number of elements in the array is 20. 1354 | 1355 | Sample Input 1: 1356 | 4 1357 | 2 1358 | 1 1359 | 3 1360 | 4 1361 | 1 1362 | 9 1363 | 2 1364 | 8 1365 | 1366 | Sample Output 1: 1367 | 2 1368 | 9 1369 | 3 1370 | 8 1371 | 1372 | 1373 | Sample Input 2: 1374 | -5 1375 | 1376 | Sample Output 2: 1377 | Invalid Input 1378 | 1379 | Sample Input 3: 1380 | 5 1381 | 23 1382 | 2 1383 | -200 1384 | 1385 | Sample Output 3: 1386 | Invalid Input 1387 | import java.util.Scanner; 1388 | public class Main 1389 | { 1390 | public static void main(String[] args) 1391 | { 1392 | int n,i,j,flag=0; 1393 | Scanner in=new Scanner(System.in); 1394 | n=in.nextInt(); 1395 | if(n < 0) 1396 | { 1397 | System.out.print("Invalid array size"); 1398 | System.exit(0); 1399 | } 1400 | else 1401 | { 1402 | int a[]=new int[n]; 1403 | for(i = 0; i< n; i++) 1404 | { 1405 | a[i] = in.nextInt(); 1406 | if(a[i] < 0) 1407 | { 1408 | System.out.print("Invalid input"); 1409 | System.exit(0); 1410 | } 1411 | } 1412 | int b[]=new int[n]; 1413 | for(i = 0; i< n; i++) 1414 | { 1415 | b[i] = in.nextInt(); 1416 | if(b[i] < 0) 1417 | { 1418 | System.out.print("Invalid input"); 1419 | System.exit(0); 1420 | } 1421 | } 1422 | int output1[]=new int[100]; 1423 | for(i=0;ib[i]) 1426 | { 1427 | output1[i]=a [i]; 1428 | } 1429 | else 1430 | { 1431 | output1[i]=b[i]; 1432 | } 1433 | } 1434 | for(i=0;i32767) 1784 | { 1785 | System.out.println ("Number too large"); 1786 | System.exit(0); 1787 | } 1788 | while(n!=0) 1789 | { 1790 | rem=n%10; 1791 | if(rem%2==0) 1792 | { 1793 | res=res+(rem*i); 1794 | i=i*10; 1795 | } 1796 | n=n/10; 1797 | } 1798 | System.out.println(res); 1799 | } 1800 | } 1801 | 1802 | 26.calculateBonus 1803 | Read the question carefully and follow the input and output format. 1804 | 1805 | Given the basic salary as input, write a program to calculate the bonus and display it. 1806 | 1807 | The bonus will be calculated based on the below category. 1808 | Basic>20000 bonus=17%of basic+1500 1809 | Basic>15000 bonus=15%of basic+1200 1810 | Basic>10000 bonus=12%of basic+1000 1811 | for rest =8%of basic+500 1812 | 1813 | Input and Output Format : 1814 | 1815 | First line of input consists of n, the basic salary. 1816 | Output is a single integer that displays the bonus. 1817 | 1818 | Print "Number too large" when the given input numbers is greater than 32767 . 1819 | Print "Number too small" when the given input is a negative number. 1820 | 1821 | Include a function named calculateBonus(int basic) whose return type is an integer, the bonus. 1822 | 1823 | Sample Input 1: 1824 | 21000 1825 | 1826 | Sample Output 1: 1827 | 5070 1828 | 1829 | Sample Input 2: 1830 | 327678 1831 | Sample Output 2: 1832 | Number too large 1833 | import java.util.Scanner; 1834 | public class Main 1835 | { 1836 | public static void main(String[] args) 1837 | { 1838 | int n,res=0; 1839 | Scanner in=new Scanner(System.in); 1840 | n = in.nextInt(); 1841 | if(n<0) 1842 | { 1843 | System.out.println ("Number too small"); 1844 | System.exit(0); 1845 | } 1846 | if(n>32767) 1847 | { 1848 | System.out.println ("Number too large"); 1849 | System.exit(0); 1850 | } 1851 | if(n>20000) 1852 | res=(n*17/100)+1500; 1853 | else if(n>15000) 1854 | res=(n*15/100)+1200; 1855 | else if(n>10000) 1856 | res=(n*12/100)+1000; 1857 | else 1858 | res=(n*8/100)+500; 1859 | System.out.println(res); 1860 | } 1861 | } 1862 | 27.Minimum of 3 1863 | Write a program to find the minimum of 3 numbers. 1864 | Include a function named findSmallest that accepts 3 integer arguments and returns an integer that corresponds to the minimum value. 1865 | Input and Output Format: 1866 | Input consists of 3 integers. 1867 | Output consists of an integer. 1868 | Refer sample output for formatting specifications. 1869 | Sample Input 1: 1870 | 4 1871 | 12 1872 | 3 1873 | Sample Output 1: 1874 | 3 1875 | import java.util.Scanner; 1876 | public class Main 1877 | { 1878 | public static void main(String[] args) 1879 | { 1880 | int a,b,c; 1881 | Scanner in=new Scanner(System.in); 1882 | a = in.nextInt(); 1883 | b = in.nextInt(); 1884 | c = in.nextInt(); 1885 | if(aoutput1[j]) 1966 | { 1967 | temp = output1[j]; 1968 | output1[j] = output1[i]; 1969 | output1[i] = temp; 1970 | } 1971 | } 1972 | } 1973 | for(i=0;i32767) 2021 | { 2022 | System.out.println ("Number too large"); 2023 | System.exit(0); 2024 | } 2025 | while(n!=0) 2026 | { 2027 | rem=n%10; 2028 | if(rem%2==0) 2029 | { 2030 | res=(rem+2)*i+res; 2031 | i=i*10; 2032 | } 2033 | else 2034 | { 2035 | res=(rem+2)*i+res; 2036 | i=i*10; 2037 | } 2038 | n = n/10; 2039 | } 2040 | System.out.println (res); 2041 | } 2042 | } 2043 | 30.findMileage 2044 | Read the question carefully and follow the input and output format. 2045 | 2046 | Given the cubic capacity(CC) of a bike. Write a function to return the mileage/liter for the given Cubic Capacity(CC). The mileage will be calculated as follows: 2047 | 2048 | if CC is between 100 and 125, mileage is 75 2049 | if CC is between 126 and 135, mileage is 70 2050 | if CC is between 136 and 150, mileage is 60 2051 | if CC is between 151 and 200, mileage is 50 2052 | if CC is between 201 and 220, mileage is 35 2053 | 2054 | First line of input consists of an integer that corresponds to CC of a bike. Output consist of an integer, which is the mileage. 2055 | 2056 | Print "Number too large" when the given input CC is greater than 220. 2057 | Print "Number too small" when the given input CC is less than 100. 2058 | 2059 | Include a function named findMileage(int cc) whose return type is an integer, which is the mileage. 2060 | 2061 | Sample Input 1: 2062 | 1 2063 | 2064 | Sample Output 1: 2065 | Number too small 2066 | 2067 | Sample Input 2: 2068 | 160 2069 | 2070 | Sample Output 2: 2071 | 50 2072 | import java.util.*; 2073 | public class Main 2074 | { 2075 | public static void main(String[] args) 2076 | { 2077 | int cc,mil; 2078 | Scanner in=new Scanner(System.in); 2079 | cc = in.nextInt(); 2080 | if(cc<100) 2081 | System.out.println ("Number too small"); 2082 | else if(cc>220) 2083 | System.out.println ("Number too large"); 2084 | else 2085 | { 2086 | if(cc>=100 && cc<=125) 2087 | mil=75; 2088 | else if(cc>=126 && cc<=135) 2089 | mil=70; 2090 | else if(cc>=136 && cc<=150) 2091 | mil=60; 2092 | else if(cc>=151 && cc<=200) 2093 | mil=50; 2094 | else 2095 | mil=35; 2096 | System.out.println (mil); 2097 | } 2098 | } 2099 | } 2100 | 2101 | 2102 | 31.sumPrimeArray 2103 | Read the question carefully and follow the input and output format. 2104 | 2105 | John is working in a bank. He has created account details transaction in a file and protected it with a password. He sent the file to his manager for review. The file is protected with a password. The password is the sum of Prime numbers. Write a function to generate the password. 2106 | 2107 | Input and Output Format: 2108 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements. Output consist of an integer, which is the sum. 2109 | 2110 | 1) Print "Invalid array size" when size of the array is a negative number and terminate the program. 2111 | 2) Print "Invalid input" when there is any negative number available in the input array and terminate the program. 2112 | 3) Print 0, when there are no prime numbers in a given input array. 2113 | 2114 | Include a function named sumPrimeArray(int array[], int size) whose return type is an integer, which is the prime sum. 2115 | 2116 | Sample Input 1: 2117 | 5 2118 | 1 2119 | 2 2120 | 3 2121 | 4 2122 | 5 2123 | 2124 | Sample Output 1: 2125 | 10 2126 | 2127 | Sample Input 2: 2128 | 3 2129 | 4 2130 | 8 2131 | 9 2132 | 2133 | Sample Output 2: 2134 | 0 2135 | import java.util.Scanner; 2136 | public class Main 2137 | { 2138 | public static void main(String[] args) 2139 | { 2140 | int n, i,j, sum=0,count=0; 2141 | // int flag=0; 2142 | Scanner in=new Scanner(System.in); 2143 | n = in.nextInt(); 2144 | if(n < 0) 2145 | { 2146 | System.out.print("Invalid array size"); 2147 | System.exit(0); 2148 | } 2149 | else 2150 | { 2151 | int a[]=new int[n]; 2152 | for(i = 0; i< n; i++) 2153 | { 2154 | a[i] = in.nextInt(); 2155 | if(a[i] < 0) 2156 | { 2157 | //flag=1; 2158 | System.out.print("Invalid input"); 2159 | System.exit(0); 2160 | } 2161 | } 2162 | //if(flag!=1) 2163 | // { 2164 | for(i=0;i18) 2486 | sum=sum+a[i]; 2487 | } 2488 | System.out.print(sum); 2489 | // } 2490 | } 2491 | } 2492 | } 2493 | 36.Array Product 2494 | Write a program to find the product of positive/nonnegative elements in a given array. 2495 | Include a function named calculateProduct that accepts 2 arguments and returns an int. The first argument is the input array and the second argument is an int that corresponds to the size of the array. The function returns an int that corresponds to the product. 2496 | If the size of the array is negative or if it is greater than 10 or if any element in the array is more than 2 digits, print “Invalid Input” and terminate the program. 2497 | Input and Output Format: 2498 | Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next 'n' integers correspond to the elements in the array. 2499 | Output consists of an integer that corresponds to the product of positive numbers in the array. 2500 | 2501 | Sample Input 1: 2502 | 8 2503 | 1 2504 | -2 2505 | 3 2506 | 4 2507 | -6 2508 | 8 2509 | 10 2510 | -6 2511 | 2512 | Sample Output 1: 2513 | 960 2514 | 2515 | Sample Input 2: 2516 | -5 2517 | 2518 | Sample Output 2: 2519 | Invalid Input 2520 | 2521 | Sample Input 3: 2522 | 5 2523 | 23 2524 | 2 2525 | 200 2526 | 2527 | Sample Output 3: 2528 | Invalid Input 2529 | 2530 | import java.util.Scanner; 2531 | public class Main 2532 | { 2533 | public static void main(String[] args) 2534 | { 2535 | int n,i, prod=1; 2536 | // int flag=0; 2537 | Scanner in=new Scanner(System.in); 2538 | n = in.nextInt(); 2539 | if(n < 0 || n>10) 2540 | { 2541 | System.out.print("Invalid array size"); 2542 | System.exit(0); 2543 | } 2544 | else 2545 | { 2546 | int a[]=new int[n]; 2547 | for(i = 0; i< n; i++) 2548 | { 2549 | a[i] = in.nextInt(); 2550 | if(a[i]>=100 || a[i]<=-100) 2551 | { 2552 | //flag=1; 2553 | System.out.print("Invalid Input"); 2554 | System.exit(0); 2555 | } 2556 | } 2557 | //if(flag!=1) 2558 | //{ 2559 | for(i=0;i0) 2562 | prod=prod*a[i]; 2563 | } 2564 | System.out.print(prod); 2565 | //} 2566 | } 2567 | } 2568 | } 2569 | 2570 | 37.Perfect Number 2571 | 2572 | Write a program to find whether the given number is a perfect Number. 2573 | 2574 | A number is a perfect number if the sum of the proper divisors of the number is equal to the number itself. 2575 | 2576 | Include a function named findPerfect that accepts an integer argument and returns an integer. The function returns 2577 | 1. 1 if the input is a Perfect Number 2578 | 2. 0 if the input is not a Perfect Number 2579 | 3. -1 if the input is a negative number or if it is greater than 32767 2580 | 2581 | Input and Output Format: 2582 | Input consists of a single integer. 2583 | Output consists of a string. 2584 | Refer sample output for formatting specifications. 2585 | 2586 | 2587 | Sample Input 1: 2588 | 6 2589 | 2590 | Sample Output 1: 2591 | yes 2592 | 2593 | Sample Input 2: 2594 | 241 2595 | 2596 | Sample Output 2: 2597 | no 2598 | 2599 | Sample Input 3: 2600 | -9 2601 | 2602 | Sample Output 3: 2603 | Invalid Input 2604 | 2605 | import java.util.Scanner; 2606 | public class Main 2607 | { 2608 | public static void main(String[] args) 2609 | { 2610 | int n,i,sum=0; 2611 | // int flag=0; 2612 | Scanner in=new Scanner(System.in); 2613 | n = in.nextInt(); 2614 | if(n<0) 2615 | { 2616 | System.out.println("Number too small"); 2617 | // flag=1; 2618 | System.exit(0); 2619 | } 2620 | if(n>32767) 2621 | { 2622 | System.out.println("Number too large"); 2623 | // flag=1; 2624 | System.exit(0); 2625 | } 2626 | // if(flag==0) 2627 | // { 2628 | for(i=1;i32767) 2670 | { 2671 | System.out.println ("Number too large"); 2672 | System.exit(0); 2673 | } 2674 | while(n!=0) 2675 | { 2676 | rem=n%10; 2677 | if(rem%2==0) 2678 | res=res+(rem*rem); 2679 | n=n/10; 2680 | } 2681 | System.out.println(res); 2682 | } 2683 | } 2684 | 2685 | 2686 | 2687 | 39.sumPrimeArray 2688 | Read the question carefully and follow the input and output format. 2689 | 2690 | John is working in a bank. He has created account details transaction in a file and protected it with a password. He sent the file to his manager for review. The file is protected with a password. The password is the sum of Prime numbers. Write a function to generate the password. 2691 | 2692 | Input and Output Format: 2693 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements. Output consist of an integer, which is the sum. 2694 | 2695 | 1) Print "Invalid array size" when size of the array is a negative number and terminate the program. 2696 | 2) Print "Invalid input" when there is any negative number available in the input array and terminate the program. 2697 | 3) Print 0, when there are no prime numbers in a given input array. 2698 | 2699 | Include a function named sumPrimeArray(int array[], int size) whose return type is an integer, which is the prime sum. 2700 | 2701 | Sample Input 1: 2702 | 5 2703 | 1 2704 | 2 2705 | 3 2706 | 4 2707 | 5 2708 | 2709 | Sample Output 1: 2710 | 10 2711 | 2712 | Sample Input 2: 2713 | 3 2714 | 4 2715 | 8 2716 | 9 2717 | 2718 | Sample Output 2: 2719 | 0 2720 | import java.util.Scanner; 2721 | public class Main 2722 | { 2723 | public static void main(String[] args) 2724 | { 2725 | int n, i,j, sum=0,count=0; 2726 | // int flag=0; 2727 | Scanner in=new Scanner(System.in); 2728 | n = in.nextInt(); 2729 | if(n < 0) 2730 | { 2731 | System.out.print("Invalid array size"); 2732 | System.exit(0); 2733 | } 2734 | else 2735 | { 2736 | int a[]=new int[n]; 2737 | for(i = 0; i< n; i++) 2738 | { 2739 | a[i] = in.nextInt(); 2740 | if(a[i] < 0) 2741 | { 2742 | // flag=1; 2743 | System.out.print("Invalid input"); 2744 | System.exit(0); 2745 | } 2746 | } 2747 | //if(flag!=1) 2748 | //{ 2749 | for(i=0;i=0;i++,j--) 2847 | { 2848 | c[i] = a[i]*b[j]; 2849 | System.out.println(c[i]); 2850 | } 2851 | //} 2852 | } 2853 | } 2854 | } 2855 | 2856 | 41.University Type 2857 | Write a program to find if the student is eligible for first, second or third grade universities by finding the average of their marks given in the input integer array. 2858 | Grade should be calculated as given below : 2859 | Average >80 First Grade University 2860 | Average >60 Second Grade University 2861 | Otherwise Third Grade University 2862 | Include a function named calculateGrade that accepts 2 arguments and returns an integer. The first argument is the input array and the second argument is an int that corresponds to the size of the array. The function returns an integer that corresponds to the university type. The function returns 1 if the student is eligible for First Grade univesity, returns 2 if the student is eligible for Second Grade University, returns 3 if the student is eligible for Third Grade University and returns -1 if the average is greater than 99. 2863 | If the size of the array is negative or if any element in the array is negative or if the average marks scored by the student is greater than 99 , print “Invalid Input” and terminate the program. 2864 | Input and Output Format: 2865 | Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next 'n' integers correspond to the elements in the array. 2866 | Output consists of a string --- “First Grade University” or “Second Grade University” or “Third Grade University” or “Invalid Input”. 2867 | Assume that the maximum size of the array is 20. 2868 | Sample Input 1: 2869 | 5 2870 | 92 2871 | 87 2872 | 78 2873 | 74 2874 | 80 2875 | 2876 | Sample Output 1: 2877 | First Grade University 2878 | Sample Input 2: 2879 | -5 2880 | Sample Output 2: 2881 | Invalid Input 2882 | Sample Input 3: 2883 | 5 2884 | 23 2885 | 2 2886 | -5 2887 | Sample Output 3: 2888 | Invalid Input 2889 | import java.util.Scanner; 2890 | public class Main 2891 | { 2892 | public static void main(String[] args) 2893 | { 2894 | int n, i, sum=0,avg=0; 2895 | // int flag=0; 2896 | Scanner in=new Scanner(System.in); 2897 | n = in.nextInt(); 2898 | if(n < 0) 2899 | { 2900 | System.out.print("Invalid array size"); 2901 | System.exit(0); 2902 | } 2903 | else 2904 | { 2905 | int a[]=new int[n]; 2906 | for(i = 0; i< n; i++) 2907 | { 2908 | a[i] = in.nextInt(); 2909 | if(a[i] < 0) 2910 | { 2911 | //flag=1; 2912 | System.out.print("Invalid input"); 2913 | System.exit(0); 2914 | } 2915 | } 2916 | //if(flag!=1) 2917 | //{ 2918 | for(i=0;i99) 2922 | System.out.print ("Invalid Input"); 2923 | else if(avg>80) 2924 | System.out.print ("First Grade University"); 2925 | else if(avg>60) 2926 | System.out.print ("Second Grade University"); 2927 | else 2928 | System.out.print ("Third Grade University"); 2929 | //} 2930 | } 2931 | } 2932 | } 2933 | 2934 | 42.Interchange Array 2935 | Write a program to interchange the first element in the array with the last element in the array. Repeat the process till the middle of the array. 2936 | Include a function named interchangeArray that accepts 2 arguments and its return type is void. The first argument is the input array and the second argument is an int that corresponds to the size of the array. 2937 | If the size of the array is negative or if any element in the array is negative, print “Invalid Input” and terminate the program. 2938 | Input and Output Format: 2939 | Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next 'n' integers correspond to the elements in the array. 2940 | Output consists of the interchanged array. 2941 | Assume that the maximum number of elements in the array is 20. 2942 | Sample Input 1: 2943 | 4 2944 | 2 2945 | 1 2946 | 3 2947 | 4 2948 | 2949 | Sample Output 1: 2950 | 4 2951 | 3 2952 | 1 2953 | 2 2954 | Sample Input 2: 2955 | -5 2956 | Sample Output 2: 2957 | Invalid Input 2958 | Sample Input 3: 2959 | 5 2960 | 23 2961 | 2 2962 | -200 2963 | Sample Output 3: 2964 | Invalid Input 2965 | 2966 | import java.util.Scanner; 2967 | public class Main 2968 | { 2969 | public static void main(String[] args) 2970 | { 2971 | int n, i,j,temp=0; 2972 | // int flag=0; 2973 | Scanner in=new Scanner(System.in); 2974 | n = in.nextInt(); 2975 | if(n < 0) 2976 | { 2977 | System.out.print("Invalid array size"); 2978 | System.exit(0); 2979 | } 2980 | else 2981 | { 2982 | int a[]=new int[n]; 2983 | for(i = 0; i< n; i++) 2984 | { 2985 | a[i] = in.nextInt(); 2986 | if(a[i] < 0) 2987 | { 2988 | // flag=1; 2989 | System.out.print("Invalid input"); 2990 | System.exit(0); 2991 | } 2992 | } 2993 | // if(flag!=1) 2994 | //{ 2995 | for(i=0,j=n-1;i999) 3041 | { 3042 | System.out.println ("Invalid Input"); 3043 | flag=1; 3044 | } 3045 | // if(flag==0) 3046 | { 3047 | temp=n; 3048 | while (n != 0) 3049 | { 3050 | rem = n%10; 3051 | sum = sum + (rem*rem*rem); 3052 | n = n/10; 3053 | } 3054 | if (temp == sum) 3055 | System.out.println("yes"); 3056 | else 3057 | System.out.println("no"); 3058 | //} 3059 | } 3060 | } 3061 | 3062 | 44.Factorial 3063 | Write a program to find the factorial of a given number. 3064 | Include a function named findFactorial that accepts an integer argument and returns an integer that corresponds to factorial. If the input value is negative or greater than 10, the function returns -1. 3065 | If the function returns -1, print Invalid Input. 3066 | Input and Output Format: 3067 | Input consists of a single integer. 3068 | Output consists of an integer. 3069 | Refer sample output for formatting specifications. 3070 | Sample Input 1: 3071 | 4 3072 | Sample Output 1: 3073 | 24 3074 | Sample Input 2: 3075 | -67 3076 | Sample Output 2: 3077 | Invalid Input 3078 | 3079 | import java.util.Scanner; 3080 | public class Main 3081 | { 3082 | public static void main(String[] args) 3083 | { 3084 | int n, fact=1,i; 3085 | // int flag=0; 3086 | Scanner in=new Scanner(System.in); 3087 | n = in.nextInt(); 3088 | if(n<0||n>10) 3089 | { 3090 | System.out.println ("Invalid Input"); 3091 | flag=1; 3092 | } 3093 | //if(flag==0) 3094 | //{ 3095 | for(i=1;i<=n;i++) 3096 | { 3097 | fact = fact * i; 3098 | } 3099 | System.out.println (fact); 3100 | //} 3101 | } 3102 | } 3103 | 3104 | 45.Salary Calculation 3105 | Jim got his salary. His salary calculations are as follows. 3106 | From his Basic amount he gets 50% of his basic for house Rent allowances and 75% of his basic as special allowances . If the number of days he worked is 31 he gets 500 extra. Write a program to calculate his gross salary after calculating all his salary split up. 3107 | 3108 | Include a function named calculateGross that accepts 2 integer arguments and returns a float. The first integer corresponds to Jim's basic salary and the second integer corresponds to the number of days Jim has worked. The function returns a float that corresponds to the gross salary. 3109 | Print Invalid Input and terminate the program in the following cases: 3110 | 1. Basic salary is greater than 10000 3111 | 2. Number of working days is greater than 31 3112 | 3. Basic salary is negative 3113 | 4. Number of working days is 0 or negative 3114 | Input and Output Format: 3115 | Input consists of 2 integers. The first integer corresponds to Jim's basic salary and the second integer corresponds to the number of days he has worked. 3116 | Output consists of a single float that corresponds to Jim's gross salary. The gross salary is displayed correct to 2 decimal places. 3117 | Sample Input 1: 3118 | 5000 3119 | 30 3120 | Sample Output 1: 3121 | 11250 .00 3122 | Sample Input 2: 3123 | 5000 3124 | 0 3125 | Sample Output 2: 3126 | Invalid Input 3127 | import java.util.Scanner; 3128 | public class Main 3129 | { 3130 | public static void main(String[] args) 3131 | { 3132 | int basic=0,days=0; 3133 | double salary; 3134 | Scanner in=new Scanner(System.in); 3135 | basic=in.nextInt(); 3136 | days= in.nextInt(); 3137 | if(basic>10000 || days>31 || basic<0 || days==0 || days<0) 3138 | { 3139 | System.out.println("Invalid Input"); 3140 | System.exit(0); 3141 | } 3142 | if(days==31) 3143 | salary=(basic*50)/100+(basic*75)/100+basic+500; 3144 | else 3145 | salary=(basic*50)/100+(basic*75)/100+basic; 3146 | System.out.printf("%.2f",salary); 3147 | } 3148 | } 3149 | 3150 | 46.Perfect Square 3151 | Write a program to find whether the given input number is a perfect square without using sqrt function. 3152 | Include a function named checkPerfectSquare that accepts an integer and returns an integer. The function returns 3153 | 1. 1 if the input is a perfect square 3154 | 2. 0 if the input is not a perfect square 3155 | 3. -1 if the input is a negative number 3156 | Print Invalid Input if the function returns -1. 3157 | Input and Output Format: 3158 | Input consists of a single integer. 3159 | Refer sample output for formatting specifications. 3160 | Sample Input 1: 3161 | 36 3162 | Sample Output 1: 3163 | yes 3164 | Sample Input 2: 3165 | 40 3166 | Sample Output 2: 3167 | no 3168 | Sample Input 3: 3169 | -2345 3170 | 3171 | Sample Output 3: 3172 | Invalid Input 3173 | 3174 | import java.util.Scanner; 3175 | public class Main 3176 | { 3177 | public static void main(String[] args) 3178 | { 3179 | int n, i,fact=1; 3180 | // int flag=0; 3181 | Scanner in=new Scanner(System.in); 3182 | n = in.nextInt(); 3183 | if(n<0) 3184 | { 3185 | System.out.println ("Invalid Input"); 3186 | //flag=1; 3187 | System.exit(0); 3188 | } 3189 | // if(flag==0) 3190 | // { 3191 | for(i=1;i<=n;i++) 3192 | { 3193 | fact=fact*i; 3194 | if(fact==n) 3195 | { 3196 | System.out.println("yes"); 3197 | System.exit(0); 3198 | } 3199 | } 3200 | System.out.println("no"); 3201 | // } 3202 | } 3203 | } 3204 | 3205 | 47.Find Index 3206 | Write a program to find the index of a particular number in a given input array. 3207 | 3208 | Include a function named findIndex that accepts 3 arguments and returns an int. The first argument is the input array, the second argument is an int that corresponds to the size of the array and the third argument is the element to be searched for. The function returns the corresponding index if the search element is present in the array and returns -1 if the search element is not present in the array. 3209 | 3210 | If the size of the array is negative or if any element in the array is negative, print “Invalid Input” and terminate the program. 3211 | 3212 | Input and Output Format: 3213 | Input consists of n+2 integers. The first integer corresponds to n, the number of elements in the array. The next 'n' integers correspond to the elements in the array. 3214 | The last integer corresponds to the element whose count needs to be found. 3215 | Output consists of an integer that corresponds to the index of the search element if it is present. 3216 | 3217 | Else, print 'not found'. 3218 | 3219 | Refer sample output for formatting specifications. 3220 | 3221 | Assume that the maximum number of elements in the array is 20 and that all elements in the array are unique. 3222 | 3223 | Sample Input 1: 3224 | 8 3225 | 2 3226 | 1 3227 | 3 3228 | 8 3229 | 6 3230 | 12 3231 | 10 3232 | 19 3233 | 8 3234 | 3235 | Sample Output 1: 3236 | 3 3237 | 3238 | Sample Input 2: 3239 | 8 3240 | 2 3241 | 1 3242 | 3 3243 | 8 3244 | 6 3245 | 12 3246 | 10 3247 | 19 3248 | 80 3249 | 3250 | Sample Output 2: 3251 | not found 3252 | 3253 | Sample Input 3: 3254 | -5 3255 | 3256 | Sample Output 3: 3257 | Invalid Input 3258 | 3259 | 3260 | 3261 | Sample Input 4: 3262 | 5 3263 | 23 3264 | 2 3265 | -200 3266 | 3267 | Sample Output 4: 3268 | Invalid Input 3269 | 3270 | import java.util.Scanner; 3271 | public class Main 3272 | { 3273 | public static void main(String[] args) 3274 | { 3275 | int n, i,se,index=0,f=0; 3276 | // int flag=0; 3277 | Scanner in=new Scanner(System.in); 3278 | n = in.nextInt(); 3279 | if(n < 0) 3280 | { 3281 | System.out.print("Invalid array size"); 3282 | System.exit(0); 3283 | } 3284 | else 3285 | { 3286 | int a[]=new int[n]; 3287 | for(i = 0; i< n; i++) 3288 | { 3289 | a[i] = in.nextInt(); 3290 | if(a[i] < 0) 3291 | { 3292 | //flag=1; 3293 | System.out.print("Invalid input"); 3294 | System.exit(0); 3295 | } 3296 | } 3297 | // if(flag!=1) 3298 | // { 3299 | se=in.nextInt(); 3300 | for(i=0 ; i90 - 50% 3478 | 81-90 - 25% 3479 | 70-80 - 10% 3480 | <70 - 0% 3481 | Calculate the fees according to above table. 3482 | 3483 | Note: 3484 | Formula : fees - (fees* discount(%)) 3485 | Print "Invalid mark" if the mark is greater than 100 3486 | Print "Invalid fee" if the fee is greater than 32767 3487 | Print "Invalid input" if any of the input is negative 3488 | 3489 | Input and Output Format : 3490 | First line of input represents the fee, second line of input represents the marks of student. 3491 | 3492 | Sample Input 1: 3493 | 10000 3494 | 95 3495 | Sample Output 1: 3496 | 5000 3497 | 3498 | Sample Input 2: 3499 | 15896 3500 | 101 3501 | Sample Output 2: 3502 | Invalid mark 3503 | import java.util.Scanner; 3504 | public class Main 3505 | { 3506 | public static void main(String[] args) 3507 | { 3508 | int fees,marks; 3509 | double discount,fee_final=0; 3510 | Scanner in=new Scanner(System.in); 3511 | fees = in.nextInt(); 3512 | marks = in.nextInt(); 3513 | if(marks>100) 3514 | System.out.print("Invalid mark"); 3515 | else if(fees>32767) 3516 | System.out.print("Invalid fees"); 3517 | else if(marks<0 || fees<0) 3518 | System.out.print("Invalid input"); 3519 | else 3520 | { 3521 | if(marks>90) 3522 | discount = .50; 3523 | else if(marks>80 && marks<=90) 3524 | discount = .25; 3525 | else if(marks>=70 && marks<=80) 3526 | discount = .10; 3527 | else 3528 | discount = 0; 3529 | fee_final = fees-(fees*discount); 3530 | System.out.printf("%.0f",fee_final); 3531 | } 3532 | } 3533 | } 3534 | 3535 | 3536 | 51.sumTwoFive 3537 | Read the question carefully and follow the input and output format. 3538 | 3539 | Given an integer array find the sum of elements which end with 2 or 5. 3540 | 3541 | Input and Output Format: 3542 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements. Output consist of an integer, the sum. 3543 | 3544 | 1) Print "Invalid array size" when size of the array is negative . 3545 | 2) Print "Invalid input" when there is any negative number available in the input array and terminate the program. 3546 | 3547 | Include a function named sumTwoFive(int array[], int size) whose return type is an integer, the sum 3548 | 3549 | Sample Input 1: 3550 | 5 3551 | 22 3552 | 35 3553 | 5 3554 | 2 3555 | 10 3556 | 3557 | Sample Output 1: 3558 | 64 3559 | 3560 | Sample Input 2: 3561 | -6 3562 | 3563 | Sample Output 2: 3564 | Invalid array size 3565 | 3566 | import java.util.Scanner; 3567 | public class Main 3568 | { 3569 | public static void main(String[] args) 3570 | { 3571 | int n, i, temp,sum=0; 3572 | // int flag=0; 3573 | Scanner in=new Scanner(System.in); 3574 | n = in.nextInt(); 3575 | if(n < 0) 3576 | { 3577 | System.out.print("Invalid array size"); 3578 | System.exit(0); 3579 | } 3580 | else 3581 | { 3582 | int a[]=new int[n]; 3583 | for(i = 0; i< n; i++) 3584 | { 3585 | a[i] = in.nextInt(); 3586 | if(a[i] < 0) 3587 | { 3588 | //flag=1; 3589 | System.out.print("Invalid input"); 3590 | System.exit(0); 3591 | } 3592 | } 3593 | // if(flag!=1) 3594 | // { 3595 | for(i=0;i32767) 3646 | { 3647 | System.out.println ("Number too large"); 3648 | // flag=1; 3649 | } 3650 | //if(flag==0) 3651 | //{ 3652 | while (n != 0) 3653 | { 3654 | rem=n%10; 3655 | if(rem%2!=0) 3656 | sum=sum+rem; 3657 | n=n/10; 3658 | } 3659 | System.out.print(sum); 3660 | // } 3661 | } 3662 | } 3663 | 53.LCM 3664 | 3665 | Write a program to calculate the LCM of the 2 given integers. 3666 | 3667 | Include a function named calculateLCM that accepts 2 integer arguments and returns an int that corresponds to the LCM of the 2 numbers. 3668 | 3669 | Print Invalid Input and terminate the program in the following cases: 3670 | 1. Any of the 2 inputs is greater than 1000 3671 | 2. Any of the 2 inputs is negative 3672 | 3673 | 3674 | Input and Output Format: 3675 | Input consists of 2 integers. 3676 | Output consists of a single integer that corresponds to the LCM. 3677 | 3678 | 3679 | Sample Input 1: 3680 | 10 3681 | 8 3682 | 3683 | Sample Output 1: 3684 | 40 3685 | 3686 | 3687 | Sample Input 2: 3688 | 50000 3689 | 3690 | 3691 | Sample Output 2: 3692 | Invalid Input 3693 | 3694 | import java.util.*; 3695 | public class Main 3696 | { 3697 | public static void main(String[] args) 3698 | { 3699 | int a, b, i, c=0; 3700 | Scanner in=new Scanner(System.in); 3701 | a = in.nextInt(); 3702 | b = in.nextInt(); 3703 | if(a>1000 || b>1000 || a<0 || b<0) 3704 | { 3705 | System.out.print ("Invalid input"); 3706 | System.exit(0); 3707 | } 3708 | c=a*b; 3709 | int d=c; 3710 | for(i=1;i<=c;i++) 3711 | { 3712 | if(i%a==0 && i%b==0 && i32767) 3761 | { 3762 | System.out.println ("Number too large"); 3763 | flag=1; 3764 | } 3765 | // if(flag==0) 3766 | // { 3767 | while (n != 0) 3768 | { 3769 | rem=n%10; 3770 | count=0; 3771 | for(i=1;i<=rem;i++) 3772 | { 3773 | if(rem%i==0) 3774 | count++; 3775 | } 3776 | if(count==2) 3777 | { 3778 | f=1; 3779 | prod = prod*rem; 3780 | } 3781 | n=n/10; 3782 | } 3783 | if(f==1) 3784 | System.out.println (prod); 3785 | // } 3786 | } 3787 | } 3788 | 55.dailyAllowance 3789 | Read the question carefully and follow the input and output format. 3790 | 3791 | A Sales person daily allowances calculated as follows . 3792 | Item Money (rupees) 3793 | Shirt 15 3794 | Saree 10 3795 | other items 5 3796 | 3797 | Given an input array in which the first index represents no.of shirts sold, second index represents the no of sarees sold and the third index represents the other items sold for a particular day, Calculate the total allowances. 3798 | 3799 | Inlcude a function named dailyAllowance(int items[], int size) that returns an integer, which is the total allowances. 3800 | 3801 | Business Rules: 3802 | 1) Print "Invalid array size" when size of the array is a negative number and terminate the program 3803 | 2) Print "Invalid item count" when there is any negative numbers available in the input array and terminate the program 3804 | 3) Print "Array size greater than 3" when size of the array is greater than 3 and terminate the program. 3805 | 3806 | Input and Output Format : 3807 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements. Output consist of the total allowance. 3808 | 3809 | Sample Input 1: 3810 | 3 3811 | 10 3812 | 5 3813 | 10 3814 | Sample Output 1: 3815 | 250 3816 | 3817 | Sample Input 2: 3818 | 4 3819 | 3820 | Sample Output 2: 3821 | Array size greater than 3 3822 | import java.util.Scanner; 3823 | public class Main 3824 | { 3825 | public static void main(String[] args) 3826 | { 3827 | int n, i; 3828 | // int flag=0; 3829 | Scanner in=new Scanner(System.in); 3830 | n = in.nextInt(); 3831 | if(n < 0) 3832 | { 3833 | System.out.print("Invalid array size"); 3834 | System.exit(0); 3835 | } 3836 | else if(n>3) 3837 | { 3838 | System.out.print("Array size greater than 3"); 3839 | System.exit(0); 3840 | } 3841 | else 3842 | { 3843 | int a[]=new int[n]; 3844 | for(i = 0; i< n; i++) 3845 | { 3846 | a[i] = in.nextInt(); 3847 | if(a[i] < 0) 3848 | { 3849 | //flag=1; 3850 | System.out.print("Invalid item count "); 3851 | System.exit(0); 3852 | } 3853 | } 3854 | // if(flag!=1) 3855 | // { 3856 | int shirt=15,saree=10,other=5,allowance=0; 3857 | for(i=0;i32767) 3910 | { 3911 | System.out.println ("Number too large"); 3912 | //flag=1; 3913 | System.exit(0); 3914 | } 3915 | // if(flag==0) 3916 | //{ 3917 | while (n != 0) 3918 | { 3919 | rem=n%10; 3920 | if(rem==2 || rem==3 || rem==5 || rem==7) 3921 | { 3922 | res=res+(rem*i); 3923 | i=i*10; 3924 | } 3925 | else if(rem==9) 3926 | { 3927 | res=res+0; 3928 | i=i*10; 3929 | } 3930 | else 3931 | { 3932 | res=res+((rem+1)*i); 3933 | i=i*10; 3934 | } 3935 | n = n/10; 3936 | } 3937 | System.out.println (res); 3938 | //} 3939 | } 3940 | } 3941 | 58.findEmployeeID 3942 | Read the question carefully and follow the input and output format. 3943 | 3944 | In a given input array, elements are given in this Format PPPII, where PPP represents the Project Code and II represents employee id . Find out the Employee Ids who are working in the given project code. 3945 | 3946 | Input and Output Format : 3947 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements. The next line corresponds to the project code. Output consists of the employee id's. 3948 | 3949 | 1) Print "Invalid array size" when size of the array is a negative number and terminate the program 3950 | 2) Print "Invalid employee id" when there is any negative numbers available in the input array and terminate the program 3951 | 3) Print "Invalid project code" when branch code is negative number and terminate the program 3952 | 3953 | Include a function named findEmployeeID(int size,int employee_ids[],int project_code). Its return type is void. 3954 | The output array is stored in a global variable named working_employee. 3955 | 3956 | Sample Input 1: 3957 | 5 3958 | 12345 3959 | 12334 3960 | 23457 3961 | 23478 3962 | 12546 3963 | 123 3964 | Sample Output 1: 3965 | 45 3966 | 34 3967 | 3968 | Sample Input 2: 3969 | 5 3970 | 12345 3971 | 12334 3972 | 23457 3973 | -23478 3974 | 3975 | Sample Output 2: 3976 | Invalid employee id 3977 | 3978 | import java.util.Scanner; 3979 | public class Main 3980 | { 3981 | public static void main(String[] args) 3982 | { 3983 | int n, i, pcode=0,count=0,temp=0,code=0; 3984 | // int flag=0; 3985 | Scanner in=new Scanner(System.in); 3986 | n = in.nextInt(); 3987 | if(n < 0) 3988 | { 3989 | System.out.print("Invalid array size"); 3990 | System.exit(0); 3991 | } 3992 | else 3993 | { 3994 | int a[]=new int[n]; 3995 | for(i = 0; i< n; i++) 3996 | { 3997 | a[i] = in.nextInt(); 3998 | if(a[i] < 0) 3999 | { 4000 | //flag=1; 4001 | System.out.print("Invalid employee id "); 4002 | System.exit(0); 4003 | } 4004 | } 4005 | //if(flag!=1) 4006 | //{ 4007 | int b[]=new int[20]; 4008 | pcode=in.nextInt(); 4009 | if(pcode<0) 4010 | { 4011 | System.out.print ("Invalid project code"); 4012 | System.exit(0); 4013 | } 4014 | for(i=0;i=0;i++,j--) 4126 | { 4127 | c[i] = a[i]+b[j]; 4128 | System.out.println(c[i]); 4129 | } 4130 | // } 4131 | } 4132 | } 4133 | } 4134 | 60.adjecentDifference 4135 | Read the question carefully and follow the input and output format. 4136 | 4137 | Given an input Integer array, find the difference in the adjacent elements and print the largest difference 4138 | 4139 | Input and Output Format : 4140 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements. Output consist of largest adjacent difference. 4141 | 4142 | Print "Invalid array size" when size of the array is a negative number and terminate the program 4143 | Print "Invalid input" when there is any negative number available in the input array and terminate the program. 4144 | Sample Input 1: 4145 | 7 4146 | 2 4147 | 4 4148 | 5 4149 | 1 4150 | 9 4151 | 3 4152 | 8 4153 | Sample Output 1: 4154 | 8 4155 | 4156 | Hint: The AdjecentElement Diff are :2,1,4,8,6,5 and Maximum diff is 8 which is obtained by 2-4 =2, 4-5=1,5- 1 =4, 1-9=8,9-3=6,3-8 =5 4157 | 4158 | Sample Input 2: 4159 | 5 4160 | 1 4161 | 7 4162 | 3 4163 | -8 4164 | Sample Output 2: 4165 | Invalid input 4166 | import java.util.*; 4167 | public class Main 4168 | { 4169 | public static void main(String[] args) 4170 | { 4171 | int n, i,diff,max=0; 4172 | Scanner in=new Scanner(System.in); 4173 | n = in.nextInt(); 4174 | if(n < 0) 4175 | { 4176 | System.out.print("Invalid array size"); 4177 | System.exit(0); 4178 | } 4179 | else 4180 | { 4181 | int a[]=new int[100]; 4182 | for(i = 0; i< n; i++) 4183 | { 4184 | a[i] = in.nextInt(); 4185 | if(a[i] < 0) 4186 | { 4187 | System.out.print("Invalid input"); 4188 | System.exit(0); 4189 | } 4190 | } 4191 | for(i=0;ia[i+1]) 4194 | diff=a[i]-a[i+1]; 4195 | else 4196 | diff=a [i+1]-a[i]; 4197 | if(diff>max) 4198 | max=diff; 4199 | } 4200 | System.out.print(max); 4201 | } 4202 | } 4203 | } 4204 | 4205 | 4206 | 61.Duplicate Elements 4207 | Tim is working as a data entry staff in a college. His manager wants him to delete the duplicate student id from the entry. Help Tim in writing a program to delete the duplicate elements. 4208 | Include a function named eliminateDuplicate that accepts 2 arguments and its return type is void. The first argument is the input array and the second argument is an int that corresponds to the size of the array. The output array is stored in a global variable named output1 and the number of elements in the output array is stored in the global variable named output 2. 4209 | If the size of the array is negative or if any element in the array is negative, print “Invalid Input” and terminate the program. 4210 | Input and Output Format: 4211 | Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next 'n' integers correspond to the elements in the array. 4212 | Output consists of an integer array. 4213 | Refer sample output for formatting specifications. 4214 | Assume that the maximum number of elements in the array is 20. 4215 | Sample Input 1: 4216 | 8 4217 | 1 4218 | 6 4219 | 3 4220 | 5 4221 | 6 4222 | 8 4223 | 5 4224 | 9 4225 | Sample Output 1: 4226 | 1 4227 | 6 4228 | 3 4229 | 5 4230 | 8 4231 | 9 4232 | 4233 | 4234 | Sample Input 2: 4235 | -5 4236 | Sample Output 2: 4237 | Invalid Input 4238 | Sample Input 3: 4239 | 5 4240 | 23 4241 | 2 4242 | -200 4243 | 4244 | Sample Output 3: 4245 | Invalid Input 4246 | import java.util.Scanner; 4247 | public class Main 4248 | { 4249 | public static void main(String[] args) 4250 | { 4251 | int n, i,j, k; 4252 | // int flag=0; 4253 | Scanner in=new Scanner(System.in); 4254 | n = in.nextInt(); 4255 | if(n < 0) 4256 | { 4257 | System.out.print("Invalid array size"); 4258 | System.exit(0); 4259 | } 4260 | else 4261 | { 4262 | int a[]=new int[n]; 4263 | for(i = 0; i< n; i++) 4264 | { 4265 | a[i] = in.nextInt(); 4266 | if(a[i] < 0) 4267 | { 4268 | //flag=1; 4269 | System.out.print("Invalid input"); 4270 | System.exit(0); 4271 | } 4272 | } 4273 | //if(flag!=1) 4274 | //{ 4275 | for(i=0;i32767) 4331 | { 4332 | System.out.println ("Number too large"); 4333 | System.exit(0); 4334 | } 4335 | else 4336 | { 4337 | for(i=2;i<=n;i++) 4338 | { 4339 | if(n%i==0) 4340 | { 4341 | System.out.println(i); 4342 | System.exit(0); 4343 | } 4344 | } 4345 | } 4346 | } 4347 | } 4348 | 63.secondMaxMinDiff 4349 | Read the question carefully and follow the input and output format. 4350 | 4351 | Given an input array, find the difference b/w second largest and second smallest element in the array. 4352 | 4353 | Hint : There is no repetition of element in the array. 4354 | 4355 | Input and Output Format: 4356 | 4357 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements. Output consist of an integer, which is the difference b/w second largest and second smallest.. 4358 | 4359 | 1) Print Invalid array size when size of the array is negative and terminate the program. 4360 | 2) Print Invalid input when there is any negative numbers available in the input array and terminate the program. 4361 | 4362 | Include a function named secondMaxMinDiff(int[] array, int n) whose return type is an integer, which is the difference b/w second largest and second smallest. 4363 | 4364 | Sample Input 1: 4365 | 5 4366 | 1 4367 | 2 4368 | 3 4369 | 4 4370 | 5 4371 | 4372 | Sample Output 1: 4373 | 2 4374 | 4375 | Sample Input 2: 4376 | 4 4377 | -3 4378 | 4379 | Sample Output 2: 4380 | Invalid input 4381 | 4382 | import java.util.Scanner; 4383 | public class Main 4384 | { 4385 | public static void main(String[] args) 4386 | { 4387 | int n, i,j, temp=0,diff=0; 4388 | // int flag=0; 4389 | Scanner in=new Scanner(System.in); 4390 | n = in.nextInt(); 4391 | if(n < 0) 4392 | { 4393 | System.out.print("Invalid array size"); 4394 | System.exit(0); 4395 | } 4396 | else 4397 | { 4398 | int a[]=new int[n]; 4399 | for(i = 0; i< n; i++) 4400 | { 4401 | a[i] = in.nextInt(); 4402 | if(a[i] < 0) 4403 | { 4404 | // flag=1; 4405 | System.out.print("Invalid input"); 4406 | System.exit(0); 4407 | } 4408 | } 4409 | // if(flag!=1) 4410 | // { 4411 | for(i=0;i32767) 4465 | { 4466 | System.out.println ("Number too large"); 4467 | System.exit(0); 4468 | } 4469 | else 4470 | { 4471 | while(n>10) 4472 | { 4473 | rem=n%10; 4474 | n=n/10; 4475 | rem1=n%10; 4476 | if(rem>rem1) 4477 | diff=rem-rem1; 4478 | else 4479 | diff=rem1-rem; 4480 | digit=digit+(diff*i); 4481 | i=i*10; 4482 | } 4483 | System.out.println(digit); 4484 | } 4485 | } 4486 | } 4487 | 65.Digit Counting 4488 | In a game show everybody got one coupon with some code. They need to count the digits in the code and send SMS to the given number. 4489 | 4490 | Write a program to find the number of digits in the given number. 4491 | Include a function named countDigits that accepts an integer argument and returns an integer that corresponds to the number of digits. If the input is a negative number, the function returns -1. 4492 | If the function returns -1, print Invalid Input. 4493 | Input and Output Format: 4494 | Input consists of a single integer. 4495 | Output consists of an integer that corresponds to the number of digits in the input. 4496 | Sample Input 1: 4497 | 250 4498 | Sample Output 1: 4499 | 3 4500 | Sample Input 2: 4501 | -2345 4502 | Sample Output 2: 4503 | Invalid Input 4504 | 4505 | import java.util.Scanner; 4506 | public class Main 4507 | { 4508 | public static void main(String[] args) 4509 | { 4510 | int n, count=0; 4511 | Scanner in=new Scanner(System.in); 4512 | n = in.nextInt(); 4513 | if(n<0) 4514 | { 4515 | System.out.println ("Number too small"); 4516 | System.exit(0); 4517 | } 4518 | if(n>32767) 4519 | { 4520 | System.out.println ("Number too large"); 4521 | System.exit(0); 4522 | } 4523 | while(n!=0) 4524 | { 4525 | count++; 4526 | n=n/10; 4527 | } 4528 | System.out.println (count); 4529 | } 4530 | } 4531 | 4532 | 66.maxScoreCount 4533 | 4534 | Read the question carefully and follow the input and output format. 4535 | 4536 | Student1 and Student2 are of same class and have recieved their scores for different subjects. Given each subject scores of the Student1 & Student2, Find out in how many subjects student1 has scored more marks than student2. 4537 | 4538 | Input and Output Format : 4539 | First line of input corresponds to the n, the number of subjects. The next n lines correspond to the scores of Student 1 and the next n lines correspond to the scores of student 2. Output is the number of subjects student1 scored more marks than student2. 4540 | 4541 | Print "Invalid size" when size of the array is a negative number and terminate the program 4542 | Print "Invalid score" when there is any negative score and terminate the program 4543 | 4544 | Include a function named maxScoreCount(int size,int student1[],int student2[]) that returns an integer, the number of subjects student1 scored more marks than student2 4545 | 4546 | Sample Input 1: 4547 | 7 4548 | 45 4549 | 23 4550 | 67 4551 | 34 4552 | 88 4553 | 13 4554 | 67 4555 | 33 4556 | 56 4557 | 89 4558 | 44 4559 | 67 4560 | 89 4561 | 55 4562 | 4563 | Sample Output 1: 4564 | 3 4565 | 4566 | Sample Input 2: 4567 | -3 4568 | 4569 | Sample Output 2: 4570 | Invalid size 4571 | 4572 | Sample Input 3: 4573 | 2 4574 | 30 4575 | -60 4576 | Sample Output 3: 4577 | Invalid score 4578 | import java.util.Scanner; 4579 | public class Main 4580 | { 4581 | public static void main(String[] args) 4582 | { 4583 | int n, i, count=0; 4584 | // int flag=0; 4585 | Scanner in=new Scanner(System.in); 4586 | n=in.nextInt(); 4587 | if(n < 0) 4588 | { 4589 | System.out.print("Invalid array size"); 4590 | System.exit(0); 4591 | } 4592 | else 4593 | { 4594 | int a[]=new int[n]; 4595 | for(i = 0; i< n; i++) 4596 | { 4597 | a[i] = in.nextInt(); 4598 | if(a[i] < 0) 4599 | { 4600 | //flag=1; 4601 | System.out.print("Invalid score"); 4602 | System.exit(0); 4603 | } 4604 | } 4605 | int b[]=new int[n]; 4606 | for(i = 0; i< n; i++) 4607 | { 4608 | b[i] = in.nextInt(); 4609 | if(b[i] < 0) 4610 | { 4611 | //flag=1; 4612 | System.out.print("Invalid score"); 4613 | System.exit(0); 4614 | } 4615 | } 4616 | // if(flag!=1) 4617 | { 4618 | for(i=0;ib[i]) 4621 | count++; 4622 | } 4623 | System.out.print(count); 4624 | } 4625 | } 4626 | } 4627 | } 4628 | 67.nextPrime 4629 | Read the question carefully and follow the input and output format. 4630 | 4631 | Write a program to find out the Next Prime to the given number. 4632 | 4633 | Hint: number is always less than 100. 4634 | 4635 | Input and Output Format : 4636 | 4637 | First line of input consists of n, the number. Output is a single integer that displays the next prime. 4638 | 4639 | Print "Number too large" when the given input number is greater than 32767 . 4640 | Print "Number too small" when given input is a negative number. 4641 | 4642 | Include a function named nextPrime(int num) whose return type is an integer, the next prime. 4643 | 4644 | Sample Input 1: 4645 | 9 4646 | 4647 | Sample Output 1: 4648 | 11 4649 | 4650 | Sample Input 2: 4651 | 98987 4652 | 4653 | Sample Output 2: 4654 | Number too large 4655 | import java.util.Scanner; 4656 | public class Main 4657 | { 4658 | public static void main(String[] args) 4659 | { 4660 | int i,j,n; 4661 | Scanner in=new Scanner(System.in); 4662 | n = in.nextInt(); 4663 | if(n<0) 4664 | { 4665 | System.out.println ("Number too small"); 4666 | System.exit(0); 4667 | } 4668 | if(n>32767) 4669 | { 4670 | System.out.println ("Number too large"); 4671 | System.exit(0); 4672 | } 4673 | else 4674 | { 4675 | int next=0,count=0; 4676 | for(i=n+1;i<100;i++) 4677 | { 4678 | count=0; 4679 | for(j=1;j<=i;j++) 4680 | { 4681 | if(i%j==0) 4682 | count++; 4683 | } 4684 | if(count==2) 4685 | { 4686 | next=i; 4687 | System.out.println(next); 4688 | } 4689 | } 4690 | if(next==0) 4691 | System.exit(0); 4692 | } 4693 | } 4694 | } 4695 | 68.Array Average 4696 | Write a program to find the average of the array. 4697 | Include a function named avgArray that accepts 2 arguments and returns a float. The first argument is the input array and the second argument is an int that corresponds to the size of the array. The function returns a float that corresponds to the average of the array. 4698 | If the size of the array is negative or if any element in the array is negative , print “Invalid Input” and terminate the program. 4699 | Input and Output Format: 4700 | Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next 'n' integers correspond to the elements in the array. 4701 | Output consists of a floating point number that corresponds to the average. It is displayed correct to 2 decimal places. 4702 | Assume that the maximum size of the array is 20. 4703 | 4704 | Sample Input 1: 4705 | 7 4706 | 1 4707 | 9 4708 | 8 4709 | 4 4710 | 6 4711 | 4 4712 | 5 4713 | Sample Output 1: 4714 | 5.29 4715 | Sample Input 2: 4716 | -5 4717 | Sample Output 2: 4718 | Invalid Input 4719 | Sample Input 3: 4720 | 5 4721 | 23 4722 | 2 4723 | -5 4724 | 4725 | Sample Output 3: 4726 | Invalid Input 4727 | 4728 | import java.util.Scanner; 4729 | public class Main 4730 | { 4731 | public static void main(String[] args) 4732 | { 4733 | int n, i, sum=0; 4734 | // int flag=0; 4735 | double avg; 4736 | Scanner in=new Scanner(System.in); 4737 | n = in.nextInt(); 4738 | if(n < 0) 4739 | { 4740 | System.out.print("Invalid array size"); 4741 | System.exit(0); 4742 | } 4743 | else 4744 | { 4745 | int a[]=new int[n]; 4746 | for(i = 0; i< n; i++) 4747 | { 4748 | a[i] = in.nextInt(); 4749 | if(a[i] < 0) 4750 | { 4751 | //flag=1; 4752 | System.out.print("Invalid input"); 4753 | System.exit(0); 4754 | } 4755 | } 4756 | // if(flag!=1) 4757 | //{ 4758 | for(i=0;i32767) 4821 | { 4822 | System.out.println ("Number too large"); 4823 | System.exit(0); 4824 | //flag=1; 4825 | } 4826 | //if(flag==0) 4827 | //{ 4828 | num=n; 4829 | while(n!=0) 4830 | { 4831 | i=1; 4832 | f=1; 4833 | r=n%10; 4834 | while(i<=r) 4835 | { 4836 | f=f*i; 4837 | i++; 4838 | } 4839 | sum=sum+f; 4840 | n=n/10; 4841 | } 4842 | if(sum==num) 4843 | System.out.println("yes"); 4844 | else 4845 | System.out.println("no"); 4846 | //} 4847 | } 4848 | } 4849 | 70.Sum of Even Digits 4850 | In the computer science department, HOD wants to divide students into two groups based on their roll numbers. He decided to find the sum of the even digits in the roll number of each student and decided to split them into 2 groups based on this. Write a program to find the sum of the even digits in a number. 4851 | Include a function named addEvenDigits to find the sum of even digits in a number. This function accepts an integer argument and returns an integer. The function returns -1 if the roll number is less than zero or if the roll number is greater than 32767. Refer function specifications given at the end of the problem for further details. 4852 | If the roll number is less than 0 or if it exceeds 32767, print “Invalid Input”. 4853 | Input and Output Format: 4854 | The input consists of an integer that corresponds to the roll number. 4855 | The output consists of an integer that corresponds to the sum of the even digits in the roll number. 4856 | Sample Input 1: 4857 | 3487 4858 | Sample Ouput 1: 4859 | 12 4860 | Sample Input 2: 4861 | -8 4862 | Sample Output 2: 4863 | Invalid Input 4864 | 4865 | 4866 | import java.util.Scanner; 4867 | public class Main 4868 | { 4869 | public static void main(String[] args) 4870 | { 4871 | int i,n,flag=0; 4872 | Scanner in=new Scanner(System.in); 4873 | n = in.nextInt(); 4874 | if(n<0) 4875 | { 4876 | System.out.println ("Number too small"); 4877 | System.exit(0); 4878 | } 4879 | if(n>32767) 4880 | { 4881 | System.out.println ("Number too large"); 4882 | System.exit(0); 4883 | } 4884 | while(n!=0) 4885 | { 4886 | rem=n%10; 4887 | if(rem%2==0) 4888 | res=res+rem; 4889 | n=n/10; 4890 | } 4891 | System.out.println(res); 4892 | } 4893 | } 4894 | 4895 | 4896 | 71.commonElementsSum 4897 | Read the question carefully and follow the input and output format. 4898 | 4899 | Given 2 integer arrays , write a program to find the sum of common elements in both the arrays. 4900 | 4901 | If there are no common elements print 0. 4902 | 4903 | Input and Output Format : 4904 | First line of input consists of n, the number of elements. Next n lines correspond to the first array elements and the next n lines correspond to the second array elements. Output consist of an integer, which is the sum 4905 | 4906 | 1) Print "Invalid array size" when size of the array is a negative number and terminate the program. 4907 | 2) Print "Invalid input" when there is any negative numbers available in the input array and terminate the program. 4908 | 4909 | Sample Input 1: 4910 | 4 4911 | 1 4912 | 2 4913 | 3 4914 | 4 4915 | 2 4916 | 3 4917 | 6 4918 | 7 4919 | 4920 | Sample Output 1: 4921 | 5 4922 | 4923 | Sample Input 2: 4924 | 3 4925 | 8 4926 | 6 4927 | -7 4928 | 4929 | Sample Output 2: 4930 | Invalid input 4931 | 4932 | 4933 | import java.util.Scanner; 4934 | public class Main 4935 | { 4936 | public static void main(String[] args) 4937 | { 4938 | int n, i,j,flag=0,k=0,sum=0; 4939 | Scanner in=new Scanner(System.in); 4940 | n=in.nextInt(); 4941 | if(n < 0) 4942 | { 4943 | System.out.print("Invalid array size"); 4944 | System.exit(0); 4945 | } 4946 | else 4947 | { 4948 | int a[]=new int[n]; 4949 | for(i = 0; i< n; i++) 4950 | { 4951 | a[i] = in.nextInt(); 4952 | if(a[i] < 0) 4953 | { 4954 | flag=1; 4955 | System.out.print("Invalid input"); 4956 | System.exit(0); 4957 | } 4958 | } 4959 | int b[]=new int[n]; 4960 | for(i = 0; i< n; i++) 4961 | { 4962 | b[i] = in.nextInt(); 4963 | if(b[i] < 0) 4964 | { 4965 | flag=1; 4966 | System.out.print("Invalid input"); 4967 | System.exit(0); 4968 | } 4969 | } 4970 | if(flag!=1) 4971 | { 4972 | int common[]=new int[n]; 4973 | for(i=0;i32767) 5168 | { 5169 | System.out.println ("Number too large"); 5170 | flag=1; 5171 | } 5172 | if(flag==0) 5173 | { 5174 | while(n!=0) 5175 | { 5176 | rem=n%10; 5177 | if(rem%2==0) 5178 | evenprod=evenprod*rem; 5179 | else 5180 | oddprod=oddprod*rem; 5181 | n=n/10; 5182 | } 5183 | res = evenprod + oddprod; 5184 | System.out.println (res); 5185 | } 5186 | } 5187 | } 5188 | 5189 | 75.Sum of the Digits 5190 | 5191 | In a lucky draw everybody got one coupon with some code. They need to sum the digits in the code and send SMS to the given number. Write a program to find the sum of digits in a number. 5192 | 5193 | Include a function named sumDigits that accepts an integer argument and returns an integer that corresponds to the sum of the digits. The function returns -1 if the input is less than zero or if the roll number is greater than 32767. 5194 | 5195 | If the function returns -1, print “Invalid Input”. 5196 | 5197 | Input and Output Format: 5198 | 5199 | The input consists of an integer. 5200 | The output consists of an integer that corresponds to the sum of the digits in the number. 5201 | 5202 | Sample Input 1: 5203 | 3487 5204 | 5205 | Sample Ouput 1: 5206 | 22 5207 | 5208 | Sample Input 2: 5209 | -8 5210 | 5211 | Sample Output 2: 5212 | Invalid Input 5213 | 5214 | import java.util.Scanner; 5215 | public class Main 5216 | { 5217 | public static void main(String[] args) 5218 | { 5219 | int n, rem,flag=0, res=0; 5220 | Scanner in=new Scanner(System.in); 5221 | n = in.nextInt(); 5222 | if(n<0) 5223 | { 5224 | System.out.println ("Number too small"); 5225 | flag=1; 5226 | } 5227 | if(n>32767) 5228 | { 5229 | System.out.println ("Number too large"); 5230 | flag=1; 5231 | } 5232 | if(flag==0) 5233 | { 5234 | while(n!=0) 5235 | { 5236 | rem=n%10; 5237 | res=res+rem; 5238 | n=n/10; 5239 | } 5240 | System.out.print(res); 5241 | } 5242 | } 5243 | } 5244 | 5245 | 76.Sum of squares of prime numbers 5246 | 5247 | Given an integer n, write a program to find the sum of squares of prime numbers upto and including n. 5248 | 5249 | Include a function named sumSquarePrime that accepts an integer argument and returns an integer that corresponds to result. The function returns -1 if the input is less than zero or if the number is greater than 32767. 5250 | 5251 | If the function returns -1, print “Invalid Input”. 5252 | 5253 | Please note that 1 is neither prime nor composite. 5254 | 5255 | Input and Output Format: 5256 | 5257 | The input consists of an integer. 5258 | The output consists of an integer that corresponds to the sum of the squares of prime numbers. 5259 | 5260 | Sample Input 1: 5261 | 10 5262 | 5263 | Sample Ouput 1: 5264 | 87 5265 | 5266 | Sample Input 2: 5267 | -8 5268 | 5269 | Sample Output 2: 5270 | Invalid Input 5271 | 5272 | import java.util.Scanner; 5273 | public class Main 5274 | { 5275 | public static void main(String[] args) 5276 | { 5277 | int i,j, n, sum=0,flag=0, count=0; 5278 | Scanner in=new Scanner(System.in); 5279 | n = in.nextInt(); 5280 | if(n<0) 5281 | { 5282 | System.out.println ("Number too small"); 5283 | flag=1; 5284 | } 5285 | if(n>32767) 5286 | { 5287 | System.out.println ("Number too large"); 5288 | flag=1; 5289 | } 5290 | if(flag==0) 5291 | { 5292 | for(i = 1;i<=n;i++) 5293 | { 5294 | count=0; 5295 | for(j=1;j<=i;j++) 5296 | { 5297 | if(i%j==0) 5298 | count++; 5299 | } 5300 | if(count==2) 5301 | sum=sum+(i*i); 5302 | 5303 | } 5304 | System.out.println (sum); 5305 | } 5306 | } 5307 | } 5308 | 5309 | 77.3/5 Number 5310 | 5311 | Write a program to find whether the given number is a 3/5 Number. 5312 | 5313 | A number is a 3/5 Number if the product of the digits in the number is divisible by 3 or 5. 5314 | 5315 | Include a function named divisibleByThreeFive that accepts an integer argument and returns an integer. The function returns 5316 | 1. 1 if it is a 3/5 Number 5317 | 2. 0 if it is not a 3/5 Number 5318 | 3. -1 if it is a negative number 5319 | 5320 | Input and Output Format: 5321 | Input consists of a single integer. 5322 | Output consists of a string. 5323 | Refer sample output for formatting specifications. 5324 | 5325 | 5326 | Sample Input 1: 5327 | 251 5328 | 5329 | Sample Output 1: 5330 | yes 5331 | 5332 | Sample Input 2: 5333 | 241 5334 | 5335 | Sample Output 2: 5336 | no 5337 | 5338 | Sample Input 3: 5339 | -9 5340 | 5341 | Sample Output 3: 5342 | Invalid Input 5343 | 5344 | import java.util.Scanner; 5345 | public class Main 5346 | { 5347 | public static void main(String[] args) 5348 | { 5349 | int n, flag=0,rem, prod=1; 5350 | Scanner in=new Scanner(System.in); 5351 | n = in.nextInt(); 5352 | if(n<0) 5353 | { 5354 | System.out.println ("Number too small"); 5355 | flag=1; 5356 | } 5357 | if(n>32767) 5358 | { 5359 | System.out.println ("Number too large"); 5360 | flag=1; 5361 | } 5362 | if(flag==0) 5363 | { 5364 | while(n!=0) 5365 | { 5366 | rem=n%10; 5367 | prod=prod*rem; 5368 | n=n/10; 5369 | } 5370 | if(prod%3==0 || prod%5==0) 5371 | System.out.println ("yes"); 5372 | else 5373 | System.out.println ("no"); 5374 | } 5375 | } 5376 | } 5377 | 5378 | 78.aboveAverageMarks 5379 | Read the question carefully and follow the input and output format. 5380 | 5381 | Given an input array that represents the marks of students, find out the marks which are greater than or equal to average mark of all students. 5382 | 5383 | Input and Output Format: 5384 | First line of input consists of n, the number of elements in the input array. 5385 | Next n lines correspond to the array elements. Output consist of an integer array. 5386 | 5387 | 1) Print "Invalid array size" when size of the array is negative and terminate the program. 5388 | 2) Print "Invalid input" when there is any negative numbers available in the input array and terminate the program. 5389 | 5390 | Include a function named aboveAverageMarks(int array[], int size) whose return type is void. 5391 | The output array is stored in a global variable named above_average. 5392 | 5393 | Sample Input 1: 5394 | 5 5395 | 10 5396 | 20 5397 | 30 5398 | 40 5399 | 50 5400 | Sample Output 1: 5401 | 30 5402 | 40 5403 | 50 5404 | 5405 | Sample Input 2: 5406 | 4 5407 | -3 5408 | Sample Output 2: 5409 | Invalid Input 5410 | import java.util.Scanner; 5411 | public class Main 5412 | { 5413 | public static void main(String[] args) 5414 | { 5415 | int n, i, flag=0,sum=0,avg=0; 5416 | Scanner in=new Scanner(System.in); 5417 | n = in.nextInt(); 5418 | if(n < 0) 5419 | { 5420 | System.out.print("Invalid array size"); 5421 | System.exit(0); 5422 | } 5423 | else 5424 | { 5425 | int a[]=new int[20]; 5426 | for(i =0; i=avg) 5444 | System.out.println (a[i]); 5445 | } 5446 | } 5447 | } 5448 | } 5449 | } 5450 | 5451 | 79.changeNumber 5452 | Read the question carefully and follow the input and output format. 5453 | 5454 | Tom needs to generate a new number from the given input with the following conditions.Consider Input is always a 3 digit number. 5455 | 5456 | conditions: 5457 | (i) Middle digit comes first. 5458 | (ii) Last digit should come in middle 5459 | (iii) First digit should come as a last digit 5460 | 5461 | 5462 | Business rule: 5463 | 1. Print "Invalid input" if input is negative number. 5464 | 2. Print "Not a 3 digit number" if the given number is not a 3 digit number. 5465 | 158 5466 | 581 5467 | Include a function named changeNumber(int number) that returns an integer. 5468 | 5469 | Input and Output Format: 5470 | Input consists of an integer. 5471 | Refer business rules and sample output for output format. 5472 | 5473 | Sample Input 1: 5474 | 123 5475 | Sample Output 1: 5476 | 231 5477 | 5478 | 5479 | Sample Input 2: 5480 | 1234 5481 | Sample Output 2: 5482 | Not a 3 digit number 5483 | import java.util.Scanner; 5484 | public class Main 5485 | { 5486 | public static void main(String[] args) 5487 | { 5488 | int n, flag=0,rem, d1,d2,d3,i=1,temp=1,res=0; 5489 | Scanner in=new Scanner(System.in); 5490 | n = in.nextInt(); 5491 | if(n<0) 5492 | { 5493 | System.out.println ("Invalid input"); 5494 | flag=1; 5495 | } 5496 | else if(n<100 || n>999) 5497 | { 5498 | System.out.println ("Not a 3 digit number"); 5499 | flag=1; 5500 | } 5501 | if(flag==0) 5502 | { 5503 | do 5504 | { 5505 | rem=n%10; 5506 | d3=rem; 5507 | n=n/10; 5508 | rem=n%10; 5509 | d2=rem; 5510 | n=n/10; 5511 | rem=n%10; 5512 | d1=rem; 5513 | }while(n<0); 5514 | do 5515 | { 5516 | res=res+(d1*i); 5517 | i=i*10; 5518 | res=res+(d3*i); 5519 | i=i*10; 5520 | res=res+(d2*i); 5521 | }while(n<0); 5522 | System.out.println (res); 5523 | } 5524 | } 5525 | } 5526 | 5527 | 80.differentElements 5528 | Read the question carefully and follow the input and output format. 5529 | 5530 | Given two input arrays find out the elements which are not common. 5531 | 5532 | Input and Output Format: 5533 | 5534 | First line of input consists of n, the number of elements. Next n lines correspond to the first array elements and the next n lines correspond to the second array elements. Output consist of an integer array, which contains the elements that are not common between the first and second array. 5535 | 5536 | 1) Print Invalid array size when size of the array is a negative number and terminate the program. 5537 | 2) Print Invalid input when there is any negative numbers available in the input array and terminate the program. 5538 | 5539 | Include a function named differentElements(int set1[], int set2[], int size) whose return type is void. 5540 | The output array is stored in a global variable named not_common. 5541 | 5542 | Sample Input 1: 5543 | 5 5544 | 1 2 3 4 5 5545 | 5 6 4 8 7 5546 | 5547 | Sample Output 1: 5548 | 1 5549 | 2 5550 | 3 5551 | 6 5552 | 8 5553 | 7 5554 | 5555 | Sample Input 2: 5556 | 5 5557 | 1 4 8 9 4 5558 | -8 5559 | Sample Output 2: 5560 | Invalid input 5561 | 5562 | import java.util.Scanner; 5563 | public class Main 5564 | { 5565 | public static void main(String[] args) 5566 | { 5567 | int n, i,j,f=0,k=0,sum=0; 5568 | Scanner in=new Scanner(System.in); 5569 | n=in.nextInt(); 5570 | if(n < 0) 5571 | { 5572 | System.out.print("Invalid array size"); 5573 | System.exit(0); 5574 | } 5575 | else 5576 | { 5577 | int a[]=new int[n]; 5578 | for(i = 0; i< n; i++) 5579 | { 5580 | a[i] = in.nextInt(); 5581 | if(a[i] < 0) 5582 | { 5583 | f=1; 5584 | System.out.print("Invalid input"); 5585 | System.exit(0); 5586 | } 5587 | } 5588 | int b[]=new int[n]; 5589 | for(i = 0; i< n; i++) 5590 | { 5591 | b[i] = in.nextInt(); 5592 | if(b[i] < 0) 5593 | { 5594 | f=1; 5595 | System.out.print("Invalid input"); 5596 | System.exit(0); 5597 | } 5598 | } 5599 | 5600 | if(f!=1) 5601 | { 5602 | int not_common[]=new int[20]; 5603 | for(i=0;i=70 5727 | 5728 | Input and Output Format : 5729 | 5730 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements. Output consist of an integer array. 5731 | 5732 | 1) Print "Invalid array size" when size of the array is negative and terminate the program. 5733 | 2) Print "Invalid input" when there is any negative number available in the input array and terminate the program. 5734 | 5735 | Include a function named clearedStage1(int array[], int size) whose return type is void. 5736 | The output array is stored in a global variable named cleared. 5737 | 5738 | Sample Input 1: 5739 | 9 5740 | 1 5741 | 25 5742 | 75 5743 | 3 5744 | 75 5745 | 80 5746 | 2 5747 | 75 5748 | 75 5749 | 5750 | Sample Output 1: 5751 | 3 5752 | 2 5753 | 5754 | Sample Input 2: 5755 | 6 5756 | 4 5757 | 25 5758 | -78 5759 | 5760 | Sample Output 2: 5761 | Invalid input 5762 | import java.util.*; 5763 | public class Main 5764 | { 5765 | public static void main(String[] args) 5766 | { 5767 | int n, i,k=0; 5768 | Scanner in=new Scanner(System.in); 5769 | n = in.nextInt(); 5770 | if(n < 0) 5771 | { 5772 | System.out.print("Invalid array size"); 5773 | System.exit(0); 5774 | } 5775 | else 5776 | { 5777 | int a[]=new int[100]; 5778 | for(i = 0; i< n; i++) 5779 | { 5780 | a[i] = in.nextInt(); 5781 | if(a[i] < 0) 5782 | { 5783 | System.out.print("Invalid input"); 5784 | System.exit(0); 5785 | } 5786 | } 5787 | int b[]=new int[100]; 5788 | for(i=0;i=70 && a[i+2]>=70) 5791 | { 5792 | b[k]=a[i]; 5793 | k++; 5794 | } 5795 | } 5796 | for(i = 0; i< k; i++) 5797 | System.out.println(b[i]); 5798 | } 5799 | } 5800 | } 5801 | 83.findLargest 5802 | Read the question carefully and follow the input and output format. 5803 | 5804 | Write a program to find the largest of the 3 given numbers. 5805 | 5806 | Input and Output Format : 5807 | Input consists of 3 integers. Output consist of an integer that is the maximum. 5808 | 5809 | Print "Number too large" when any of given input numbers is greater than 32767 . 5810 | Print "Number too small" when given input is a negative number. 5811 | 5812 | Include a function named findLargest(int num1, int num2, int num3) whose return type is an integer, which is the largest. 5813 | 5814 | Sample Input 1: 5815 | 2 5816 | 3 5817 | 4 5818 | 5819 | Sample Output 1: 5820 | 4 5821 | 5822 | Sample Input 2: 5823 | 98974 5824 | 5825 | Sample Output 2: 5826 | Number too large 5827 | 5828 | Sample Input 3: 5829 | -32767 5830 | 5831 | Sample Output 3: 5832 | Number too small 5833 | 5834 | 84.sumThreeLargest 5835 | Read the question carefully and follow the input and output format. 5836 | 5837 | Write a program to find the sum of first ,second and third largest element in the given array. 5838 | 5839 | Input and Output Format: 5840 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements . Output consists of an Integer, the sum. 5841 | 5842 | 1) Print "Invalid array size" when size of the array is a negative number and terminate the program. 5843 | 2) Print "Invalid input" when there is any negative numbers available in the input array and terminate the program. 5844 | 5845 | Sample Input 1: 5846 | 8 5847 | 1 5848 | 2 5849 | 2 5850 | 3 5851 | 4 5852 | 5 5853 | 5 5854 | 4 5855 | 5856 | Sample Output 1: 5857 | 12 5858 | 5859 | Sample Input 2: 5860 | 4 5861 | 1 5862 | 2 5863 | -3 5864 | Sample Output 2: 5865 | Invalid input 5866 | import java.util.Scanner; 5867 | public class Main 5868 | { 5869 | public static void main(String[] args) 5870 | { 5871 | int n, i,j,k=0,temp=0, sum=0; 5872 | //int flag=0; 5873 | Scanner in=new Scanner(System.in); 5874 | n = in.nextInt(); 5875 | if(n < 0) 5876 | { 5877 | System.out.print("Invalid array size"); 5878 | System.exit(0); 5879 | } 5880 | else 5881 | { 5882 | int a[]=new int[20]; 5883 | for(i = 0; i< n; i++) 5884 | { 5885 | a[i] = in.nextInt(); 5886 | if(a[i] < 0) 5887 | { 5888 | //flag=1; 5889 | System.out.print("Invalid input"); 5890 | System.exit(0); 5891 | } 5892 | } 5893 | // if(flag!=1) 5894 | // { 5895 | for(i=0;imax) 5989 | { 5990 | max=a[i]; 5991 | highest=a[i-1]; 5992 | } 5993 | } 5994 | System.out.print(highest); 5995 | //} 5996 | } 5997 | } 5998 | } 5999 | 86.countNoOfConnections 6000 | Read the question carefully and follow the input and output format. 6001 | 6002 | Given an input array, the elements are of format XYYY . where X represents the connection type. YYY represents the connection id. 6003 | 6004 | If X is 2 -> means 2G connection 6005 | If X is 3 -> means 3G connection 6006 | If X is 4 -> means 4G connection 6007 | You need to find the number of type of connections. 6008 | 6009 | Note: 6010 | If a particular connection type (starting with 2 or 3 or 4) is not available represent with zero in the corresponding position. 6011 | 6012 | 6013 | Business Rules : 6014 | 1) Print "Invalid array size" when size of the array is a negative number and terminate the program. 6015 | 2) Print "Invalid connection" when there is any negative number available in the input array and terminate the program 6016 | 6017 | Input and Output Format : 6018 | First line of input corresponds to n, next n lines corresponds to the elements of the array 6019 | Output consists of the the number of type of connections. [1st line of the output corresponds to the number of 2G connections, 2nd line corresponds to the number of 3G connections and 3rd line corresponds to the number of 4G connections] 6020 | 6021 | Sample Input 1 : 6022 | 5 6023 | 2333 6024 | 3101 6025 | 2102 6026 | 4567 6027 | 3123 6028 | 6029 | Sample Output 1: 6030 | 2 6031 | 2 6032 | 1 6033 | 6034 | Sample Input 2 : 6035 | 2 6036 | -2234 6037 | 6038 | 6039 | Sample Output 2: 6040 | Invalid connection 6041 | 6042 | import java.util.Scanner; 6043 | public class Main 6044 | { 6045 | public static void main(String[] args) 6046 | { 6047 | int n, i, x,temp,two=0,three=0,four=0,other; 6048 | //int flag=0; 6049 | Scanner in=new Scanner(System.in); 6050 | n = in.nextInt(); 6051 | if(n < 0) 6052 | { 6053 | System.out.print("Invalid array size"); 6054 | System.exit(0); 6055 | } 6056 | else 6057 | { 6058 | int a[]=new int[20]; 6059 | for(i = 0; i< n; i++) 6060 | { 6061 | a[i] = in.nextInt(); 6062 | if(a[i] < 0) 6063 | { 6064 | //flag=1; 6065 | System.out.print("Invalid connection"); 6066 | System.exit(0); 6067 | } 6068 | } 6069 | //if(flag!=1) 6070 | //{ 6071 | for(i=0;i0) 6134 | sum=sum+a [i]; 6135 | } 6136 | System.out.print(sum); 6137 | } 6138 | } 6139 | } 6140 | 88.Find Index 6141 | Write a program to find the index of a particular number in a given input array. 6142 | Include a function named findIndex that accepts 3 arguments and returns an int. The first argument is the input array, the second argument is an int that corresponds to the size of the array and the third argument is the element to be searched for. The function returns the corresponding index if the search element is present in the array and returns -1 if the search element is not present in the array. 6143 | If the size of the array is negative or if any element in the array is negative, print “Invalid Input” and terminate the program. 6144 | Input and Output Format: 6145 | Input consists of n+2 integers. The first integer corresponds to n, the number of elements in the array. The next 'n' integers correspond to the elements in the array. The last integer corresponds to the element whose count needs to be found. 6146 | Output consists of an integer that corresponds to the index of the search element if it is present. 6147 | Else, print 'not found'. Refer sample output for formatting specifications. Assume that the maximum number of elements in the array is 20 and that all elements in the array are unique. 6148 | Sample Input 1: 6149 | 8 6150 | 2 6151 | 1 6152 | 3 6153 | 8 6154 | 6 6155 | 12 6156 | 10 6157 | 19 6158 | 8 6159 | Sample Output 1: 6160 | 3 6161 | Sample Input 2: 6162 | 8 6163 | 2 6164 | 1 6165 | 3 6166 | 8 6167 | 6 6168 | 12 6169 | 10 6170 | 19 6171 | 80 6172 | Sample Output 2: 6173 | not found 6174 | Sample Input 3: 6175 | -5 6176 | Sample Output 3: 6177 | Invalid Input 6178 | Sample Input 4: 6179 | 5 6180 | 23 6181 | 2 6182 | -200 6183 | Sample Output 4: 6184 | Invalid Input 6185 | 6186 | import java.util.Scanner; 6187 | public class Main 6188 | { 6189 | public static void main(String[] args) 6190 | { 6191 | int n, i, se,f=0,index=0; 6192 | //int flag=0; 6193 | Scanner in=new Scanner(System.in); 6194 | n = in.nextInt(); 6195 | if(n < 0) 6196 | { 6197 | System.out.print("Invalid array size"); 6198 | System.exit(0); 6199 | } 6200 | else 6201 | { 6202 | int a[]=new int[20]; 6203 | for(i = 0; i< n; i++) 6204 | { 6205 | a[i] = in.nextInt(); 6206 | if(a[i] < 0) 6207 | { 6208 | //flag=1; 6209 | System.out.print("Invalid input"); 6210 | System.exit(0); 6211 | } 6212 | } 6213 | // if(flag!=1) 6214 | //{ 6215 | se=in.nextInt(); 6216 | for(i=0 ; i=90 ) : 1 6304 | If(Marks >=80 and <90 ) : 2 6305 | If(Marks >=70 and <80) : 3 6306 | If(Marks <70 ) : 0 6307 | 6308 | Hint: Array size is always is even. 6309 | 6310 | Input and Output Format : 6311 | 6312 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements. Output consist of an integer array. 6313 | 1) Print "Invalid array size" when size of the array is negative and terminate the program. 6314 | 2) Print "Invalid input" when there is any negative numbers available in the input array and terminate the program. 6315 | 6316 | Include a function named findGrade(int array[], int size) whose return type is void. 6317 | The output array is stored in a global variable named grade. 6318 | 6319 | Sample Input 1: 6320 | 10 6321 | 1 6322 | 65 6323 | 2 6324 | 74 6325 | 3 6326 | 86 6327 | 4 6328 | 95 6329 | 6 6330 | 69 6331 | 6332 | Sample Output 1: 6333 | 1 6334 | 0 6335 | 2 6336 | 3 6337 | 3 6338 | 2 6339 | 4 6340 | 1 6341 | 6 6342 | 0 6343 | 6344 | Sample Input 2: 6345 | 4 6346 | -3 6347 | Sample Output 2: 6348 | Invalid input 6349 | import java.util.Scanner; 6350 | public class Main 6351 | { 6352 | public static void main(String[] args) 6353 | { 6354 | int n, i; 6355 | //int flag=0; 6356 | Scanner in=new Scanner(System.in); 6357 | n = in.nextInt(); 6358 | if(n < 0) 6359 | { 6360 | System.out.print("Invalid array size"); 6361 | System.exit(0); 6362 | } 6363 | else 6364 | { 6365 | int a[]=new int[20]; 6366 | for(i = 0; i< n; i++) 6367 | { 6368 | a[i] = in.nextInt(); 6369 | if(a[i] < 0) 6370 | { 6371 | //flag=1; 6372 | System.out.print("Invalid input"); 6373 | System.exit(0); 6374 | } 6375 | } 6376 | //if(flag!=1) 6377 | // { 6378 | int grade[]=new int[20]; 6379 | for(i=1;i=90) 6382 | { 6383 | grade[i-1]=a[i-1]; 6384 | grade[i]=1; 6385 | } 6386 | else if(a[i]>=80 && a[i]<90) 6387 | { 6388 | grade[i-1]=a[i-1]; 6389 | grade[i]=2; 6390 | } 6391 | else if(a[i]>=70 && a[i]<80) 6392 | { 6393 | grade[i-1]=a[i-1]; 6394 | grade[i]=3; 6395 | } 6396 | else 6397 | { 6398 | grade[i-1]=a[i-1]; 6399 | grade[i]=0; 6400 | } 6401 | } 6402 | for(i=0;i100) 6530 | { 6531 | 6532 | System.out.print("Number too large"); 6533 | System.exit(0); 6534 | } 6535 | 6536 | else 6537 | { 6538 | int a[]=new int[20]; 6539 | while(n>0) 6540 | { 6541 | a[i]=n%2; 6542 | i++; 6543 | n=n/2; 6544 | } 6545 | for(int j=i-1;j>=0;j--) 6546 | System.out.print (a[j]); 6547 | } 6548 | } 6549 | } 6550 | 93.reverseNumber 6551 | Read the question carefully and follow the input and output format. 6552 | 6553 | Write a program to find the reverse of a given input integer 6554 | 6555 | Input and Output Format : 6556 | Input consists of an integer, n. Output consist of the reverse of the number n. 6557 | 6558 | Print "Number too large" when the given input number is greater than 32767 6559 | Print "Number too small" when the given input numbers is a negative number. 6560 | 6561 | Sample Input 1: 6562 | 1234 6563 | 6564 | Sample Output 1: 6565 | 4321 6566 | 6567 | Sample Input 2: 6568 | 326357 6569 | 6570 | Sample Output 2: 6571 | Number too large 6572 | import java.util.Scanner; 6573 | public class Main 6574 | { 6575 | public static void main(String[] args) 6576 | { 6577 | int n, rev=0 ,rem; 6578 | //int flag=0; 6579 | Scanner in=new Scanner(System.in); 6580 | n = in.nextInt(); 6581 | if(n<0) 6582 | { 6583 | System.out.println ("Number too small"); 6584 | System.exit(0); 6585 | //flag=1; 6586 | } 6587 | if(n>32767) 6588 | { 6589 | System.out.println ("Number too large"); 6590 | System.exit(0); 6591 | //flag=1; 6592 | } 6593 | //if(flag==0) 6594 | //{ 6595 | while (n != 0) 6596 | { 6597 | rem =n%10; 6598 | rev = (rev *10)+rem; 6599 | n=n/10; 6600 | } 6601 | System.out.println (rev); 6602 | //} 6603 | } 6604 | } 6605 | 94.Sum of Prime Cubes 6606 | Given an input integer n, write a program to find the sum of the cubes of the prime numbers upto n. (including n) 6607 | 6608 | Please note that 1 is neither prime nor composite. 6609 | 6610 | The function returns -1 if the input is a negative number or if it is greater than 3000. 6611 | 6612 | If the function returns -1, print Invalid Input. 6613 | 6614 | Input and Output Format: 6615 | Input consists of a single integer. 6616 | Output consists of a single integer. 6617 | Refer sample output for formatting specifications. 6618 | 6619 | 6620 | Sample Input 1: 6621 | 5 6622 | 6623 | Sample Output 1: 6624 | 160 6625 | 6626 | Sample Input 2: 6627 | -241 6628 | 6629 | Sample Output 2: 6630 | Invalid Input 6631 | 6632 | Sample Input 3: 6633 | 50000 6634 | 6635 | Sample Output 3: 6636 | Invalid Input 6637 | 6638 | import java.util.Scanner; 6639 | public class Main 6640 | { 6641 | public static void main(String[] args) 6642 | { 6643 | int i,n,j, k,sum=0,flag=0; 6644 | Scanner in=new Scanner(System.in); 6645 | n = in.nextInt(); 6646 | if(n<0 || n>3000) 6647 | System.out.println ("Invalid input"); 6648 | else 6649 | { 6650 | for(i=2;i<=n;i++) 6651 | { 6652 | int flag=0; 6653 | for(j=2;j0) 6732 | { 6733 | output1[k]=a [i]; 6734 | k++; 6735 | } 6736 | } 6737 | for(i=0;ioutput1[j]) 6742 | { 6743 | temp = output1[i]; 6744 | output1[i]=output1[j]; 6745 | output1[j]=temp; 6746 | } 6747 | } 6748 | } 6749 | for(i=0;i9000 || days>31 || basic<0 || days<0) 6797 | { 6798 | System.out.println("Invalid input"); 6799 | System.exit(0); 6800 | } 6801 | spent=(basic*70)/100; 6802 | if(days==31) 6803 | savings = basic+500-spent; 6804 | else 6805 | savings = basic-spent; 6806 | System.out.printf(“%.2f”,savings); 6807 | } 6808 | } 6809 | 6810 | 97.studentMarks 6811 | Read the question carefully and follow the input and output format. 6812 | 6813 | The Given input is of the format XXXYY , where XXX is Id , YY is marks. Write a code to display the id and marks separately as given in the output formats. [Refer sample input and output] 6814 | 6815 | Input and Output Format : 6816 | Input consists of a number. Refer sample output for output format. 6817 | 6818 | Print "Number too large" when any of given input numbers is greater than 32767 . 6819 | Print "Number too small" when given input is a negative number. 6820 | 6821 | Include a function named studentMarks(int number) whose return type is void. 6822 | 6823 | Sample Input 1: 6824 | 12345 6825 | 6826 | Sample Output 1: 6827 | 123 6828 | 45 6829 | 6830 | Sample Input 2: 6831 | -13 6832 | 6833 | Sample Output 2: 6834 | Invalid input 6835 | 6836 | import java.util.Scanner; 6837 | public class Main 6838 | { 6839 | public static void main(String[] args) 6840 | { 6841 | int n=0,result; 6842 | Scanner in=new Scanner(System.in); 6843 | n = in.nextInt(); 6844 | if(n<0) 6845 | System.out.println ("number too small"); 6846 | else if(n>32767) 6847 | System.out.println ("number too large"); 6848 | else 6849 | { 6850 | int id=0,mark=0,rem=0,i=1,flag=1,j=1; 6851 | while(n!=0) 6852 | { 6853 | if(flag<=2) 6854 | { 6855 | rem=n%10; 6856 | mark=mark+(rem*i); 6857 | i=i*10; 6858 | n=n/10; 6859 | flag++; 6860 | } 6861 | else 6862 | { 6863 | rem=n%10; 6864 | id=id+(rem*j); 6865 | j=j*10; 6866 | n=n/10; 6867 | flag++; 6868 | } 6869 | } 6870 | System.out.println(id); 6871 | System.out.println(mark); 6872 | } 6873 | } 6874 | } 6875 | 98.maximumSum 6876 | Read the question carefully and follow the input and output format. 6877 | 6878 | Given an Integer array, find out sum of Even and odd Numbers individually and find the maximum. 6879 | 6880 | Input and Output Format : 6881 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements. Output consist of maximum of odd and even sum. 6882 | 6883 | 1) Print "Invalid array size" when size of the array is a negative number and terminate the program. 6884 | 2) Print "Invalid input" when there is any negative numbers available in the input array and terminate the program. 6885 | 6886 | Sample Input 1: 6887 | 5 6888 | 12 6889 | 13 6890 | 14 6891 | 15 6892 | 16 6893 | 6894 | Sample Output 1: 6895 | 42 6896 | 6897 | Sample Input 2: 6898 | -13 6899 | 6900 | Sample Output 2: 6901 | Invalid array size 6902 | 6903 | import java.util.Scanner; 6904 | public class Main 6905 | { 6906 | public static void main(String[] args) 6907 | { 6908 | int n, i, oddsum=0,evensum=0,max=0; 6909 | //int flag=0; 6910 | Scanner in=new Scanner(System.in); 6911 | n = in.nextInt(); 6912 | if(n < 0) 6913 | { 6914 | System.out.print("Invalid array size"); 6915 | System.exit(0); 6916 | } 6917 | else 6918 | { 6919 | int a[]=new int[n]; 6920 | for(i = 0; i< n; i++) 6921 | { 6922 | a[i] = in.nextInt(); 6923 | if(a[i] < 0) 6924 | { 6925 | //flag=1; 6926 | System.out.print("Invalid input"); 6927 | System.exit(0); 6928 | } 6929 | } 6930 | //if(flag!=1) 6931 | //{ 6932 | for(i=0;ioddsum) 6939 | max = evensum; 6940 | else 6941 | max=oddsum; 6942 | } 6943 | System.out.print(max); 6944 | //} 6945 | } 6946 | } 6947 | } 6948 | 99.newNumber 6949 | Read the question carefully and follow the input and output format. 6950 | 6951 | Write a program to find the difference between consecutive digits in the given input integer and display it. 6952 | 6953 | Input and Output Format: 6954 | Input consists of an integer and output the difference between the consecutive digits. 6955 | 6956 | Print "Number too small" if the number is less than 0 6957 | Print "Number too large" if the number is greater than 32767 6958 | 6959 | 6960 | Sample Input 1: 6961 | 1325 6962 | Sample Output 1: 6963 | 213 6964 | 6965 | Sample Input 2: 6966 | -13 6967 | Sample Output 2: 6968 | Number too small 6969 | import java.util.Scanner; 6970 | public class Main 6971 | { 6972 | public static void main(String[] args) 6973 | { 6974 | int n, rem,i=1,rem1=0,diff=0,digit=0; 6975 | Scanner in=new Scanner(System.in); 6976 | n = in.nextInt(); 6977 | if(n<0) 6978 | { 6979 | System.out.println ("Number too small"); 6980 | System.exit(0); 6981 | } 6982 | if(n>32767) 6983 | { 6984 | System.out.println ("Number too large"); 6985 | System.exit(0); 6986 | } 6987 | else 6988 | { 6989 | while(n>10) 6990 | { 6991 | rem=n%10; 6992 | n=n/10; 6993 | rem1=n%10; 6994 | if(rem>rem1) 6995 | diff=rem-rem1; 6996 | else 6997 | diff=rem1-rem; 6998 | digit=digit+(diff*i); 6999 | i=i*10; 7000 | } 7001 | System.out.println(digit); 7002 | } 7003 | } 7004 | } 7005 | 100.nonWorkingDoctors 7006 | Read the question carefully and follow the input and output format. 7007 | 7008 | A doctor survey results information is stored in 2 arrays. First array represents all doctors ids (working and non -working both). Second array represents only working doctor's id . Please find the doctor ids who are not working . 7009 | 7010 | Input and Output Format : 7011 | First line of input corresponds to n1, the size of first array and next n1 lines correspond to the elements of the first array. The next line corresponds to n2, the size of second array and next n2 lines correspond to the elements of the second array 7012 | Output is the id's of doctor who are not working 7013 | 7014 | Print "Invalid array size" when size of the array is a negative number and terminate the program 7015 | Print "Invalid id" when there is any negative numbers available in the input array and terminate the program. 7016 | 7017 | Include a function named nonWorkingDoctors(int total[],int working[],int n,int m) whose return type is void. 7018 | 7019 | Sample Input 1: 7020 | 7 7021 | 7 7022 | 2 7023 | 3 7024 | 4 7025 | 5 7026 | 6 7027 | 1 7028 | 3 7029 | 3 7030 | 4 7031 | 5 7032 | 7033 | Sample Output 1: 7034 | 7 7035 | 2 7036 | 6 7037 | 1 7038 | 7039 | Sample Input 2: 7040 | 7 7041 | 7 7042 | 2 7043 | 3 7044 | 4 7045 | 5 7046 | 6 7047 | -1 7048 | 7049 | Sample Output 2: 7050 | Invalid id 7051 | 7052 | import java.util.Scanner; 7053 | public class Main 7054 | { 7055 | public static void main(String[] args) 7056 | { 7057 | int m,n, i,j; 7058 | //int flag=0; 7059 | Scanner in=new Scanner(System.in); 7060 | m=in.nextInt(); 7061 | if(m < 0) 7062 | { 7063 | System.out.print("Invalid array size"); 7064 | System.exit(0); 7065 | } 7066 | else 7067 | { 7068 | int a[]=new int[m]; 7069 | for(i = 0; i< m; i++) 7070 | { 7071 | a[i] = in.nextInt(); 7072 | if(a[i] < 0) 7073 | { 7074 | //flag=1; 7075 | System.out.print("Invalid input"); 7076 | System.exit(0); 7077 | } 7078 | } 7079 | n=in.nextInt(); 7080 | if(n<0) 7081 | { 7082 | System.out.print("Invalid array size"); 7083 | System.exit(0); 7084 | } 7085 | int b[]=new int[n]; 7086 | for(i = 0; i< n; i++) 7087 | { 7088 | b[i] = in.nextInt(); 7089 | if(b[i] < 0) 7090 | { 7091 | //flag=1; 7092 | System.out.print("Invalid input"); 7093 | System.exit(0); 7094 | } 7095 | } 7096 | //if(flag!=1) 7097 | //{ 7098 | for(i=0;ia [j]) 7376 | { 7377 | temp = a [i]; 7378 | a [i] = a [j]; 7379 | a [j] = temp; 7380 | } 7381 | } 7382 | } 7383 | for(i=mid;i32767) 7442 | { 7443 | System.out.println ("Number too large"); 7444 | //flag=1; 7445 | System.exit(0); 7446 | } 7447 | //if(flag==0) 7448 | //{ 7449 | digit=in.nextInt(); 7450 | if(digit<0) 7451 | { 7452 | System.out.println ("Number too small"); 7453 | System.exit(0); 7454 | } 7455 | else if(digit >32767) 7456 | { 7457 | System.out.println ("Number too large"); 7458 | System.exit(0); 7459 | } 7460 | else 7461 | { 7462 | int rem,count=0; 7463 | while(n!=0) 7464 | { 7465 | rem=n%10; 7466 | if(rem==digit) 7467 | count++; 7468 | n=n/10; 7469 | } 7470 | System.out.println(count); 7471 | } 7472 | //} 7473 | } 7474 | } 7475 | 105.consecutiveDifference 7476 | Read the question carefully and follow the input and output format. 7477 | 7478 | Given input as integer array in which consecutive elements should have a difference of 4 or more than 4. 7479 | 7480 | If the above condition matches display “1” else “0” 7481 | 7482 | Input and Output Format : 7483 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements. Output consist of an integer, which is the either 1 or 0 7484 | 7485 | 1) Print "Invalid array size" when size of the array is a negative number and terminate the program. 7486 | 2) Print "Invalid input" when there is any negative numbers available in the input array and terminate the program. 7487 | 7488 | Sample Input 1: 7489 | 6 7490 | 1 7491 | 5 7492 | 10 7493 | 6 7494 | 2 7495 | 7 7496 | 7497 | Sample Output 1: 7498 | 1 7499 | 7500 | Sample Input 2: 7501 | -8 7502 | 7503 | Sample Output 2: 7504 | Invalid array size 7505 | import java.util.Scanner; 7506 | public class Main 7507 | { 7508 | public static void main(String[] args) 7509 | { 7510 | 7511 | int n, i,j, res=0,diff=0,f=0; 7512 | // int flag=0; 7513 | Scanner in=new Scanner(System.in); 7514 | n = in.nextInt(); 7515 | if(n < 0) 7516 | { 7517 | System.out.print("Invalid array size"); 7518 | System.exit(0); 7519 | } 7520 | else 7521 | { 7522 | int a[]=new int[n]; 7523 | for(i = 0; i< n; i++) 7524 | { 7525 | a[i] = in.nextInt(); 7526 | if(a[i] < 0) 7527 | { 7528 | //flag=1; 7529 | System.out.print("Invalid input"); 7530 | System.exit(0); 7531 | } 7532 | } 7533 | //if(flag!=1) 7534 | //{ 7535 | 7536 | for(i=0;ia[i+1]) 7540 | diff=a[i]-a[i+1]; 7541 | else 7542 | diff=a[i+1]-a[i]; 7543 | if(diff<4) 7544 | { 7545 | f=1; 7546 | break; 7547 | } 7548 | } 7549 | if(f==1) 7550 | System.out.print("0"); 7551 | else 7552 | System.out.print("1"); 7553 | //} 7554 | } 7555 | } 7556 | } 7557 | 7558 | 106.Change using Minimal Coins / Notes 7559 | 7560 | Ram needs to pay the school fees of his 6 year old kid. As he is busy with his work, he is not finding time to go to the school to make payment. His kid's school doesn't accept online payment. So he decided to send the fee amount through his kid. The available denominations of rupees or coins is 500, 100, 50, 10, 5 and 1. Can you write a program to find the minimal number of coins or notes to be given to his kid? 7561 | 7562 | Input and Output Format: 7563 | Input consists of a single integer that corresponds to the fee amount to be paid. 7564 | Output consists of an integer that corresponds to the minimal number of coins or rupee notes. 7565 | 7566 | Print Invalid Input if the input value is negative. 7567 | 7568 | Sample Input 1: 7569 | 682 7570 | 7571 | Sample Output 1: 7572 | 8 7573 | 7574 | 7575 | Sample Input 2: 7576 | -2345 7577 | 7578 | Sample Output 2: 7579 | Invalid Input 7580 | 7581 | import java.util.Scanner; 7582 | public class Main 7583 | { 7584 | public static void main(String[] args) 7585 | { 7586 | int rupees,count=0, c1,c2,c3,c4,c5,c6; 7587 | Scanner in=new Scanner(System.in); 7588 | rupees = in.nextInt(); 7589 | if(rupees<0) 7590 | { 7591 | count=-1; 7592 | System.out.print("Invalid input"); 7593 | System.exit(0); 7594 | } 7595 | else 7596 | { 7597 | c1=rupees/500; 7598 | rupees=rupees-(500*c1); 7599 | c2=rupees/100; 7600 | rupees=rupees-(100*c2); 7601 | c3=rupees/50; 7602 | rupees=rupees-(50*c3); 7603 | c4=rupees/10; 7604 | rupees=rupees-(10*c4); 7605 | c5=rupees/5; 7606 | rupees=rupees-(5*c5); 7607 | c6=rupees/1; 7608 | rupees=rupees-(1*c6); 7609 | count=c1+c2+c3+c4+c5+c6; 7610 | System.out.print(count); 7611 | } 7612 | } 7613 | } 7614 | 7615 | 107.singleDoubleTripCount 7616 | Read the question carefully and follow the input and output format. 7617 | 7618 | In a given input array find out number of single digits, double digits and triple digits. 7619 | 7620 | Input and Output Format : 7621 | First line of input consists of an integer, the size of the array. Next line correspond to the elements of the array. Output consist of an integer array. 7622 | 7623 | 1) Print "Invalid array size" when size of the array is a negative number and terminate the program. 7624 | 2) Print "Invalid input" when there is any negative numbers available in the input array and terminate the program. 7625 | 7626 | 7627 | The output array is stored in a global variable named count. The size of the output array is 6. 7628 | Sample Input 1: 7629 | 5 7630 | 1 7631 | 2 7632 | 23 7633 | 34 7634 | 456 7635 | 7636 | Sample Output 1: 7637 | 1 7638 | 2 7639 | 2 7640 | 2 7641 | 3 7642 | 1 7643 | 7644 | Sample Input 2: 7645 | 4 7646 | 1 7647 | 2 7648 | -3 7649 | 7650 | Sample Output 2: 7651 | Invalid input 7652 | import java.util.Scanner; 7653 | public class Main 7654 | { 7655 | public static void main(String[] args) 7656 | { 7657 | int n, i, c1=0,c2=0,c3=0; 7658 | // int flag=0; 7659 | Scanner in=new Scanner(System.in); 7660 | n = in.nextInt(); 7661 | if(n < 0) 7662 | { 7663 | System.out.print("Invalid array size"); 7664 | System.exit(0); 7665 | } 7666 | else 7667 | { 7668 | int a[]=new int[n]; 7669 | for(i = 0; i< n; i++) 7670 | { 7671 | a[i] = in.nextInt(); 7672 | if(a[i] < 0) 7673 | { 7674 | //flag=1; 7675 | System.out.print("Invalid input"); 7676 | System.exit(0); 7677 | } 7678 | } 7679 | //if(flag!=1) 7680 | //{ 7681 | int count[]=new int[6]; 7682 | count[0]=1; 7683 | count[2]=2; 7684 | count[4]=3; 7685 | for(i=0;i=0 && a[i]<=9) 7688 | count[1]=++c1; 7689 | else if(a[i]>=10 && a[i]<=99) 7690 | count[3]=++c2; 7691 | else if(a[i]>=100 && a[i]<=999) 7692 | count[5]=++c3; 7693 | else 7694 | ; 7695 | } 7696 | for(i=0;i<6;i++) 7697 | System.out.println (count[i]); 7698 | //} 7699 | } 7700 | } 7701 | } 7702 | 7703 | 108.Sum of multiples 7704 | 7705 | Given 2 integers n and m, write a program to find the sum of all numbers divisible by m upto and including n (i.e from 1 to n). 7706 | 7707 | The first argument corresponds to n and the second argument corresponds to m. The function returns -1 if the any of the two inputs is less than 0. 7708 | 7709 | If the function returns -1, print “Invalid Input”. 7710 | 7711 | Input and Output Format: 7712 | 7713 | The input consists of 2 integers. The 1st integer corresponds to n and the 2nd integer corresponds to m. 7714 | The output consists of an integer that corresponds to the sum of the multiples. 7715 | 7716 | Sample Input 1: 7717 | 11 7718 | 5 7719 | 7720 | Sample Ouput 1: 7721 | 15 7722 | 7723 | Sample Input 2: 7724 | -8 7725 | 2 7726 | 7727 | Sample Output 2: 7728 | Invalid Input 7729 | 7730 | import java.util.*; 7731 | public class Main 7732 | { 7733 | public static void main(String[] args) 7734 | { 7735 | int n=0,m=0,sum=0,i; 7736 | Scanner in=new Scanner(System.in); 7737 | n = in.nextInt(); 7738 | m = in.nextInt(); 7739 | 7740 | if(n<0 || m<0) 7741 | { 7742 | System.out.print("Invalid Input"); 7743 | System.exit(0); 7744 | } 7745 | else 7746 | { 7747 | for(i=m;i<=n;i++) 7748 | { 7749 | if(i%m==0) 7750 | sum = sum + i; 7751 | } 7752 | System.out.print(sum); 7753 | } 7754 | } 7755 | } 7756 | 7757 | 7758 | 109.Search Element 7759 | 7760 | Write a program to find whether a particular number appears in a given input array. 7761 | 7762 | Include a function named isElementPresent that accepts 3 arguments and returns an int. The first argument is the input array, the second argument is an int that corresponds to the size of the array and the third argument is the element to be searched for. The function returns 1 if the search element is present in the array and returns 0 if the search element is not present in the array. 7763 | 7764 | If the size of the array is negative or if any element in the array is negative, print “Invalid Input” and terminate the program. 7765 | 7766 | Input and Output Format: 7767 | Input consists of n+2 integers. The first integer corresponds to n, the number of elements in the array. The next 'n' integers correspond to the elements in the array. The last integer corresponds to the element whose count needs to be found. 7768 | Output consists of a string that is either 'yes' or 'no'. 7769 | 7770 | Refer sample output for formatting specifications. 7771 | 7772 | Assume that the maximum number of elements in the array is 20. 7773 | 7774 | Sample Input 1: 7775 | 8 7776 | 2 7777 | 1 7778 | 3 7779 | 8 7780 | 6 7781 | 8 7782 | 10 7783 | 8 7784 | 8 7785 | 7786 | Sample Output 1: 7787 | yes 7788 | 7789 | Sample Input 2: 7790 | -5 7791 | 7792 | Sample Output 2: 7793 | Invalid Input 7794 | 7795 | Sample Input 3: 7796 | 5 7797 | 23 7798 | 2 7799 | -200 7800 | 7801 | Sample Output 3: 7802 | Invalid Input 7803 | 7804 | import java.util.Scanner; 7805 | public class Main 7806 | { 7807 | public static void main(String[] args) 7808 | { 7809 | int n, i, se,f=0; 7810 | // int flag=0; 7811 | Scanner in=new Scanner(System.in); 7812 | n = in.nextInt(); 7813 | if(n < 0) 7814 | { 7815 | System.out.print("Invalid array size"); 7816 | System.exit(0); 7817 | } 7818 | else 7819 | { 7820 | int a[]=new int[n]; 7821 | 7822 | for(i = 0; i< n; i++) 7823 | { 7824 | a[i] = in.nextInt(); 7825 | if(a[i] < 0) 7826 | { 7827 | //flag=1; 7828 | System.out.print("Invalid input"); 7829 | System.exit(0); 7830 | } 7831 | } 7832 | //if(flag!=1) 7833 | //{ 7834 | se=in.nextInt(); 7835 | for(i=0;i100) 7893 | { 7894 | 7895 | System.out.print("Invalid array size"); 7896 | System.exit(0); 7897 | } 7898 | else 7899 | { 7900 | int a[]=new int[20]; 7901 | while(n>0) 7902 | { 7903 | a[i]=n%2; 7904 | i++; 7905 | n=n/2; 7906 | } 7907 | for(int j=i-1;j>=0;j--) 7908 | System.out.print (a[j]); 7909 | 7910 | } 7911 | } 7912 | } 7913 | 7914 | 7915 | 7916 | 111.Repeated Element 7917 | 7918 | Write a program to find the maximum repeated element in a given input array. 7919 | 7920 | Include a function named maxRepeatedElement that accepts 2 arguments and returns an int. The first argument is the input array and the second argument is an int that corresponds to the size of the array. The function returns an int that corresponds to the maximum repeated element. 7921 | 7922 | If the size of the array is negative or if any element in the array is negative, print “Invalid Input” and terminate the program. 7923 | 7924 | Input and Output Format: 7925 | Input consists of n+1 integers. The first integer corresponds to n, the number of elements in the array. The next 'n' integers correspond to the elements in the array. 7926 | Output consists of an integer that corresponds to the maximum repeated element. 7927 | Assume that the maximum number of elements in the array is 20 and that there will always be a unique maximum repeated element. 7928 | 7929 | Sample Input 1: 7930 | 8 7931 | 2 7932 | 1 7933 | 3 7934 | 4 7935 | 6 7936 | 8 7937 | 10 7938 | 8 7939 | 7940 | Sample Output 1: 7941 | 8 7942 | 7943 | Sample Input 2: 7944 | -5 7945 | 7946 | Sample Output 2: 7947 | Invalid Input 7948 | 7949 | Sample Input 3: 7950 | 5 7951 | 23 7952 | 2 7953 | -200 7954 | 7955 | Sample Output 3: 7956 | Invalid Input 7957 | 7958 | import java.util.*; 7959 | public class Main 7960 | { 7961 | public static void main(String[] args) 7962 | { 7963 | int n, i,j,k=0; 7964 | Scanner in=new Scanner(System.in); 7965 | n = in.nextInt(); 7966 | if(n < 0) 7967 | { 7968 | System.out.print("Invalid array size"); 7969 | System.exit(0); 7970 | } 7971 | else 7972 | { 7973 | int a[]=new int[100]; 7974 | for(i = 0; i< n; i++) 7975 | { 7976 | a[i] = in.nextInt(); 7977 | if(a[i] < 0) 7978 | { 7979 | System.out.print("Invalid input"); 7980 | System.exit(0); 7981 | } 7982 | } 7983 | for(i=0;i32767) 8131 | { 8132 | System.out.println ("Number too large"); 8133 | System.exit(0); 8134 | } 8135 | while(n!=0) 8136 | { 8137 | rem=n%10; 8138 | count=0; 8139 | for(i=1;i<=rem;i++) 8140 | { 8141 | if(rem%i==0) 8142 | count++; 8143 | } 8144 | if(count==2) 8145 | { 8146 | fact=1; 8147 | for(i=1;i<=rem;i++) 8148 | fact = fact *i; 8149 | sum = sum + fact; 8150 | } 8151 | n=n/ 10; 8152 | } 8153 | System.out.println (sum); 8154 | } 8155 | } 8156 | 8157 | 8158 | 8159 | 102.countEvenRepeat 8160 | Read the question carefully and follow the input and output format. 8161 | 8162 | Write a program to count the number of repeated even elements in a given input array. 8163 | 8164 | Input and Output Format : 8165 | First line of input consists of n, the number of elements. And the remaining n lines is the elements of the array. 8166 | Output is a single integer that displays the count. 8167 | 8168 | 1) Print "Invalid array size" when size of the array is a negative number and terminate the program . 8169 | 2) Print "Invalid input" when there is any negative numbers available in the input array and terminate the program. 8170 | 8171 | Include a function named countEvenRepeat(int array[], int size) whose return type is an integer, the count 8172 | 8173 | Sample Input 1: 8174 | 9 8175 | 8 8176 | 4 8177 | 5 8178 | 8 8179 | 4 8180 | 2 8181 | 1 8182 | 4 8183 | 5 8184 | 8185 | Sample Output 1: 8186 | 2 8187 | 8188 | Sample Input 2: 8189 | 10 8190 | 4 8191 | 5 8192 | 6 8193 | -8 8194 | 8195 | Sample Output 2: 8196 | Invalid input 8197 | 8198 | import java.util.*; 8199 | public class Main 8200 | { 8201 | public static void main(String[] args) 8202 | { 8203 | int n, i,j,k,count=0,sum=0; 8204 | Scanner in=new Scanner(System.in); 8205 | n = in.nextInt(); 8206 | if(n < 0) 8207 | { 8208 | System.out.print("Invalid array size"); 8209 | System.exit(0); 8210 | } 8211 | else 8212 | { 8213 | int a[]=new int[100]; 8214 | for(i = 0; i< n; i++) 8215 | { 8216 | a[i] = in.nextInt(); 8217 | if(a[i] < 0) 8218 | { 8219 | System.out.print("Invalid input"); 8220 | System.exit(0); 8221 | } 8222 | } 8223 | for(i=0;i32767) 8463 | { 8464 | System.out.println ("Number too large"); 8465 | System.exit(0); 8466 | } 8467 | while(n!=0) 8468 | { 8469 | rem=n%10; 8470 | count=0; 8471 | for(i=1;i<=rem;i++) 8472 | { 8473 | if(rem%i==0) 8474 | count++; 8475 | } 8476 | if(count==2) 8477 | { 8478 | fact=1; 8479 | for(i=1;i<=rem;i++) 8480 | fact = fact *i; 8481 | sum = sum + fact; 8482 | } 8483 | n=n/ 10; 8484 | } 8485 | System.out.println (sum); 8486 | } 8487 | } 8488 | 8489 | 60.adjecentDifference 8490 | Read the question carefully and follow the input and output format. 8491 | 8492 | Given an input Integer array, find the difference in the adjacent elements and print the largest difference 8493 | 8494 | Input and Output Format : 8495 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements. Output consist of largest adjacent difference. 8496 | 8497 | Print "Invalid array size" when size of the array is a negative number and terminate the program 8498 | Print "Invalid input" when there is any negative number available in the input array and terminate the program. 8499 | 8500 | Include a function named adjecentDifference(int numbers[], int size) whose return type is an integer 8501 | 8502 | Sample Input 1: 8503 | 7 8504 | 2 8505 | 4 8506 | 5 8507 | 1 8508 | 9 8509 | 3 8510 | 8 8511 | Sample Output 1: 8512 | 8 8513 | 8514 | Hint: The AdjecentElement Diff are :2,1,4,8,6,5 and Maximum diff is 8 which is obtained by 2-4 =2, 4-5=1,5- 1 =4, 1-9=8,9-3=6,3-8 =5 8515 | 8516 | Sample Input 2: 8517 | 5 8518 | 1 8519 | 7 8520 | 3 8521 | -8 8522 | Sample Output 2: 8523 | Invalid input 8524 | import java.util.*; 8525 | public class Main 8526 | { 8527 | public static void main(String[] args) 8528 | { 8529 | int n, i,diff,max=0; 8530 | Scanner in=new Scanner(System.in); 8531 | n = in.nextInt(); 8532 | if(n < 0) 8533 | { 8534 | System.out.print("Invalid array size"); 8535 | System.exit(0); 8536 | } 8537 | else 8538 | { 8539 | int a[]=new int[100]; 8540 | for(i = 0; i< n; i++) 8541 | { 8542 | a[i] = in.nextInt(); 8543 | if(a[i] < 0) 8544 | { 8545 | System.out.print("Invalid input"); 8546 | System.exit(0); 8547 | } 8548 | } 8549 | for(i=0;ia[i+1]) 8552 | diff=a[i]-a[i+1]; 8553 | else 8554 | diff=a [i+1]-a[i]; 8555 | if(diff>max) 8556 | max=diff; 8557 | } 8558 | System.out.print(max); 8559 | } 8560 | } 8561 | } 8562 | 8563 | 82.clearedStage1 8564 | Read the question carefully and follow the input and output format. 8565 | 8566 | Given an integer array. The first index represents the Student id, Second index represents C-programming marks and the third index Represents SQL marks. Write a program to find the Ids of students who have cleared both C-programming and SQL. 8567 | 8568 | Note :(1) The Pass Marks is >=70 8569 | 8570 | Input and Output Format : 8571 | 8572 | First line of input consists of n, the number of elements. Next n lines correspond to the array elements. Output consist of an integer array. 8573 | 8574 | 1) Print "Invalid array size" when size of the array is negative and terminate the program. 8575 | 2) Print "Invalid input" when there is any negative number available in the input array and terminate the program. 8576 | 8577 | Include a function named clearedStage1(int array[], int size) whose return type is void. 8578 | The output array is stored in a global variable named cleared. 8579 | 8580 | Sample Input 1: 8581 | 9 8582 | 1 8583 | 25 8584 | 75 8585 | 3 8586 | 75 8587 | 80 8588 | 2 8589 | 75 8590 | 75 8591 | 8592 | Sample Output 1: 8593 | 3 8594 | 2 8595 | 8596 | Sample Input 2: 8597 | 6 8598 | 4 8599 | 25 8600 | -78 8601 | 8602 | Sample Output 2: 8603 | Invalid input 8604 | import java.util.*; 8605 | public class Main 8606 | { 8607 | public static void main(String[] args) 8608 | { 8609 | int n, i,k=0; 8610 | Scanner in=new Scanner(System.in); 8611 | n = in.nextInt(); 8612 | if(n < 0) 8613 | { 8614 | System.out.print("Invalid array size"); 8615 | System.exit(0); 8616 | } 8617 | else 8618 | { 8619 | int a[]=new int[100]; 8620 | for(i = 0; i< n; i++) 8621 | { 8622 | a[i] = in.nextInt(); 8623 | if(a[i] < 0) 8624 | { 8625 | System.out.print("Invalid input"); 8626 | System.exit(0); 8627 | } 8628 | } 8629 | int b[]=new int[100]; 8630 | for(i=0;i=70 && a[i+2]>=70) 8633 | { 8634 | b[k]=a[i]; 8635 | k++; 8636 | } 8637 | } 8638 | for(i = 0; i< k; i++) 8639 | System.out.println(b[i]); 8640 | } 8641 | } 8642 | } 8643 | 8644 | 8645 | 8646 | --------------------------------------------------------------------------------