1. import java.
Scanner;
public class Test {
public static int sumToSingleDigit(int number) {
/**this function is created to loop until a number is 2 digit it will run
For example: if we call the sumOfDigits function with 9992 then it will return 29, so 29 is a two digit number that’s why
the loop will continue and once it is no longer a two digit number it will return the number*/
while (number >= 10) {
number = sumOfDigits(number);
}
return number;
}
public static int sumOfDigits(int number) {
/** This function is created to first do a mod operation to extract the remainder of the digit, then it will add with the sum
variable which was initiated with 0, once that is done it will do a division with the initial number, this loop will continue
until the number is greater than 0, once it is less than or equals to zero it will return the sum variable*/
int sum = 0;
while (number > 0) {
sum += number % 10;
number /= 10;
}
return sum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]); //creating a scanner object from Scanner Class
[Link]("Enter a number: "); //this is just to tell the user
int number = [Link](); // the nextInt() function use to take only integer
[Link](); // closing the scanner
int result = sumToSingleDigit(number);//calling this method with the number
[Link]("The single digit sum is: " + result); // printing the final result
}
}
2. import [Link];
public class Test {
public static boolean checkPalindrome(int number) {
/** I am using this function to check if a number is palindrom or not, so the basic working of this is it will
convert the integer to string by concating with an empty string so I will be having the length function by which I will be
forming the loop, and then I am creating a variable j which is holding the length of the string minus 1 as in array the last
element is always length – 1, then inside the loop I am using the charAt function with the ‘I’ and ‘j’ variable to compare
the string. So j is just looping the string from the end and I is to loop the string from the start so I will be comparing
individual characters from start and end once the condition is satisfied it will return true else it will return false */
String pal = number + "";
int j = [Link]() - 1;
for (int i = 0; i < [Link]() / 2; i++) {
if ([Link](i) != [Link](j)) {
return false;
}
j--;
}
return true;
}
public static void makeItPalindrom(int number) {
/** if the number is not found to be palindrome then the helper function will call this function with the current number ,
and here I am creating stringbuilder which is holding the string , the string builder have a inbuilt function called reverse ,
which I have used to reverse the string of integer, then once it is reversed I am converting back to integer using the
parseInt fucntions of Integer class and then adding the two numbers , and sending it back to the helper function to check
if they are palindrome yet or not */
String pal = number + "";
StringBuilder instrng = new StringBuilder([Link]());
[Link](pal);
String p = [Link]().toString();
int sum = [Link](p) + number;
helper(sum);
}
public static void helper(int number) {
/** this is the helper function which is only used to call the checkPalindrome and makeItPalindrome function with the
numbers which it received either from the main function or from the makeItPalindrome function.*/
if (checkPalindrome(number)) {
[Link]("\nThe number is in Palindrom");
} else {
makeItPalindrom(number);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("\nEnter a number: ");
int number = [Link]();
[Link]();
helper(number);//calling the helper function
}
}
3. import [Link];
public class ReversingNumberWithoutMod{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Please enter the number: ");
int number = [Link]();
[Link]();
int finalNumber = 0;
while (number != 0) {
/** this while loop will loop until the number which i am taking from the user becomes 0, once it become 0 the
loop is going to stop.
* i have declated a variable named finalNumber in the main method which is initialized with 0.
* the important operation is going on in the temp variable in which first the number is divided with 10, for
example if the number variable become 2897 then after the division it is going to be 289 as the division operator only
gives the divident not the remainder.
* Then the next step is to multiply it with 10, now the new number after multiplication with 10 it becomes 2890,
and as per BODMAS rule the subtraction is going to happen after the multiplication so right now i am subtracting it with
the initial number, which is 2897 so the answer will be 7, and it will be alocated to temp variable.
* and the final step is to re-initialize the number variable with number divided by 10 which in our case will be 289
and the loop continues until number is zero and stops once it become 0.
*/
int temp = number - (number / 10) * 10;
finalNumber = finalNumber * 10 + temp;
number = number / 10;
}
[Link]("\n" + finalNumber);
}
}
4. import [Link];
public class Test {
public static int checkUnique(int number) {
/** multiplying with the unit value of the number*/
int temp = number % 10;
return temp * number;
}
public static int rev(int number) {
/** Reused the logic from answer 3rd, only difference is here i am returning the final value instead of printing it and
taking the number input from the helper function*/
int finalNumber = 0;
while (number != 0) {
int temp = number - (number / 10) * 10;
finalNumber = finalNumber * 10 + temp;
number = number / 10;
}
return finalNumber;
}
public static void helper(int number) {
/** this helper function is used here to just call both the functions and check if it is equal or not if it is equal then it is a
unique number if not then it is not unique*/
if (checkUnique(number) == rev(number)) {
[Link]("Unique Number");
} else {
[Link]("Not Unique Number");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("\nEnter a number: ");
int number = [Link]();
[Link]();
helper(number); // calling the helper function
}
}
5. import [Link];
public class Test {
public static void main(String[] args){
for(int i = 0; i<=1000; i++){
/** This for loop is looping from 1 to 1000*/
int mainNumber = i; //this variable is storing the current value of i
int reverseNumber = 0; //this variable is initialized with zero
while(mainNumber != 0){
/** This while loop is looping with the initial value of i, which is then stored in
the variable mainNumber, so until the mainNumber variable becomes 0 it will run.
* inside the loop i have initialized a temp variable which which is using mod operator on mainNumber
variable to get the remainder.
* next i am re-initiallizing the reverseNumber variable with multiplying with 10 and adding with the temp variable,
let's take one example; let the i be 989 at this moment, and when calculating the modulous it is giving 9 and when i am
multiplying with the initial value of reverseNumber which was 0 with 10 and adding the temp number it becomes 9 as
*(0*10+9) = 9
*(9*10+8) = 98
*(90*10+8) = 989
* and the final step is to check equality between the initial value of i and reverse number
* so in our case reverseNumber == i (989 == 989) which will give true and hence it is a palindrome number.*/
int temp = mainNumber % 10;
reverseNumber = reverseNumber * 10 + temp;
mainNumber = mainNumber/10;
}
if(reverseNumber == i){
[Link](i + " is a Palindrome");
}
}
}
}
6. import [Link];
import [Link];
public class Test {
public static void main(String[] args){
/** In this program I have first taken the input from the user and then stored in a array with length = 2, then once it is
stored in the array I am using the [Link] from the array class to sort the array ascending to descending, then just
accessing the last element or the 1 element as that will be the larger element in the array*/
Scanner sc = new Scanner([Link]);
int number[] = new int[2];
[Link]("Enter first number :");
number[0] = [Link]();
[Link]("Enter second number :");
number[1] = [Link]();
[Link](number);
[Link]("The Biggest Number is: "+number[1]);
}
}
7. import [Link];
import [Link];
public class Test {
public static void main(String[] args){
/** In this program first I am initiating a linked list , I can use array too but as I will not be knowing the length of the
binary digits of different number so choosed linked list to dynamically handle the storing of each digits.
* So the logic of converting any decimal to array is basically I will be taking input from the user and storing it in a variable
called number and then I am doing a loop using while loop and the looping will continue once the number is 0.
* Inside the loop I am initiating a variable called temp in which I am storing the value of number mod by 2.
* then adding the temp value to the list by using the inbuilt function add()
* and finally re-initiating the number by dividing it with two.
* And then just looping through the linked list and printing the value.*/
Scanner sc = new Scanner([Link]);
[Link]("Enter the number");
int number = [Link]();
LinkedList<Integer> bs = new LinkedList<>();
while(number != 0){
int tempValue = number % 2;
[Link](tempValue);
number = number/2;
}
for(int i = [Link]()-1; i>=0; i--){
[Link]([Link](i));
}
}
}
8. import [Link];
public class Test {
public boolean duplicateChecker(int value, int index, int arr[]) {
/** This non static function is used to check if the entered value by the user is present in the array or not, this function is
accepting in the parameter the current entered value, the current index and the array filled so far, the loop is working
upto the index, once the i variable in the for loop is bigger than the index value then it is stopping, while looping into the
array it is checking the value and the value inside the array with all the index generating from the for loop.*/
for (int i = 0; i < index; i++) {
if (arr[i] == value) {
return true;
}
}
return false;
}
public static void main(String[] args) {
/** Here in the main method I am creating an object of the current class to access the duplicateChecker method.
* Then I am initiating a empty array with the length of 10, then in the while loop I am giving the user to enter the
number, then once the user enters a number it is checking a condition in for loop which is will work only if
duplicateChecker method returns false, I am calling the duplicateChecker method with the parameter it is initialized with.
If the duplicateChecker is returning true that means the entered number already exists in the array and the else condition
will work now and print “The number already exist please enter a new value” and waits for the user entry once the user
enters a new value the loop continues.*/
Test t = new Test();
Scanner sc = new Scanner([Link]);
int numberContainer[] = new int[10];
int i = 0;
while (i < 10) {
[Link]("Enter the Value: ");
int userInput = [Link]();
if () {
numberContainer[i] = userInput;
i++;
} else {
[Link]("The number already exists; please enter new value: ");
}
}
[Link]();
for (int j = 0; j < [Link]; j++) {
[Link]("main " + numberContainer[j]);
}
}
}
9.
10. import [Link];
public class Test {
public static void main(String[] args) {
/** In this program I am first accepting the input from the user and storing it in respective variables,
*The variables are firstInt, secondInt and operations, so the firstInt will hold the first number and the secondInt will hold
the second integer entered by the user, the operations will accept any character but it is instructed to the user to enter
only +,-, / and * , then using a switch case I am performing the complete calculation operations*/
Scanner sc = new Scanner([Link]);
[Link]("Please Enter the Number: ");
int firstInt = [Link]();
[Link]("Please Enter the Second Number: ");
int secondInt = [Link]();
[Link]("Please Select the Operation(+ - / *): ");
char operation = [Link]().charAt(0);
switch (operation) {
case '+':
[Link](firstInt + " + " + secondInt + " = " + (firstInt + secondInt));
break;
case '-':
[Link](firstInt + " - " + secondInt + " = " + (firstInt - secondInt));
break;
case '/':
if (secondInt == 0) {
[Link]("Cannot divide with 0");
} else {
[Link](firstInt + " / " + secondInt + " = " + (firstInt / secondInt));
}
break;
case '*':
[Link](firstInt + " * " + secondInt + " = " + (firstInt * secondInt));
break;
default:
break;
}
[Link]();
}
}
11. import [Link];
import [Link];
public class Test {
public static void main(String[] args) {
/** Here also I am taking help of liked list and using a for loop adding the character from A-Z to the list ,
* In the next step I am converting the list to array to easily iterate using for loop, we can also use iterator too to loop in
the list.
* And then using simple for loop I am printing all the required items.*/
LinkedList<Character> alphabets = new LinkedList<>();
for(char i = 'A'; i<='Z'; i++){
[Link](i);
}
Object alphabetArray [] = [Link]();
[Link]("All the English Alphabets: ");
for(int i = 0; i<26;i++){
[Link](alphabetArray[i]+" ");
}
[Link]("\n");
[Link]("The first 6 English Alphabets: ");
for(int i = 0; i<6;i++){
[Link](alphabetArray[i]+" ");
}
[Link]("\n");
[Link]("The last 10 English Alphabets: ");
for(int i = 16; i<26;i++){
[Link](alphabetArray[i]+" ");
}
[Link]("\n");
[Link]("The 10th English Alphabet: ");
[Link](alphabetArray[10]+" ");
}
12.
13.
14. import [Link];
public class Test {
public static void main(String[] args) {
/** This a straight forward program using nested if for finding if any number which is entered by the user is divisible by
the number 5 or 6 | 5 and 6 | neither by 5 & 6. */
Scanner sc = new Scanner([Link]);
[Link]("Please Enter the number: ");
int userInput = [Link]();
if((userInput%5==0)&&(userInput%6==0)){
[Link](userInput +" is divisible by both 5 and 6");
}else if((userInput%5!=0)&&(userInput%6!=0)){
[Link](userInput +" is not divisible by either 5 or 6");
}else if((userInput%5!=0)&&(userInput%6==0)){
[Link](userInput +" is divisible by 6, but not both");
}else if((userInput%5==0)&&(userInput%6!=0)){
[Link](userInput +" is divisible by 5, but not both");
}else {
[Link]("No Response");
}
}
}
15. public class Test {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
/** This for loop is printing the top row, first the i variable is starting with 0 so instead of printing the 0 I kept an if
condition to print an empty space, and once the I variable is not 0 then it should print the rest of the integers, here I am
using the print formatters with ‘3d’ as the largest number the below for loop is going to produce is a 2 digit number so I
kept the 3 character padding for rows.*/
if (i == 0) {
[Link](" ");
} else {
[Link]("%3d", i);
}
}
[Link]();
for (int i = 1; i < 10; i++) {
/** This is the inside for loop which will be having another for loop the first loop prints the label of the multiplication
table with 2 padding so it I need to print 1 the the 1 will be printed as “_1” but if I want to print the value I have 3d which
means If I print 1 it will be “_ _1” and if 10 then “_10” which balances the spacing, and in the second loop basically I am
multiplying the variable or the first for loop with the variable of the second for loop inside the second for loop*/
[Link]("%2d", i);
for (int j = 1; j < 10; j++) {
[Link]("%3d", i * j);
}
[Link]();
}
}
}
16.
17. import [Link].*;
public class Test {
public static void main(String[] args){
/** In this program I am using tree map because I wanted to store both the string and the integer inside a same element
so here the mark is integer and the name is string, the mark is the key and the name is the value here, as I have
instructed in the question to show the lowest mark student and the highest mark student only , the treemap is helping to
do that by sorting the elements based on the key internally, and I am using the inbuilt function of treemap (firstEntry)
and the (lastEntry) to get the first and last element of the map as it is already sorted ascending to descending order, and
then using the getValue() and getKey() methods I am printing the final result.*/
TreeMap<Integer, String> student = new TreeMap<>();
Scanner sc = new Scanner([Link]);
[Link]("Please Enter the number of students: ");
int totalStudent = [Link]();
[Link]();
while(totalStudent > 0){
[Link]("Please Enter the Student Name: ");
String name = [Link]().trim();
[Link]("Please Enter "+ name + "'s Mark: ");
int mark = [Link]();
[Link]();
[Link](mark,name);
totalStudent--;
}
[Link]();
[Link]<Integer, String> lowestMark = [Link]();
[Link]<Integer, String> highestMark = [Link]();
[Link]("Name: " + [Link]() +" Mark: "+ [Link]()+" secured the
Lowest");
[Link]("Name: " + [Link]() +" Mark: "+ [Link]()+" secured the
Highest");
}
}
18. public class Test {
public static void divisionChecker(int numberToCheck){
/** I am checking the number divisibility by putting them in a if condition, so it it is divisible by both the number 5 and 6
then it is going to print the data.*/
if((numberToCheck%5==0)&&(numberToCheck%6==0)){
[Link](numberToCheck +" is divisible by both 5 and 6");
}
}
public static void main(String[] args) {
for(int i= 100; i<=1000; i++){
/** this is a for loop counting from 100 to 1000 and calling the divisionChecker function with the variable of for loop*/
divisionChecker(i);
}
}
}
19.
20.
21.
22.
23. import [Link];
public class Test {
public static void main(String[] args) {
/** Here in this program I am using the inbuilt function of year class isLeap() to find out the year is a leap year or not. */
for(int i = 1900; i<= 2011; i++){
/** In this for loop I am counting the year from 1900 to 2011 and checking with the isLeap function in the if condition.*/
if([Link](i)){
[Link](i+" is a Leap Year");
}
}
}
}
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.