Java 40
Java 40
long RevNumber=0;
while (n>0)
RevNumber=(RevNumber*10)+(n%10);
n=n/10;
Fibn=Fibn-1+Fib(n-2)
return 1;
num = scan.nextInt();
if(num%i == 0)
count++;
break;
if(count == 0)
System.out.print("Prime Number");
}
else
int temp = a;
a = b;
b = temp;
b = b + a;
a = b - a;
b = b - a;
int a = 10;
int b = 20;
swapNumbers(a, b);
int N = 200;
int k;
k = sc.nextInt();
int i = 0;
System.out.println(num);
i++;
if (num % i == 0) {
return false;
return true;
}
if(map.containsKey(array[i])) {
result[0] = map.get(array[i]);
result[1] = i;
} else {
map.put(difference, i);
return result;
}
7. Check if a given number is palindrome or not.
Ans. A palindrome is a number which is when reversed give the
same number. We find the reverse and check whether it is equal
to the given number or not.
if(str.charAt(i) != str.charAt(length-1-i)) {
System.out.println("Snot a palindrome.");
isPalindrome = false;
break;
if(isPalindrome) {
System.out.println("palindrome.");
int count = 0;
int sum = 0;
while(num > 0)
count++;
num = temp;
int val = 1;
if(temp == sum)
{ System.out.println("Armstrong number");
else
N!=1*2*3*4*…*N-1*N
int i,j,fact=1;
i=Integer.parseInt(br.readLine());
for(j=1;j<=i;j++) {
fact=fact*j;
System.out.println(fact);
a = (a % b);
} else {
b = (b % a);
if ((a == 0)) {
return b;
} else {
return a;
System.out.println(GCD.gcd(a, b));
boolean isLeap;
if(year%4==0){
if(year%100==0){
if(year%400==0){
isLeap = true;
}else{
isLeap = false;
}else{
isLeap = true;
}else{
isLeap = false;
if(isLeap)
System.out.println("leap year");
else
isLeapYear(1992);
isLeapYear(2000);
isLeapYear(2001);
isLeapYear(2002);
isLeapYear(2004);
int i=0;
System.out.println("Vowel");
else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
System.out.println("Consonant");
else
System.out.println("Symbols");
Input: a
Output: Vowel
{
if(num>a[a.length-1]||num<a[0])return -1;
int start=0;
int mid=(start+end)/2;
while(a[mid]!=num){
if(num<mid){
end=mid;
}else{
start=mid;
mid = (start+end)/2;
return mid;
boolean f = false;
char[] c = A.toCharArray();
Arrays.sort(c);
char[] d = B.toCharArray();
Arrays.sort(d);
if (a.equals(b)) {
f=true;
return f;
String A=sc.next();
String B=sc.next();
boolean ret=isAnagram(A,B);
if(ret)System.out.println("Anagrams");
validateInput(numbers);
while (swap) {
swap = false;
for (int i = 0; i < length - 1; i++) {
swap(numbers, j, j + 1);
swap = true;
if(last - first == 0)
a[first] = a[last];
a[last] = temp;
else
private static void merge(int[] a, int first, int mid, int last)
j++;
i++;
else
break;
else
else
j++;
matrixTwo[i][j] = in.nextDouble();
return c;
int n=sc.nextInt();
int i = 0;
while(n > 0)
bin[i++] = n%2;
n = n/2;
System.out.print(bin[j]);
char
ch[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int rem,num;
String hexd="";
num=sc.nextInt();
while(num != 0)
rem=num%16;
num= num/16;
System.out.print(hexd);
1ni=n*(n+1)/2
int n,sum=0;
sum = (n*(n+1))/2;
System.out.println(sum);
long n,sum=0;
n=sc.nextLong();
int i=1;
while(i<=n/2)
if(n%i==0)
sum+=i;
i++;
if(sum==n)
else
num1 = temp;
return num1;
long number=-9999,input=-9999;
try{
number=Long.parseLong(System.console().readLine());
input=number;
}catch(NumberFormatException nfe){
System.exit(1);
long remainder=0,total=0;
remainder=number % 10;
total+=remainder;
number=number/10;
System.out.println(total);
row1 = in.nextInt();;
col1 = in.nextInt();
row2 = in.nextInt();
col2 = in.nextInt();
if(col1==row2)
mat2[i][j] = in.nextInt();
for ( j= 0 ; j <col2;j++)
sum=0;
for ( k= 0 ; k <row2;k++ )
sum +=mat1[i][k]*mat2[k][j] ;
res[i][j]=sum;
return reverse;
}
28. Write a program to reverse a string.
Ans. We use the string buffer class to easily reverse the string
word = "thisisstring";
System.out.printf(reverse);
return source;
return reverse;
selectedValue =n;
swapValues = arr[selectedValue];
arr[selectedValue] = arr[i];
arr[i] = swapValues;
sortArr(arr);
int value = 7;
for (int i=0 ;i< size-1; i++){
if(array[i]==value){
System.out.println(i);
}else{
Output: 2
int count = 0;
if (begin == null) {
System.out.println("Empty");
return;
System.out.println();
SinglyLinkedList p = begin;
while(p != null) {
count++;
p = p.next;
System.out.println();
int data;
Node next;
this.data = item;
}*/
prev = list.head;
current = prev.next;
next = current.next;
current.next = prev;
if (firstReverse) {
prev.next = null;
firstReverse = false;
prev = current;
current = next;
next = next.next;
current.next = prev;
list.head = current;
public LinkedList() {
while(current.next() != null){
current = current.next();
current.setNext(node);
fast = fast.next.next;
slow = slow.next;
if(fast == slow ){
return true;
return false;
int count = 0;
while(current != null)
{ sb.append(current.data).append("-->");
current = current.next();
count++;
if(count == 10)
break;
return sb.toString();
this.data = data;
return data;
this.data = data;
}
public Node next() {
return next;
this.next = next;
@Override
return this.data;
if(false){
linkedList.appendIntoTail(new LinkedList.Node("101"));
linkedList.appendIntoTail(new LinkedList.Node("201"));
linkedList.appendIntoTail(new LinkedList.Node("301"));
linkedList.appendIntoTail(new LinkedList.Node("401"));
}else{
linkedList.appendIntoTail(new LinkedList.Node("101"));
LinkedList.Node cycle = new LinkedList.Node("201");
linkedList.appendIntoTail(cycle);
linkedList.appendIntoTail(new LinkedList.Node("301"));
linkedList.appendIntoTail(new LinkedList.Node("401"));
linkedList.appendIntoTail(cycle);
if(linkedList.isCyclic()){
System.out.println("contains loop");
else{
System.out.println("no loop");
map.put(split[i], count+1);
else {
map.put(split[i], 1);
System.out.println(map);
int count = 0;
if (chars[i] == chars[j]) {
System.out.println(chars[j]);
count++;
break;
}
int largest = 0;
int secondLargest = 0;
System.out.print(arr[i] + "\t");
secondLargest = largest;
largest = arr[i];
secondLargest = arr[i];
}
System.out.println(secondLargest);
if (duplicate.contains(letter)) {
continue;
if (nonDuplicate.contains(letter)) {
nonDuplicate.remove((Character) letter);
duplicate.add(letter);
} else {
nonDuplicate.add(letter);
}
return nonDuplicate.get(0);
matrics[i][j] = sc.nextInt();
matricsT[i][j] = matrics[j][i];
System.out.println("");
}
System.out.println(str.substring(i, j));
PrintSubstring(str, str.length());
int count=0;
if(arr[i]==0)
{
count++;
arr[i]=0;
arr[i]=1;
return arr;
int[] arr1={1,3,6,4,2};
System.out.println(missingNumber(arr1));
int n=arr.length+1;
int sum=n*(n+1)/2;
int restSum=0;
for (int i = 0; i < arr.length; i++) {
restSum+=arr[i];
int missingNumber=sum-restSum;
return missingNumber; }
Output: 5
43. You are given an array, find the subarray with a given
sum.
Ans.
int left = 0;
int sum = 0;
if (arr.length == 0) {
System.out.println("-1");
return;
if (arr.length == 1) {
if (arr[0] == target) {
} else {
System.out.println("-1");
}
sum += arr[left] + arr[right];
if (sum == target) {
return;
right++;
if (right = 0) {
int n = in.nextInt();
arr[i] = in.nextInt();
ss.findSubarraySum(arr, target);
int count=0;
for(int i=0;i<num;i++){
arr[i] = sys.nextInt();
for(int i=0;i<num;i++){
count=1;
for(int j=i+1;j<num;j++){
count++;
arr[j]='\0';
if(arr[i]!='\0'){
System.out.println(arr[i]);
if (set.contains(a[i])) {
majority = i;
} else {
set.add(a[i]);
System.out.println(a[firstrepeating]);
int n = arr.length;
firstrep(arr,n);
Output: 2
findMaxSubArray(Arr);
int maxEndIndex = 0;
int cumulativeSum = 0;
int maxStartIndexUntilNow = 0;
maxSum = cumulativeSum;
maxStartIndex = maxStartIndexUntilNow;
maxEndIndex = currentIndex;
if (cumulativeSum < 0) {
maxStartIndexUntilNow = currentIndex + 1;
cumulativeSum = 0;
System.out.println(maxSum);
int order ;
array_num[i] = reader.nextInt();
order = reader.nextInt();
reader.close();
int k = 0;
if (j = order ) {
results[j] = array_num[k];
k++;
System.out.println(Arrays.toString(results));
return 0;
}
int n = nums.length;
int start = 0;
int end = n - 1;
return mid;
start = mid;
} else {
end = mid;
if (fastPtr.next != null) {
slowPtr = slowPtr.next;
fastPtr = fastPtr.next.next;
} else {
fastPtr = fastPtr.next;
return slowPtr.data;
s1.push(l1);
l1 = l1.next;
s2.push(l2);
l2 = l2.next;
int carry = 0;
if (!s1.isEmpty()) {
sum += s1.pop().val;
if (!s2.isEmpty()) {
sum += s2.pop().val;
result.push(new ListNode(sum));
if (carry != 0) {
result.push(new ListNode(carry));
while (!result.isEmpty()) {
node.next = result.pop();
node = node.next;
}
return temp.next;